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 foofunction
is a global function, why don't you just make a function inside window
(or unsafeWindow
, I think, for Greasemonkey)?
unsafeWindow.foofunction = function(foo){
dosomething(foo);
//myinjectedcode;
}
foo
is a local variable, so it only exists inside the foofunction
function. If you want access to it outside of that, you'd need to make it a global variable.
unsafeWindow.foofunction = function(foo){
dosomething(foo);
//myinjectedcode;
unsafeWindow.myFoo = foo;
}
unsafeWindow.myFoo; // will be set to `foo`, but only after `fooFunction` is ran
Problem with that is, myFoo
will only be set after fooFunction
is ran, and there's no good way for you to wait until then. I suggest making a "callback" function.
unsafeWindow.foofunction = function(foo){
dosomething(foo);
//myinjectedcode;
unsafeWindow.myFooCallback(foo);
}
unsafeWindow.myFooCallback(foo){
// this will be called with `foo` after `fooFunction` is ran.
}
But, this is kinda pointless as your injecting code into fooFunction
anyway.
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 foo
inside fooFunction
?
如果您要在过程中跟踪值,我建议使用Firebug调试此类型的脚本,只需在脚本中所需的任何位置放置一个断点,并在其停止时将为您提供当前值关闭所有正在使用的变量。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.