Access Javascript Private Variables Using Greasemonkey

function Foo1() { var bar = 'test'; } 

AFAIK, it’s not possible to access a panel of variables from a global scope unless one of them writes a privileged function, for example

 function Foo2() { var bar = 'test'; this.read = function(){return bar;}; } 

Does Greasemonkey (or some other tool) provide any means to access the variable bar, with the exception of overriding the entire Foo1 function with Foo2? Greasemonkey has a GM_xmlhttprequest that wraps certain restrictions, so I was wondering if this could do this and save some problems.

I'm currently trying to read and write a private variable built into a function, which itself is also in a separate .js include. Thus, I cannot directly modify the script, and I need to load .js using AJAX, perform the modification, and then overwrite the original script. This is very cumbersome, and I would like this method to be easier.

+4
source share
2 answers

GM_xmlhttprequest simply bypasses browser protection. On the other hand, the variable area is part of the Javascript language and virtual machine. With the exception of modifying the Javascript VM, there is no access to these 'private' variables outside the scope.

+2
source

If Foo1 not a native function and you know the function template, you can get the contents of the variable using:

 var bar = /bar = '(\w+)'/.test(Foo1.toString()) && RegExp.$1; alert(bar); // will return "test" 
-1
source

Source: https://habr.com/ru/post/1483973/


All Articles