在 Firefox 扩展中注入动态脚本
我现在正在做一个扩展,我的脚本的一部分是静态的(永远不会改变),另一部分是从网站加载的。而且,我看到了两种方法:
- 使用 XMLHttpRequest 加载它并注入网页
- 将其作为
并让它自行加载
但是,第二种方式可能无法访问我的扩展 API(扩展文件中定义的函数,即 chrome://myext/script.js
中)
并且,第一种方法可能是不安全,因为我必须评估 gBrowser.contentWindow.wrappedJSObject 对象中的代码,该对象是加载页面的 Window 对象
有什么想法吗?
I'm doing an extension now and i have one part of script which is static (will never change) and another part which is loaded from the website. And, i'm seeing 2 ways:
- To load it with XMLHttpRequest and inject into web page
- To put it as a
<script src="example.com/myscript.js"></script>
and have it load it itself
But, the second way probably won't have access to my extension API (to functions defined in extension files, i.e. in chrome://myext/script.js
)
And, the first way will probably be unsecure because i will have to eval the code in a gBrowser.contentWindow.wrappedJSObject
object which is a Window object for the loaded page
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是说你想让动态脚本有chrome权限吗?如果是这样,为什么不使用 XMLHttpRequest 加载它,将其保存到磁盘,然后将其作为 JavaScript 模块导入 (https://developer.mozilla.org/en/JavaScript_code_modules/Using)。显然,存在安全考虑,因为您为来自网络的脚本提供了几乎无限的特权,但如果您控制脚本的源,那么您可能没问题。如果您真的担心,可以使用 HTTPS 下载脚本,这将防止有人拦截流量。
如果您希望代码以内容权限运行,但可以访问 chrome JavaScript 中的函数,那么您可能希望将 chrome 函数公开给内容,如本文所述:http://weblogs.mozillazine.org/weirdal/archives/017188.html
Are you saying that you want the dynamic script to have chrome privileges? If so, why not load it using XMLHttpRequest, save it to disk and then import it as a JavaScript Module (https://developer.mozilla.org/en/JavaScript_code_modules/Using). Obviously there are security considerations since you are giving a script from the web pretty much unlimited privileged, but if you control the script's source then you are presumably okay. If you are really worried you can use HTTPS to download the script, which will protect against someone intercepting the traffic.
If you want the code to run with content privileges but have access to functions in your chrome JavaScript, then maybe you want to expose the chrome functions to content as described in this article: http://weblogs.mozillazine.org/weirdal/archives/017188.html