1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 03:12:33 +01:00

code review

This commit is contained in:
gorhill 2014-09-16 15:39:41 -04:00
parent fc66d2626b
commit 19c6155787
2 changed files with 22 additions and 17 deletions

View File

@ -56,7 +56,7 @@ var uBlockMessaging = (function(name){
return; return;
} }
var callback = requestIdToCallbackMap[details.id]; var callback = requestIdToCallbackMap[details.id];
if ( !callback ) { if ( callback === undefined ) {
return; return;
} }
// Must be removed before calling client to be sure to not execute // Must be removed before calling client to be sure to not execute
@ -117,7 +117,7 @@ var uBlockMessaging = (function(name){
continue; continue;
} }
callback = requestIdToCallbackMap[id]; callback = requestIdToCallbackMap[id];
if ( !callback ) { if ( callback === undefined ) {
continue; continue;
} }
// Must be removed before calling client to be sure to not execute // 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. // These can be inserted before the DOM is loaded.
var cosmeticFilters = function(details) { 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'); style.setAttribute('id', 'ublock-preload-1ae7a5f130fc79b4fdb8a4272d9426b5');
var donthide = details.cosmeticDonthide; var donthide = details.cosmeticDonthide;
var hide = details.cosmeticHide; var hide = details.cosmeticHide;
@ -198,18 +203,18 @@ var netFilters = function(details) {
}; };
var filteringHandler = function(details) { var filteringHandler = function(details) {
// The port will never be used again at this point, disconnecting allows if ( details ) {
// the browser to flush this script from memory.
uBlockMessaging.stop();
if ( !details ) {
return;
}
if ( details.cosmeticHide.length !== 0 || details.cosmeticDonthide.length !== 0 ) { if ( details.cosmeticHide.length !== 0 || details.cosmeticDonthide.length !== 0 ) {
cosmeticFilters(details); cosmeticFilters(details);
} }
if ( details.netHide.length !== 0 ) { if ( details.netHide.length !== 0 ) {
netFilters(details); 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();
}; };
var hideElements = function(selectors) { var hideElements = function(selectors) {

View File

@ -111,15 +111,15 @@ var announce = function(msg) {
/******************************************************************************/ /******************************************************************************/
var onMessage = function(request, port) { var onMessage = function(request, port) {
var reqId = request.id;
// Annoucement: dispatch everywhere. // Annoucement: dispatch everywhere.
if ( request.id < 0 ) { if ( reqId < 0 ) {
announce(request.msg); announce(request.msg);
return; return;
} }
var listener = listenerFromPortName(port.name) || defaultHandler; var listener = listenerFromPortName(port.name) || defaultHandler;
var reqId = request.id;
// Being told // Being told
if ( reqId <= 0 ) { if ( reqId === 0 ) {
listener(request.msg, port.sender, nullFunc); listener(request.msg, port.sender, nullFunc);
return; return;
} }