2

I need one hidden iframe for all opened Firefox windows.

For now I'm creating iframe inside XUL overlay, so it is created for each browser window.

I think i should use XPCOM component to have single iframe instance for all browser windows, but i can't find way how to create XUL elements from it.

Is it possible?

KAdot
  • 1,997
  • 13
  • 21

1 Answers1

3

You can create a frame inside the hidden window:

var hiddenWindow = Components.classes["@mozilla.org/appshell/appShellService;1"]
                             .getService(Components.interfaces.nsIAppShellService)
                             .hiddenDOMWindow;
var frame = hiddenWindow.document.getElementById("myExtensionFrame");
if (!frame)
{
  var XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  frame = hiddenWindow.document.createElementNS(XUL_NS, "iframe");
  frame.setAttribute("id", "myExtensionFrame");
  frame.setAttribute("src", "...");
  hiddenWindow.document.documentElement.appendChild(frame);
}

However, if all you need is a place to run your global code then there are better ways - like JavaScript code modules.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Good solution, thanks, but seems anyone (from other extensions) can change location.href of hidden window and my code will be interrupted. – KAdot Jun 28 '12 at 12:52
  • 1
    @KAdot: Theoretically - yes. Practically - doing that would be pretty disruptive, no extension does that. – Wladimir Palant Jun 28 '12 at 15:51
  • Each Firefox addon has a lot more control that required, like it can modify the core browser functionality, as well as disrupt other (competitors?) addons... But just because this is possible doesn't mean people does these kind of weird things... So, you can safely assume that no addon changes the hidden windows href... – Sai Prasad Jun 30 '12 at 14:00
  • I'm trying to use hiddenDOMWindow from bootstrapped extension. If i'm trying to access hiddenDOMWindow in "startup" function it cause error "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIAppShellService.hiddenDOMWindow]", but after some delay it works fine. How i can ensure that hiddenDOMWindow is ready and i can use it? – KAdot Nov 08 '12 at 10:54
  • 1
    If `nsIAppShellService.hiddenDOMWindow` fails then one fallback option would be to [register a listener](https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIWindowWatcher#registerNotification%28%29) to get notified when a window is created. The hidden window is always the first window to be opened. You would still have to add an event listener to that window to wait for it to load. – Wladimir Palant Nov 09 '12 at 07:08