Bug 1234677 - Overwrite alert at window init instead of onload draft
authorRob Wu <rob@robwu.nl>
Wed, 20 Jul 2016 01:00:29 -0700
changeset 389850 61d5b991530c693efed1738e5913016facc49a9f
parent 386302 214884d507ee369c1cf14edb26527c4f9a97bf48
child 389851 0f7bd0037aabcb597d4647a65ecf1eccc7907293
push id23548
push userbmo:rob@robwu.nl
push dateWed, 20 Jul 2016 08:03:07 +0000
bugs1234677
milestone50.0a1
Bug 1234677 - Overwrite alert at window init instead of onload A nice extra effect of this is that it will also work after reloading the background page (location.reload()) and it should also work for child frames in background pages. MozReview-Commit-ID: ErvpzD8d3ow
toolkit/components/extensions/Extension.jsm
toolkit/components/extensions/ext-backgroundPage.js
--- a/toolkit/components/extensions/Extension.jsm
+++ b/toolkit/components/extensions/Extension.jsm
@@ -655,27 +655,58 @@ GlobalManager = {
 
 
     let extension = this.extensionMap.get(id);
     let uri = contentWindow.document.documentURIObject;
     let incognito = PrivateBrowsingUtils.isContentWindowPrivate(contentWindow);
 
     let context = new ExtensionContext(extension, {type, contentWindow, uri, docShell, incognito});
     inject(extension, context);
+    if (type == "background") {
+      this._initializeBackgroundPage(contentWindow);
+    }
 
     let eventHandler = docShell.chromeEventHandler;
     let listener = event => {
       if (event.target != docShell.contentViewer.DOMDocument) {
         return;
       }
       eventHandler.removeEventListener("unload", listener, true);
       context.unload();
     };
     eventHandler.addEventListener("unload", listener, true);
   },
+
+  _initializeBackgroundPage(contentWindow) {
+    // Override the `alert()` method inside background windows;
+    // we alias it to console.log().
+    // See: https://bugzilla.mozilla.org/show_bug.cgi?id=1203394
+    let alertDisplayedWarning = false;
+    let alertOverwrite = text => {
+      if (!alertDisplayedWarning) {
+        let consoleWindow = Services.wm.getMostRecentWindow("devtools:webconsole");
+        if (!consoleWindow) {
+          let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
+          require("devtools/client/framework/devtools-browser");
+          let hudservice = require("devtools/client/webconsole/hudservice");
+          hudservice.toggleBrowserConsole().catch(Cu.reportError);
+        } else {
+          // the Browser Console was already open
+          consoleWindow.focus();
+        }
+
+        contentWindow.console.warn("alert() is not supported in background windows; please use console.log instead.");
+
+        alertDisplayedWarning = true;
+      }
+
+      contentWindow.console.log(text);
+    };
+    Cu.exportFunction(alertOverwrite, contentWindow, {defineAs: "alert"});
+  },
 };
 
 // All moz-extension URIs use a machine-specific UUID rather than the
 // extension's own ID in the host component. This makes it more
 // difficult for web pages to detect whether a user has a given add-on
 // installed (by trying to load a moz-extension URI referring to a
 // web_accessible_resource from the extension). getExtensionUUID
 // returns the UUID for a given add-on ID.
--- a/toolkit/components/extensions/ext-backgroundPage.js
+++ b/toolkit/components/extensions/ext-backgroundPage.js
@@ -65,44 +65,19 @@ BackgroundPage.prototype = {
 
     if (this.extension.addonData.instanceID) {
       AddonManager.getAddonByInstanceID(this.extension.addonData.instanceID)
                   .then(addon => addon.setDebugGlobal(window));
     }
 
     // TODO: Right now we run onStartup after the background page
     // finishes. See if this is what Chrome does.
+    // TODO(robwu): This implementation of onStartup is wrong, see
+    // https://bugzilla.mozilla.org/show_bug.cgi?id=1247435#c1
     let loadListener = event => {
-      // Override the `alert()` method inside background windows;
-      // we alias it to console.log().
-      // See: https://bugzilla.mozilla.org/show_bug.cgi?id=1203394
-      let alertDisplayedWarning = false;
-      let alertOverwrite = text => {
-        if (!alertDisplayedWarning) {
-          let consoleWindow = Services.wm.getMostRecentWindow("devtools:webconsole");
-          if (!consoleWindow) {
-            let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
-            require("devtools/client/framework/devtools-browser");
-            let hudservice = require("devtools/client/webconsole/hudservice");
-            hudservice.toggleBrowserConsole().catch(Cu.reportError);
-          } else {
-            // the Browser Console was already open
-            consoleWindow.focus();
-          }
-
-          this.contentWindow.console.warn("alert() is not supported in background windows; please use console.log instead.");
-
-          alertDisplayedWarning = true;
-        }
-
-        window.console.log(text);
-      };
-      Components.utils.exportFunction(alertOverwrite, window, {
-        defineAs: "alert",
-      });
       if (event.target != window.document) {
         return;
       }
       event.currentTarget.removeEventListener("load", loadListener, true);
 
       if (this.scripts) {
         let doc = window.document;
         for (let script of this.scripts) {