1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

hopefully this fixes #749

This commit is contained in:
gorhill 2015-11-03 16:40:22 -05:00
parent 8612c54b6c
commit 6bec4d795c

View File

@ -31,7 +31,7 @@ const {classes: Cc, interfaces: Ci} = Components;
// Accessing the context of the background page: // Accessing the context of the background page:
// var win = Services.appShell.hiddenDOMWindow.document.querySelector('iframe[src*=ublock0]').contentWindow; // var win = Services.appShell.hiddenDOMWindow.document.querySelector('iframe[src*=ublock0]').contentWindow;
let bgProcess; let bgProcess = null;
let version; let version;
const hostName = 'ublock0'; const hostName = 'ublock0';
const restartListener = { const restartListener = {
@ -53,23 +53,25 @@ function startup(data/*, reason*/) {
version = data.version; version = data.version;
} }
// Already started?
if ( bgProcess !== null ) {
return;
}
let appShell = Cc['@mozilla.org/appshell/appShellService;1'] let appShell = Cc['@mozilla.org/appshell/appShellService;1']
.getService(Ci.nsIAppShellService); .getService(Ci.nsIAppShellService);
let onReady = function(e) { let isReady = function() {
if ( e ) { var hiddenDoc = null;
this.removeEventListener(e.type, onReady);
try {
hiddenDoc = appShell.hiddenDOMWindow &&
appShell.hiddenDOMWindow.document;
} catch (ex) {
} }
let hiddenDoc = appShell.hiddenDOMWindow.document; if ( hiddenDoc === null || hiddenDoc.readyState === 'loading' ) {
return false;
// https://github.com/gorhill/uBlock/issues/10
// 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);
return;
} }
bgProcess = hiddenDoc.documentElement.appendChild( bgProcess = hiddenDoc.documentElement.appendChild(
@ -84,38 +86,42 @@ function startup(data/*, reason*/) {
hostName + '-restart', hostName + '-restart',
restartListener restartListener
); );
return true;
}; };
var ready = false; if ( isReady() ) {
try {
ready = appShell.hiddenDOMWindow &&
appShell.hiddenDOMWindow.document;
} catch (ex) {
}
if ( ready ) {
onReady();
return; return;
} }
let ww = Cc['@mozilla.org/embedcomp/window-watcher;1'] // https://github.com/gorhill/uBlock/issues/749
.getService(Ci.nsIWindowWatcher); // Poll until the proper environment is set up -- or give up eventually.
ww.registerNotification({ let tryCount = 300;
observe: function(win, topic) { let timer = Cc['@mozilla.org/timer;1']
if ( topic !== 'domwindowopened' ) { .createInstance(Ci.nsITimer);
return;
}
try { let checkLater = function() {
void appShell.hiddenDOMWindow; tryCount -= 1;
} catch (ex) { if ( tryCount > 0 ) {
return; timer.init(timerObserver, 100, timer.TYPE_ONE_SHOT);
} } else {
timer = null;
ww.unregisterNotification(this);
win.addEventListener('DOMContentLoaded', onReady);
} }
}); };
var timerObserver = {
observe: function() {
timer.cancel();
if ( isReady() ) {
timer = null;
} else {
checkLater();
}
}
};
checkLater();
} }
/******************************************************************************/ /******************************************************************************/
@ -125,7 +131,10 @@ function shutdown(data, reason) {
return; return;
} }
bgProcess.parentNode.removeChild(bgProcess); if ( bgProcess !== null ) {
bgProcess.parentNode.removeChild(bgProcess);
bgProcess = null;
}
if ( data === undefined ) { if ( data === undefined ) {
return; return;