Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
this fixes #193
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Aug 28, 2014
1 parent fe2d761 commit c8e288f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 86 deletions.
56 changes: 37 additions & 19 deletions js/contentscript-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

var uBlockMessaging = (function(name){
var port = null;
var dangling = false;
var requestId = 1;
var requestIdToCallbackMap = {};
var listenCallback = null;
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
}
if ( Object.keys(requestIdToCallbackMap).length ) {
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();
}
port.disconnect();
port = null;
};

return {
Expand Down
71 changes: 42 additions & 29 deletions js/contentscript-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
}
if ( Object.keys(requestIdToCallbackMap).length ) {
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();
}
port.disconnect();
port = null;
};

return {
Expand Down Expand Up @@ -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;
}
Expand All @@ -213,7 +234,7 @@ var hideElements = function(selectors) {
}
};

messaging.ask(
uBlockMessaging.ask(
{
what: 'retrieveDomainCosmeticSelectors',
pageURL: window.location.href,
Expand All @@ -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();

/******************************************************************************/
/******************************************************************************/

})();

/******************************************************************************/
56 changes: 37 additions & 19 deletions js/element-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@

var messaging = (function(name){
var port = null;
var dangling = false;
var requestId = 1;
var requestIdToCallbackMap = {};
var listenCallback = null;
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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;
}
if ( Object.keys(requestIdToCallbackMap).length ) {
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();
}
port.disconnect();
port = null;
};

return {
Expand Down
Loading

0 comments on commit c8e288f

Please sign in to comment.