【问题标题】:Firefox CloneInto does not copy function from add-on script to page scriptFirefox CloneInto 不会将功能从附加脚本复制到页面脚本
【发布时间】: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


【解决方案1】:

为此,有几种安全的方法described on this page.

这是描述对附加 SDK 内容脚本的更改。

更改与 Greasemonkey 或用户脚本无关,Addon-sdk 内容脚本与 Greasemonkey 用户脚本不同。

对于 Greasemonkey,我认为这仍然有效:

unsafeWindow.myObj = myObj

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-24
    • 2017-09-02
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 2013-04-05
    相关资源
    最近更新 更多