Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
function Foo1() {
    var bar = 'test';
}

AFAIK, it is impossible to access the variable bar from global scope, unless one writes a privileged function to do so, such as

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

Does Greasemonkey (or any other tool) provide any means to access the variable bar, short of re-defining the entire function Foo1 with Foo2? Greasemonkey has GM_xmlhttprequest which sidesteps certain restrictions, so I was wondering if it could do this too and save me some issues.

What I am trying to do currently is to read and write a private variable embedded in a function, which is also itself located in a separate .js include. Thus, I cannot directly modify the script, and I have to load the .js with AJAX, carry out the modification, and then overwrite the original script. This is very cumbersome and I would like an easier way to carry this task out.

share|improve this question
    
I don't think it's possible with Greasemonkey. You would need to use a Firebug-like tool (or API) which can inspect (and change) internal of the Javascript engine. –  Bergi Jun 1 '13 at 20:04
    
Since GM could run scripts under its own context, I was wondering whether it could do something similar and run under a private context, it would help greatly for debugging JS that was coded as private on certain sites. –  March Ho Jun 1 '13 at 20:22

2 Answers 2

GM_xmlhttprequest just bypasses browser security. Variable scope, on the other hand, is part of the language and the Javascript VM. Short of modifying the Javascript VM, there is no way to access those 'private' variables outside the function scope.

share|improve this answer

If Foo1 is not a native function and you do know the pattern of the function, then you can get the content of the variable by using:

var bar = /bar = '(\w+)'/.test(Foo1.toString()) && RegExp.$1;
alert(bar); // will return "test"
share|improve this answer
1  
This would only work with static, string-based variables, and you still cannot use this to write the variable without completely recreating the function. Interesting hack, though. –  March Ho Jun 2 '13 at 5:44
    
The example should work without static, string-based variables too. I just followed your example. Basically you can't do what you want without this hack. Give a full practical example and I might give you another example. –  w35l3y Jun 2 '13 at 11:49
    
what I mean is this: –  March Ho Jun 3 '13 at 12:29
    
function Foo1() { var bar = Math.random(); } Will the value still be able to read using this hack? I want the current value of random(), not the script itself. –  March Ho Jun 3 '13 at 12:30
    
This fool example is even easier... alert(Math.random()) should return the value you are looking for. :P As I said, give a practical example. –  w35l3y Jun 3 '13 at 22:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.