开发者

using eval.call() in firefox XPCOM component

开发者 https://www.devze.com 2023-01-06 23:03 出处:网络
I\'m making an extension that on load of every page creates an instance of my xpcom component specifically for that page.

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 = f开发者_开发技巧unction(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..


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);
  }

}

0

精彩评论

暂无评论...
验证码 换一张
取 消