From 09abdc1b9f6a267daa35e4bb0638cb64417ddef2 Mon Sep 17 00:00:00 2001 From: gorhill Date: Sat, 1 Apr 2017 19:47:31 -0400 Subject: [PATCH] fix https://github.com/gorhill/uBlock/issues/2502#issuecomment-290941686 --- platform/chromium/manifest.json | 2 +- platform/firefox/bootstrap.js | 66 ++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/platform/chromium/manifest.json b/platform/chromium/manifest.json index 568ccd0..fbde9eb 100644 --- a/platform/chromium/manifest.json +++ b/platform/chromium/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "uMatrix", "short_name": "uMatrix", - "version": "0.9.9.10", + "version": "0.9.9.11", "description": "__MSG_extShortDesc__", "icons": { "16": "img/icon_16.png", diff --git a/platform/firefox/bootstrap.js b/platform/firefox/bootstrap.js index 44644cc..1446102 100644 --- a/platform/firefox/bootstrap.js +++ b/platform/firefox/bootstrap.js @@ -64,16 +64,7 @@ function startup(data/*, reason*/) { return; } - let appShell = Cc['@mozilla.org/appshell/appShellService;1'] - .getService(Ci.nsIAppShellService); - - if ( appShell.createWindowlessBrowser ) { - getWindowlessBrowserFrame(appShell); - } else { - getHiddenWindowBrowserFrame(appShell); - } - - + waitForHiddenWindow(); } function createBgProcess(parentDocument) { @@ -118,7 +109,10 @@ function getWindowlessBrowserFrame(appShell) { windowlessBrowser.document.location = "data:application/vnd.mozilla.xul+xml;charset=utf-8,"; } -function getHiddenWindowBrowserFrame(appShell) { +function waitForHiddenWindow() { + let appShell = Cc['@mozilla.org/appshell/appShellService;1'] + .getService(Ci.nsIAppShellService); + let onReady = function(e) { if ( e ) { this.removeEventListener(e.type, onReady); @@ -130,13 +124,57 @@ function getHiddenWindowBrowserFrame(appShell) { // Fixed by github.com/AlexVallat: // https://github.com/chrisaljoudi/uBlock/issues/1149 // https://github.com/AlexVallat/uBlock/commit/e762a29d308caa46578cdc34a9be92c4ad5ecdd0 - if ( hiddenDoc.readyState === 'loading' ) { - hiddenDoc.addEventListener('DOMContentLoaded', onReady); + if ( !hiddenDoc || hiddenDoc.readyState === 'loading' ) { + appShell.hiddenDOMWindow.addEventListener('DOMContentLoaded', onReady); return; } - createBgProcess(hiddenDoc); + // Fix from https://github.com/gijsk, taken from: + // - https://github.com/gorhill/uBlock/commit/53a794d9b2a8c65406ee7a201cacbc91c297b2f8 + // + // In theory, it should be possible to create a windowless browser + // immediately, without waiting for the hidden window to have loaded + // completely. However, in practice, on Windows this seems to lead + // to a broken Firefox appearance. To avoid this, we only create the + // windowless browser here. We'll use that rather than the hidden + // window for the actual background page (windowless browsers are + // also what the webextension implementation in Firefox uses for + // background pages). + if ( appShell.createWindowlessBrowser ) { + getWindowlessBrowserFrame(appShell); + } else { + createBgProcess(hiddenDoc); + } }; + + var ready = false; + try { + ready = appShell.hiddenDOMWindow && + appShell.hiddenDOMWindow.document; + } catch (ex) { + } + if ( ready ) { + onReady(); + return; + } + + let ww = Components.classes['@mozilla.org/embedcomp/window-watcher;1'] + .getService(Components.interfaces.nsIWindowWatcher); + + ww.registerNotification({ + observe: function(win, topic) { + if ( topic !== 'domwindowopened' ) { + return; + } + try { + void appShell.hiddenDOMWindow; + } catch (ex) { + return; + } + ww.unregisterNotification(this); + onReady(); + } + }); } /******************************************************************************/