it’s been an hour I’m going crazy with this problem.
There is a page, with a javascript function:
<script>
function foofunction(foo){
dosomething(foo);
}
</script>
I have replaced this function with a greasemonkey script:
var scriptCode = new Array();
scriptCode.push('function foofunction(foo){ ');
scriptCode.push(' dosomething(foo); ');
scriptCode.push(' myinjectedcode; ');
scriptCode.push('}' );
var script = document.createElement('script');
script.innerHTML = scriptCode.join('\n');
scriptCode.length = 0;
document.getElementsByTagName('head')[0].appendChild(script);
//here I have to access to foo value
This works.
Now I have to access to foo value in my greasemonkey script, how can I?
Why are you making your JavaScript function inside a array? I’m assuming
foofunctionis a global function, why don’t you just make a function insidewindow(orunsafeWindow, I think, for Greasemonkey)?foois a local variable, so it only exists inside thefoofunctionfunction. If you want access to it outside of that, you’d need to make it a global variable.Problem with that is,
myFoowill only be set afterfooFunctionis ran, and there’s no good way for you to wait until then. I suggest making a “callback” function.But, this is kinda pointless as your injecting code into
fooFunctionanyway.So, to answer your question, you cannot get the value of a local variable outside of the function. Since you’re injecting code anyway, why don’t you just put all code relating to
fooinsidefooFunction?