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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Good solution, thanks, but seems anyone (from other extensions) can change location.href of hidden window and my code will be interrupted.
@KAdot: Theoretically - yes. Practically - doing that would be pretty disruptive, no extension does that.
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...
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?
If nsIAppShellService.hiddenDOMWindow fails then one fallback option would be to register a listener 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.