【问题标题】:How to inject script when contentaccessible=yes fails当 contentaccessible=yes 失败时如何注入脚本
【发布时间】:2015-08-15 22:27:43
【问题描述】:

我必须在网页中收听 jquery 事件。我无法从 WrappedJSObject 附加函数,因为它在执行 chrome 范围内创建的函数时遇到问题。

所以我想动态插入一个脚本,该脚本将调度一个自定义事件,我将让我的插件监听该事件。但这是一个简单的案例,只需插入警报脚本:

var aContentWindow = gBrowser.contentWindow;
var aContentDocument = aContentWindow.document;

var myScript = aContentDocument.createElement('script');
myScript.textContent = 'alert("registered");'

aContentDocument.documentElement.appendChild(myScript);

所以我尝试运行它的网站是 twitter,它可以在其他网站上运行。

所以我想让我们从带有 contentaccessible=true 的 chrome 路径的插件范围执行此操作,所以我这样做了,但它仍然会给我错误:

内容安全策略:页面的设置阻止在自身加载资源(“script-src 'unsafe-inline' 'nonce-hnhqgKrLIxRgkHGdVqfgfA==' 'unsafe-eval' https://twitter.com https://*.twimg .com https://twitter.comhttps://ton.twitter.comhttps://platform.twitter.comhttps://syndication.twitter.comhttps://analytics.twitter.comhttps://www.google-analytics.comhttps://ssl.google-analytics.comhttps://connect.facebook.nethttps://cm.g.doubleclick.nethttps://api.twitter.com@987654332"@987654332"@9 twitter.com:562:0

同时还有第二个:

内容安全策略:页面的设置阻止在自身加载资源(“script-src 'unsafe-inline' 'nonce-hnhqgKrLIxRgkHGdVqfgfA==' 'unsafe-eval' https://twitter.com https://*.twimg .com https://twitter.comhttps://ton.twitter.comhttps://platform.twitter.comhttps://syndication.twitter.comhttps://analytics.twitter.comhttps://www.google-analytics.comhttps://ssl.google-analytics.comhttps://connect.facebook.nethttps://cm.g.doubleclick.nethttps://api.twitter.com@98765434@5)@98765434@5)@9

任何想法如何让这个脚本在那里?

【问题讨论】:

    标签: firefox-addon


    【解决方案1】:

    您是否尝试过subscript loader,将 WrappedJSObject 作为目标上下文,而不是操作 DOM?


    您可以尝试的另一件事是创建a sandbox,将目标窗口指定为sandboxPrototype,将wantXrays 设置为false,并使用目标窗口的document.domain 作为安全主体。

    这应该会给你一个沙盒,它可以完全访问目标窗口,它不应该引发任何安全错误,因为它将使用transparent wrappers

    拥有沙箱后,您可以尝试通过evalInSandbox 在沙箱内运行脚本,或使用下标加载器,将沙箱作为目标而不是窗口。

    【讨论】:

    • 我对此进行了测试,但无法使其正常工作,还有其他想法吗? :(
    • 非常感谢!我真的很想尽快尝试一下,这是我从未探索过的东西!
    猜你喜欢
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2016-06-30
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多