javascript:通过GM脚本调用嵌入式功能 [英] javascript: call an embedded function from a GM script
问题描述
在网页上有
< script>
函数fn982734()
{
//一些代码
}
< / script>
在我的Greasemonkey脚本中,我有以下代码:
var fn = fields [5] .getElementsByTagName(a)[0] .getAttribute('onclick')。substr(7,11);
console.log(fn); //输出fn982734到萤幕控制台
window [fn]();
此代码无效,并在错误控制台中产生错误:window [fn]不是一个函数。然而,直接输入到firebug中:
var fn ='fn982734';
window [fn]();
完美无缺。发生了什么事?
Greasemonkey脚本位于沙盒内,而Firebug不在。 您的GM脚本可以通过 。 同样适用于Greasemonkey脚本。 On a webpage there's In my Greasemonkey script, I have the following code: This code does not work, and spawns an error in the error console: window[fn] is not a function. However, typing directly into firebug: works perfectly. What's going on? The Greasemonkey script is inside a sandbox and Firebug is not.
See: "Avoid Common Pitfalls" (in Greasemonkey). Your GM script would access that function via . Also works -- from inside the Greasemonkey script. 这篇关于javascript:通过GM脚本调用嵌入式功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
请参阅:避免常见陷阱(在Greasemonkey中)
unsafeWindow
访问该函数。像这样:
unsafeWindow.fn982734();
另外,
var fn ='fn982734';
unsafeWindow [fn]();
<script>
function fn982734()
{
// some code
}
</script>
var fn = fields[5].getElementsByTagName("a")[0].getAttribute('onclick').substr(7,11);
console.log(fn); // outputs fn982734 to the firebug console
window[fn]();
var fn = 'fn982734';
window[fn]();
unsafeWindow
. Like so:unsafeWindow.fn982734();
Alternatively, var fn = 'fn982734';
unsafeWindow[fn]();