我创建了一个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"显示正确,但然后" undefined"出现而不是我的功能
然而,我按照文档的建议使用了{cloneFunctions: true}
。
我使用的是Firefox 34,有人有想法解决这个问题吗?
编辑:使用谷歌浏览器和Tampermonkey工作正常 修改2 :已打开issue 2070 on the Greasemonkey repository。
答案 0 :(得分:-1)
为此,有几种安全方式described on this page.
这描述了对Add-on SDK内容脚本的更改。
此更改与Greasemonkey或用户脚本无关,Addon-sdk内容脚本与Greasemonkey用户脚本不同。
对于Greasemonkey,我认为这仍然有效:
unsafeWindow.myObj = myObj