在 Firefox XPCOM 组件中使用 eval.call()

发布于 2024-09-10 00:26:43 字数 654 浏览 11 评论 0 原文

我正在制作一个扩展,在加载每个页面时都会专门为该页面创建 xpcom 组件的实例。

我这样做:

var appcontent = document.getElementById("appcontent");   // browser
if(appcontent) {
   appcontent.addEventListener("load", onPageLoad, true);
}
var onPageLoad = function(aEvent) {
   var doc = aEvent.originalTarget; //this is the reference to the opened page
   pages.push(createInstanceOfMyXPCOM(doc));
}

我的问题是,在 XPCOM 组件中,如何在该文档的全局上下文中使用 eval() 。如果你只是在 html 中的常规 javascript 中执行此操作,你可以这样做:

window.eval.call(window, somecode);

问题是我的 xpcom 组件中没有 window 变量(或者我有),我只有对文档的引用。我也可以在创建时将窗口传递给我的 XPCOM 组件,但如果我打开了多个页面,我不知道这是如何工作的。

I'm making an extension that on load of every page creates an instance of my xpcom component specifically for that page.

I do that like this:

var appcontent = document.getElementById("appcontent");   // browser
if(appcontent) {
   appcontent.addEventListener("load", onPageLoad, true);
}
var onPageLoad = function(aEvent) {
   var doc = aEvent.originalTarget; //this is the reference to the opened page
   pages.push(createInstanceOfMyXPCOM(doc));
}

My question is, within the XPCOM component, how can I use eval() within the global context of that doc. If you would just do this in a regular javascript in html you could do:

window.eval.call(window, somecode);

The problem is I don't have window variable in my xpcom component (or do I), I only have the reference to the document. I could pass the window to my XPCOM component on creation as well, but if I have several pages opened, I don't see how that would work..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小草泠泠 2024-09-17 00:26:43

从 XPCOM 中,您应该能够使用以下方法获取主窗口的引用:

var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                       .getInterface(Components.interfaces.nsIWebNavigation)
                       .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                       .rootTreeItem
                       .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                       .getInterface(Components.interfaces.nsIDOMWindow);

然后您可以使用以下方法获取当前选定的选项卡文档:

document = mainWindow.gBrowser.contentDocument;

您可以在此处找到更多信息:

https://developer.mozilla.org/en/Code_snippets/Tabbed_browser

https://developer.mozilla.org/en/Working_with_windows_in_chrome_code

更新:

试试这个,您应该能够获得最新版本的参考窗口:

var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                   .getService(Components.interfaces.nsIWindowMediator);
var recentWindow = wm.getMostRecentWindow("navigator:browser");

如果您有多个选项卡,则可以使用类似的内容(来自 Mozilla 开发站点的代码)来迭代所有选项卡并访问每个文档:

var num = gBrowser.browsers.length;
for (var i = 0; i < num; i++) {
  var b = gBrowser.getBrowserAtIndex(i);
  try {
    dump(b.currentURI.spec); // dump URLs of all open tabs to console
  } catch(e) {
    Components.utils.reportError(e);
  }

}

From the XPCOM you should be able to get a reference to the main window using:

var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                       .getInterface(Components.interfaces.nsIWebNavigation)
                       .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                       .rootTreeItem
                       .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                       .getInterface(Components.interfaces.nsIDOMWindow);

Then you can get the currently selected tab document with:

document = mainWindow.gBrowser.contentDocument;

You can fin more information here:

https://developer.mozilla.org/en/Code_snippets/Tabbed_browser

https://developer.mozilla.org/en/Working_with_windows_in_chrome_code

UPDATE:

Try this one, you should be able to get a reference to the most recent window:

var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                   .getService(Components.interfaces.nsIWindowMediator);
var recentWindow = wm.getMostRecentWindow("navigator:browser");

If you have multiple tabs, you use something like this (code from Mozilla Dev Site) to iterate over all of them and access each document:

var num = gBrowser.browsers.length;
for (var i = 0; i < num; i++) {
  var b = gBrowser.getBrowserAtIndex(i);
  try {
    dump(b.currentURI.spec); // dump URLs of all open tabs to console
  } catch(e) {
    Components.utils.reportError(e);
  }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文