diff --git a/js/contentscript-start.js b/js/contentscript-start.js index 2928d6202..3f1e58842 100644 --- a/js/contentscript-start.js +++ b/js/contentscript-start.js @@ -56,7 +56,7 @@ var uBlockMessaging = (function(name){ return; } var callback = requestIdToCallbackMap[details.id]; - if ( !callback ) { + if ( callback === undefined ) { return; } // Must be removed before calling client to be sure to not execute @@ -117,7 +117,7 @@ var uBlockMessaging = (function(name){ continue; } callback = requestIdToCallbackMap[id]; - if ( !callback ) { + if ( callback === undefined ) { continue; } // Must be removed before calling client to be sure to not execute @@ -143,7 +143,12 @@ var uBlockMessaging = (function(name){ // These can be inserted before the DOM is loaded. var cosmeticFilters = function(details) { - var style = document.createElement('style'); + // Maybe uBlock's style tag was already injected? + var style = document.getElementById('ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5'); + if ( style !== null ) { + return; + } + style = document.createElement('style'); style.setAttribute('id', 'ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5'); var donthide = details.cosmeticDonthide; var hide = details.cosmeticHide; @@ -198,18 +203,18 @@ var netFilters = function(details) { }; var filteringHandler = function(details) { - // The port will never be used again at this point, disconnecting allows - // the browser to flush this script from memory. + if ( details ) { + if ( details.cosmeticHide.length !== 0 || details.cosmeticDonthide.length !== 0 ) { + cosmeticFilters(details); + } + if ( details.netHide.length !== 0 ) { + netFilters(details); + } + // The port will never be used again at this point, disconnecting allows + // the browser to flush this script from memory. + } + // Do not close the port before we are done. uBlockMessaging.stop(); - if ( !details ) { - return; - } - if ( details.cosmeticHide.length !== 0 || details.cosmeticDonthide.length !== 0 ) { - cosmeticFilters(details); - } - if ( details.netHide.length !== 0 ) { - netFilters(details); - } }; var hideElements = function(selectors) { diff --git a/js/messaging.js b/js/messaging.js index e928cc961..180959b65 100644 --- a/js/messaging.js +++ b/js/messaging.js @@ -111,15 +111,15 @@ var announce = function(msg) { /******************************************************************************/ var onMessage = function(request, port) { + var reqId = request.id; // Annoucement: dispatch everywhere. - if ( request.id < 0 ) { + if ( reqId < 0 ) { announce(request.msg); return; } var listener = listenerFromPortName(port.name) || defaultHandler; - var reqId = request.id; // Being told - if ( reqId <= 0 ) { + if ( reqId === 0 ) { listener(request.msg, port.sender, nullFunc); return; }