javascript:通过GM脚本调用嵌入式功能 [英] javascript: call an embedded function from a GM script

查看:161
本文介绍了javascript:通过GM脚本调用嵌入式功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网页上有

 < 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不在。
请参阅:避免常见陷阱(在Greasemonkey中)


您的GM脚本可以通过 unsafeWindow 访问该函数。像这样:

  unsafeWindow.fn982734(); 



另外,


  var fn ='fn982734'; 
unsafeWindow [fn]();

同样适用于Greasemonkey脚本。


On a webpage there's

<script>
  function fn982734()
  {
     // some code
  }
</script>

In my Greasemonkey script, I have the following code:

var fn = fields[5].getElementsByTagName("a")[0].getAttribute('onclick').substr(7,11);
console.log(fn); // outputs fn982734 to the firebug console
window[fn]();

This code does not work, and spawns an error in the error console: window[fn] is not a function. However, typing directly into firebug:

var fn = 'fn982734';
window[fn]();

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 unsafeWindow. Like so:

unsafeWindow.fn982734();

.
Alternatively,

var fn = 'fn982734';
unsafeWindow[fn]();

Also works -- from inside the Greasemonkey script.

这篇关于javascript:通过GM脚本调用嵌入式功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆