【发布时间】:2015-01-11 10:58:40
【问题描述】:
我创建了一个 Greasemonkey 脚本,其中包含一个我想从页面的内部脚本访问的对象。
为此,this page 中描述了几种安全方法。
其中之一是使用Components.utils.cloneInto 函数。
这是一个脚本示例。
// ==UserScript==
// @name Test CloneInto
// @namespace Test CloneInto
// @description Test CloneInto
// @include http://stackoverflow.com*
// @version 1
// @grant GM_xmlhttpRequest
// ==/UserScript==
var myObj = {}
myObj.test = function() {
alert("works");
}
myObj.num = 152;
unsafeWindow.myObj = cloneInto(myObj, unsafeWindow, {cloneFunctions: true});
var scriptDOM = document.createElement("script");
scriptDOM.type = "text/javascript";
scriptDOM.innerHTML = "alert(window.myObj.num);\
alert(window.myObj.test);"
document.getElementsByTagName("head")[0].appendChild(scriptDOM);
“152”显示正确,但随后出现“未定义”而不是我的函数。
但是我按照文档的建议使用了{cloneFunctions: true}。
我使用的是 Firefox 34,请问有人有办法解决这个问题吗?
编辑:使用 Google Chrome 和 Tampermonkey 运行良好。
编辑 2:打开 issue 2070 on the Greasemonkey repository。
【问题讨论】:
-
看起来这可能是 Greasemonkey 本身的一个错误。尚未确认。
-
@BrockAdams 实际上,我不知道它是来自 GreaseMonkey 还是来自 Firefox。我在 Greasemonkey Github 上开了一个 issue,会看到的。
标签: javascript firefox firefox-addon greasemonkey userscripts