How to inject some XUL-elements to browser windows in restart-less addon **WITHOUT** Jetpack
See also: How to create restart-less #Firefox4 extension WITHOUT Jetpack
XUL-overlay is unusable for restart-less extensions.
If you want to inject some XUL-elements (and scripts) to Firefox browser window, you need to write some codes in bootstrap.js
.
1. Browser observer
When a browser window is opened, I want to inject some XUL-elements(, scripts, and so on) to the browser window. When a browser window is closed, everything injected shall be removed.
BrowserWindowObserver
class I wrote do the following things:
- When a browser window is opened, a function
handlers.onStartup(aWindow)
is called. - When a browser window is closed, a function
handlers.onShutdown(aWindow)
is called. handler
shall be set in the constructor.
Source code:
Note that:
- Firefox browser windows' type is
"navigator:browser"
. - When
observe()
is called withaTopic
="domwindowopened"
, the DOM tree is not generated and the browser type is not specified. So, an event listener is required so to wait for"DOMContentLoaded"
(refer to following sequence diagram.)
2. startup()
When the add-on is activated, ...
- create and register a new instance of
BrowserWindowObserver
; and - call a startup function for each the opened browser windows.
Source code:
Note that:
- Register a browser window observer to
nsIWindowWatcher
service. - Use
nsIWindowMediator
service for enumerating browser windows.
3. shutdown()
When the add-on is deactivated, ...
- unregister the browser window observer; and
- call a shutdown function for each the browser windows.
Source code:
4. inject and remove
Injection and Removal of XUL-elements is straight forward!
Source code:
References
- How to create restart-less #Firefox4 extension WITHOUT Jetpack - My Blog Post
- Bootstrapped extensions - MDN Docs
- nsIWindowWatcher - MDN XPCOM Interface Reference
- nsIWindowMediator - MDN XPCOM Interface Reference
- DOMContentLoaded (Gecko-Specific DOM Events) - MDN Docs