JQuery可以被多进程Firefox扩展使用吗?

时间:2016-03-25 15:20:48

标签: jquery firefox firefox-addon overlays e10s

Mozilla文档对此问题保持沉默。有没有人能够回答并解释为什么或为什么不解释?如果没有,我想了解政策原因和架构决策,为什么不这样做。

编辑:此问题仅限于无法使用附加SDK的扩展程序,而是使用传统的叠加模式。

2 个答案:

答案 0 :(得分:1)

Framescripts are not webpages并且不提供对jquery期望存在的大多数全局变量的访问,例如XHR,文档和窗口本身等。

即使您以一种看起来像窗口环境的方式操纵变量,这仍然会有很大的问题,因为帧脚本的生命周期超出了DOM窗口的生命周期,即它的存在与标签相关联,而不是到标签的各个页面。 Jquery只能在页面生效时使用。

第三个问题是安全性,framecripts使用chrome/system privileges运行,如果你直接从框架脚本运行它,那么jquery也是如此。 Jquery的设计并不具备安全意识,因为它通常受到网站同源策略的约束。因此,事件处理和XHR之间的一些复杂交互可能会打开安全漏洞。

因此不建议在浏览器内部脚本环境中使用jquery。

从框架脚本执行DOM操作的两个选项是

a)直接从框架脚本使用标准DOM API。 Addon脚本在启用ES6支持的情况下自动运行(例如,解构,箭头功能等),并且不必担心跨浏览器兼容性。换句话说:不需要jquery

b)如果绝对需要使用jquery,例如因为某些第三方库依赖于它,所以可以使用当前窗口作为其原型创建sandbox并使用subscript loader将jquery和自定义脚本注入其中。

建议创建沙箱以将其与不受信任的内容隔离并同时删除系统权限的方法:

let options = {
  // this is the name reported in about:memory
  sandboxName: "<addon name> <purpose of sandbox>",

  // ensure that jquery sees the window as global
  sandboxPrototype: content,

  // reduces GC overhead by having the sandbox reside in the same space as target window
  sameZoneAs: content,

  // don't include components object that grants access to privileged APIs
  wantComponents: false, 

  // helper functions for interacting with untrusted content
  wantExportHelpers: true,

  // clean view of DOM APIs, otherwise untrusted content could override prototypes
  wantXrays: true,

  // addon ID, used by addon debugger and memory reporting
  // sdk addons can obtain it via require("sdk/self").id, other addons define it in the install.rdf
  metadata: {addonID: id}
}

// set the security principal to an extended principal covering the target window
let sandbox = Cu.Sandbox([content], options)

// structured-clone objects into the sandbox
sandbox.myData = {foo: "bar"}

loader.loadSubscript("resource://myaddon-id/libs/jquery.js", sandbox, "UTF-8")
loader.loadSubscript("resource://myaddon-id/src/mypagescript.js", sandbox, "UTF-8")

// call custom function created by mypagescript.js
sandbox.myFunc()

请注意,沙箱仅在页面的生命周期内有效,因此如果框架导航到新窗口(content对象),则必须创建新的沙箱

以上基本上是SDK page-mod和webextensions content-scripts使用的基础低级API。

答案 1 :(得分:0)

请参阅我对您的OP的评论。您阅读的文档与内容脚本无关。这是关于framecripts和其他高架范围。 Google Chrome没有这些提升的范围。他们只有内容脚本。这就是为什么我们都感到困惑。

这是您使用jpm addon sdk在内容脚本中使用jQuery的方法。

将jquery lib下载到您的数据文件夹中。

var tabs = require("sdk/tabs");
var mod = require("sdk/page-mod");
var self = require("sdk/self");

var pageUrl = self.data.url("page.html")

var pageMod = mod.PageMod({
  include: '*',
  contentScript: [self.data.url('jquery.min.js'), "console.log(jQuery);"]
})

这会将jQuery插入到所有网站中。 tabs.open(PAGEURL);

如果你使用webextensions的方式与google chrome相同:https://developer.mozilla.org/en-US/Add-ons/WebExtensions/