Bug 1234677 - Overwrite alert at window init instead of onload r=billm
authorRob Wu <rob@robwu.nl>
Wed, 20 Jul 2016 01:00:29 -0700
changeset 306046 58a8eeabbe7f4cb01e895c90bb6a16df01339e4a
parent 306045 d05aa8ecdb0b184bc22a0efaaf006c26c46401de
child 306047 36b524e69dc5250a87d8f6c42a369c817d508f39
push id79765
push usercbook@mozilla.com
push dateThu, 21 Jul 2016 14:26:34 +0000
treeherdermozilla-inbound@ab54bfc55266 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbillm
bugs1234677
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1234677 - Overwrite alert at window init instead of onload r=billm 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) {