1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-03 09:39:38 +02:00
This commit is contained in:
gorhill 2014-08-28 11:08:51 -04:00
parent fe2d761908
commit c8e288fdf6
4 changed files with 153 additions and 86 deletions

View File

@ -30,7 +30,6 @@
var uBlockMessaging = (function(name){
var port = null;
var dangling = false;
var requestId = 1;
var requestIdToCallbackMap = {};
var listenCallback = null;
@ -50,9 +49,10 @@ var uBlockMessaging = (function(name){
if ( !callback ) {
return;
}
callback(details.msg);
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[details.id];
checkDisconnect();
callback(details.msg);
};
var start = function(name) {
@ -67,20 +67,30 @@ var uBlockMessaging = (function(name){
)
});
port.onMessage.addListener(onPortMessage);
};
if ( typeof name === 'string' && name.length > 0 ) {
start(name);
}
// https://github.com/gorhill/uBlock/issues/193
port.onDisconnect.addListener(stop);
};
var stop = function() {
listenCallback = null;
dangling = true;
checkDisconnect();
port.disconnect();
port = null;
flushCallbacks();
};
if ( typeof name === 'string' && name !== '' ) {
start(name);
}
var ask = function(msg, callback) {
if ( !callback ) {
if ( port === null ) {
if ( typeof callback === 'function' ) {
callback();
}
return;
}
if ( callback === undefined ) {
tell(msg);
return;
}
@ -90,22 +100,30 @@ var uBlockMessaging = (function(name){
};
var tell = function(msg) {
port.postMessage({ id: 0, msg: msg });
if ( port !== null ) {
port.postMessage({ id: 0, msg: msg });
}
};
var listen = function(callback) {
listenCallback = callback;
};
var checkDisconnect = function() {
if ( !dangling ) {
return;
var flushCallbacks = function() {
var callback;
for ( id in requestIdToCallbackMap ) {
if ( requestIdToCallbackMap.hasOwnProperty(id) === false ) {
continue;
}
callback = requestIdToCallbackMap[id];
if ( !callback ) {
continue;
}
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[id];
callback();
}
if ( Object.keys(requestIdToCallbackMap).length ) {
return;
}
port.disconnect();
port = null;
};
return {

View File

@ -38,9 +38,8 @@
// https://github.com/gorhill/httpswitchboard/issues/345
var messaging = (function(name){
var uBlockMessaging = (function(name){
var port = null;
var dangling = false;
var requestId = 1;
var requestIdToCallbackMap = {};
var listenCallback = null;
@ -60,9 +59,10 @@ var messaging = (function(name){
if ( !callback ) {
return;
}
callback(details.msg);
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[details.id];
checkDisconnect();
callback(details.msg);
};
var start = function(name) {
@ -77,20 +77,30 @@ var messaging = (function(name){
)
});
port.onMessage.addListener(onPortMessage);
};
if ( typeof name === 'string' && name.length > 0 ) {
start(name);
}
// https://github.com/gorhill/uBlock/issues/193
port.onDisconnect.addListener(stop);
};
var stop = function() {
listenCallback = null;
dangling = true;
checkDisconnect();
port.disconnect();
port = null;
flushCallbacks();
};
if ( typeof name === 'string' && name !== '' ) {
start(name);
}
var ask = function(msg, callback) {
if ( !callback ) {
if ( port === null ) {
if ( typeof callback === 'function' ) {
callback();
}
return;
}
if ( callback === undefined ) {
tell(msg);
return;
}
@ -100,22 +110,30 @@ var messaging = (function(name){
};
var tell = function(msg) {
port.postMessage({ id: 0, msg: msg });
if ( port !== null ) {
port.postMessage({ id: 0, msg: msg });
}
};
var listen = function(callback) {
listenCallback = callback;
};
var checkDisconnect = function() {
if ( !dangling ) {
return;
var flushCallbacks = function() {
var callback;
for ( id in requestIdToCallbackMap ) {
if ( requestIdToCallbackMap.hasOwnProperty(id) === false ) {
continue;
}
callback = requestIdToCallbackMap[id];
if ( !callback ) {
continue;
}
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[id];
callback();
}
if ( Object.keys(requestIdToCallbackMap).length ) {
return;
}
port.disconnect();
port = null;
};
return {
@ -189,6 +207,9 @@ 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.
uBlockMessaging.stop();
if ( !details ) {
return;
}
@ -213,7 +234,7 @@ var hideElements = function(selectors) {
}
};
messaging.ask(
uBlockMessaging.ask(
{
what: 'retrieveDomainCosmeticSelectors',
pageURL: window.location.href,
@ -225,14 +246,6 @@ messaging.ask(
/******************************************************************************/
/******************************************************************************/
// The port will never be used again at this point, disconnecting allows
// the browser to flush this script from memory.
messaging.stop();
/******************************************************************************/
/******************************************************************************/
})();
/******************************************************************************/

View File

@ -117,7 +117,6 @@
var messaging = (function(name){
var port = null;
var dangling = false;
var requestId = 1;
var requestIdToCallbackMap = {};
var listenCallback = null;
@ -137,9 +136,10 @@ var messaging = (function(name){
if ( !callback ) {
return;
}
callback(details.msg);
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[details.id];
checkDisconnect();
callback(details.msg);
};
var start = function(name) {
@ -154,20 +154,30 @@ var messaging = (function(name){
)
});
port.onMessage.addListener(onPortMessage);
};
if ( typeof name === 'string' && name.length > 0 ) {
start(name);
}
// https://github.com/gorhill/uBlock/issues/193
port.onDisconnect.addListener(stop);
};
var stop = function() {
listenCallback = null;
dangling = true;
checkDisconnect();
port.disconnect();
port = null;
flushCallbacks();
};
if ( typeof name === 'string' && name !== '' ) {
start(name);
}
var ask = function(msg, callback) {
if ( !callback ) {
if ( port === null ) {
if ( typeof callback === 'function' ) {
callback();
}
return;
}
if ( callback === undefined ) {
tell(msg);
return;
}
@ -177,22 +187,30 @@ var messaging = (function(name){
};
var tell = function(msg) {
port.postMessage({ id: 0, msg: msg });
if ( port !== null ) {
port.postMessage({ id: 0, msg: msg });
}
};
var listen = function(callback) {
listenCallback = callback;
};
var checkDisconnect = function() {
if ( !dangling ) {
return;
var flushCallbacks = function() {
var callback;
for ( id in requestIdToCallbackMap ) {
if ( requestIdToCallbackMap.hasOwnProperty(id) === false ) {
continue;
}
callback = requestIdToCallbackMap[id];
if ( !callback ) {
continue;
}
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[id];
callback();
}
if ( Object.keys(requestIdToCallbackMap).length ) {
return;
}
port.disconnect();
port = null;
};
return {

View File

@ -42,7 +42,6 @@
var messaging = (function(name){
var port = null;
var dangling = false;
var requestId = 1;
var requestIdToCallbackMap = {};
var listenCallback = null;
@ -62,9 +61,10 @@ var messaging = (function(name){
if ( !callback ) {
return;
}
callback(details.msg);
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[details.id];
checkDisconnect();
callback(details.msg);
};
var start = function(name) {
@ -79,20 +79,30 @@ var messaging = (function(name){
)
});
port.onMessage.addListener(onPortMessage);
};
if ( typeof name === 'string' && name.length > 0 ) {
start(name);
}
// https://github.com/gorhill/uBlock/issues/193
port.onDisconnect.addListener(stop);
};
var stop = function() {
listenCallback = null;
dangling = true;
checkDisconnect();
port.disconnect();
port = null;
flushCallbacks();
};
if ( typeof name === 'string' && name !== '' ) {
start(name);
}
var ask = function(msg, callback) {
if ( !callback ) {
if ( port === null ) {
if ( typeof callback === 'function' ) {
callback();
}
return;
}
if ( callback === undefined ) {
tell(msg);
return;
}
@ -102,22 +112,30 @@ var messaging = (function(name){
};
var tell = function(msg) {
port.postMessage({ id: 0, msg: msg });
if ( port !== null ) {
port.postMessage({ id: 0, msg: msg });
}
};
var listen = function(callback) {
listenCallback = callback;
};
var checkDisconnect = function() {
if ( !dangling ) {
return;
var flushCallbacks = function() {
var callback;
for ( id in requestIdToCallbackMap ) {
if ( requestIdToCallbackMap.hasOwnProperty(id) === false ) {
continue;
}
callback = requestIdToCallbackMap[id];
if ( !callback ) {
continue;
}
// Must be removed before calling client to be sure to not execute
// callback again if the client stops the messaging service.
delete requestIdToCallbackMap[id];
callback();
}
if ( Object.keys(requestIdToCallbackMap).length ) {
return;
}
port.disconnect();
port = null;
};
return {