0

我需要使用 WebExtensions 在 Firefox 插件中创建一个隐藏的 iframe。

这就是我正在尝试做的事情(如此处所建议: Load multiple pages in a hidden iframe from a xul-based firefox extension):

var hiddenWindow = Components.classes["@mozilla.org/appshell/appShellService;1"].
                   getService(Components.interfaces.nsIAppShellService).
                   hiddenDOMWindow;

我收到错误“Components.classes 未定义”,似乎无法解决。

帮助表示赞赏。谢谢你。

4

1 回答 1

0

不要这样做。在 WEAPI 中这甚至是不可能的,你不能在那个范围内运行这样的代码。

WEAPI

如果你使用 webextensions 使用背景页面,你不必担心这个 - https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Anatomy_of_a_WebExtension#Background_scripts

引导程序

在引导程序中,您可以这样做,但不要这样做。在引导程序中使用 WEAPI 使用的相同技术,即自 Firefox 23 起支持的无窗口浏览器 -

 var webnav = Services.appShell.createWindowlessBrowser(true);
 var docshell = webnav.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDocShell);
 var systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal);
 docshell.createAboutBlankContentViewer(systemPrincipal);
 var contentWindow = docshell.contentViewer.DOMDocument.defaultView;

 // when you are done with it, destroy it
 if (webnav.close) { webnav.close() }; // only available in Firefox 46+, and is needed for good measure
 webnav = null; // in Firefox <= 45 setting to null will cause it to get GC'ed which will destroy it

这是另一个示例,这是 webextensions 如何使用上面的代码:

let chromeWebNav = Services.appShell.createWindowlessBrowser(true);
this.chromeWebNav = chromeWebNav;

let url;
if (this.page) {
  url = this.extension.baseURI.resolve(this.page);
} else {
  // TODO: Chrome uses "_generated_background_page.html" for this.
  url = this.extension.baseURI.resolve("_blank.html");
}

if (!this.extension.isExtensionURL(url)) {
  this.extension.manifestError("Background page must be a file within the extension");
  url = this.extension.baseURI.resolve("_blank.html");
}

let system = Services.scriptSecurityManager.getSystemPrincipal();

let chromeShell = chromeWebNav.QueryInterface(Ci.nsIInterfaceRequestor)
                              .getInterface(Ci.nsIDocShell);
chromeShell.createAboutBlankContentViewer(system);

let chromeDoc = chromeWebNav.document;
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
let browser = chromeDoc.createElementNS(XUL_NS, "browser");
browser.setAttribute("type", "content");
browser.setAttribute("disableglobalhistory", "true");
browser.setAttribute("webextension-view-type", "background");
chromeDoc.body.appendChild(browser);

let frameLoader = browser.QueryInterface(Ci.nsIFrameLoaderOwner).frameLoader;
let docShell = frameLoader.docShell;

let webNav = docShell.QueryInterface(Ci.nsIWebNavigation);
this.webNav = webNav;

webNav.loadURI(url, 0, null, null, null);

let window = webNav.document.defaultView;
this.contentWindow = window;

https://dxr.mozilla.org/mozilla-central/source/toolkit/components/extensions/ext-backgroundPage.js#25-64

于 2016-06-14T12:11:01.677 回答