bug 1203394: alias alert() to console.log() in background scripts r?kmag draft
authortofumatt <tofumatt@mozilla.com>
Mon, 14 Mar 2016 17:26:32 +0100
changeset 339979 c2c18fbf014c81d939e3fbb525c3b15f3979c240
parent 339874 f0c0480732d36153e8839c7f17394d45f679f87d
child 340527 8a81d73e9e5106be2604a1cb497b71dd4ab8f7b5
child 340529 e83bc395704c4062aecd74caa9193593a8d135cb
child 340987 1f44005dce4ae610576ac39287ff4ea4566f0ce7
push id12860
push userbmo:tofumatt@mozilla.com
push dateMon, 14 Mar 2016 16:36:00 +0000
reviewerskmag
bugs1203394
milestone48.0a1
bug 1203394: alias alert() to console.log() in background scripts r?kmag MozReview-Commit-ID: 65m3yVGQhlQ
toolkit/components/extensions/ext-backgroundPage.js
toolkit/components/extensions/test/mochitest/mochitest.ini
--- a/toolkit/components/extensions/ext-backgroundPage.js
+++ b/toolkit/components/extensions/ext-backgroundPage.js
@@ -63,16 +63,44 @@ BackgroundPage.prototype = {
 
     let window = webNav.document.defaultView;
     this.contentWindow = window;
     this.context.contentWindow = window;
 
     // TODO: Right now we run onStartup after the background page
     // finishes. See if this is what Chrome does.
     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");
+            let { console } = Cu.import("resource://gre/modules/Console.jsm", {});
+            hudservice.toggleBrowserConsole().then(null, console.error);
+          } else {
+            // the Browser Console was already open
+            consoleWindow.focus();
+          }
+
+          this.context.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, this.contentWindow, {
+        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) {
--- a/toolkit/components/extensions/test/mochitest/mochitest.ini
+++ b/toolkit/components/extensions/test/mochitest/mochitest.ini
@@ -61,16 +61,17 @@ skip-if = buildapp == 'b2g' # sender.tab
 [test_ext_cookies_permissions.html]
 skip-if = e10s || buildapp == 'b2g' # Uses cookie service via SpecialPowers.Services, which does not support e10s.
 [test_ext_bookmarks.html]
 skip-if = buildapp == 'b2g' # unimplemented api.
 [test_ext_alarms.html]
 [test_ext_background_window_properties.html]
 [test_ext_background_sub_windows.html]
 [test_ext_background_api_injection.html]
+[test_ext_background_page.html]
 [test_ext_jsversion.html]
 skip-if = e10s || buildapp == 'b2g' # Uses a console monitor which doesn't work from a content process. The code being tested doesn't run in a tab content process in any case.
 [test_ext_i18n.html]
 [test_ext_web_accessible_resources.html]
 [test_ext_webrequest.html]
 skip-if = buildapp == 'b2g' # webrequest api uninplemented (bug 1199504)
 [test_ext_webnavigation.html]
 skip-if = buildapp == 'b2g' # needs TabManager which is not yet implemented