var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
function install(data, reason) {
function uninstall(data, reason) {
function startup(data, reason) {
// Check if the window we want to modify is already open.
let windows = Services.wm.getEnumerator("mail:3pane");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
WindowListener.loadIntoWindow(domWindow);
// Wait for any new windows to open.
Services.wm.addListener(WindowListener);
function shutdown(data, reason) {
// When the application is shutting down we normally don't have to clean
// up any UI changes made.
if (reason == APP_SHUTDOWN) {
let windows = Services.wm.getEnumerator("mail:3pane");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
WindowListener.unloadFromWindow(domWindow);
// Stop listening for any new windows to open.
Services.wm.removeListener(WindowListener);
async loadIntoWindow(window) {
console.log("load (1/2): " + window.document.readyState);
if (window.document.readyState != "complete") {
// Make sure the window load has completed.
await new Promise(resolve => {
window.addEventListener("load", resolve, { once: true });
this.loadIntoWindowAfterWindowIsReady(window);
loadIntoWindowAfterWindowIsReady(window) {
console.log("load (2/2): " + window.document.readyState);
// Check if the opened window is the one we want to modify.
if (window.document.documentElement
.getAttribute("windowtype") === "mail:3pane") {
let document = window.document;
// Take any steps to add UI or anything to the window
// document.getElementById() etc. will work here.
unloadFromWindow(window) {
console.log("unload: " + window.document.readyState);
let document = window.document;
// Take any steps to remove UI or anything from the window
// document.getElementById() etc. will work here.
// nsIWindowMediatorListener functions
onOpenWindow(xulWindow) {
// A new window has opened.
let domWindow = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
this.loadIntoWindow(domWindow);
onCloseWindow(xulWindow) {
onWindowTitleChange(xulWindow, newTitle) {