mirror of
https://github.com/gorhill/uMatrix.git
synced 2024-06-01 18:10:17 +12:00
* Minimal refactor of vapi-client to get firefox version working * Move messagingConnector into vAPI.messaging * Improve vAPI.messaging setup and connecting * Shutdown-related changes, implement remaining missing functions
This commit is contained in:
parent
2b02690b93
commit
204aaae213
|
@ -73,132 +73,139 @@ vAPI.shutdown = (function() {
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
var messagingConnector = function(response) {
|
|
||||||
if ( !response ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var channels = vAPI.messaging.channels;
|
|
||||||
var channel, listener;
|
|
||||||
|
|
||||||
if ( response.broadcast && !response.channelName ) {
|
|
||||||
for ( channel in channels ) {
|
|
||||||
if ( channels.hasOwnProperty(channel) === false ) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
listener = channels[channel].listener;
|
|
||||||
if ( typeof listener === 'function' ) {
|
|
||||||
listener(response.msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( response.requestId ) {
|
|
||||||
listener = vAPI.messaging.listeners[response.requestId];
|
|
||||||
delete vAPI.messaging.listeners[response.requestId];
|
|
||||||
delete response.requestId;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( !listener ) {
|
|
||||||
channel = channels[response.channelName];
|
|
||||||
listener = channel && channel.listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( typeof listener === 'function' ) {
|
|
||||||
listener(response.msg);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
vAPI.messaging = {
|
vAPI.messaging = {
|
||||||
channels: {},
|
listeners: new Set(),
|
||||||
listeners: {},
|
pending: new Map(),
|
||||||
requestId: 1,
|
requestId: 1,
|
||||||
|
connected: false,
|
||||||
|
|
||||||
setup: function() {
|
setup: function() {
|
||||||
this.connector = function(msg) {
|
this.addListener(this.builtinListener);
|
||||||
messagingConnector(JSON.parse(msg));
|
if ( this.toggleListenerCallback === null ) {
|
||||||
};
|
this.toggleListenerCallback = this.toggleListener.bind(this);
|
||||||
|
}
|
||||||
addMessageListener(this.connector);
|
window.addEventListener('pagehide', this.toggleListenerCallback, true);
|
||||||
|
window.addEventListener('pageshow', this.toggleListenerCallback, true);
|
||||||
this.channels.vAPI = {
|
|
||||||
listener: function(msg) {
|
|
||||||
if ( typeof msg.cmd === 'string' && msg.cmd === 'injectScript' ) {
|
|
||||||
var details = msg.details;
|
|
||||||
if ( !details.allFrames && window !== window.top ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
self.injectScript(details.file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
|
|
||||||
close: function() {
|
shutdown: function() {
|
||||||
if ( !this.connector ) {
|
if ( this.toggleListenerCallback !== null ) {
|
||||||
|
window.removeEventListener('pagehide', this.toggleListenerCallback, true);
|
||||||
|
window.removeEventListener('pageshow', this.toggleListenerCallback, true);
|
||||||
|
}
|
||||||
|
this.removeAllListeners();
|
||||||
|
//service pending callbacks
|
||||||
|
var pending = this.pending;
|
||||||
|
this.pending.clear();
|
||||||
|
for ( var callback of pending.values() ) {
|
||||||
|
if ( typeof callback === 'function' ) {
|
||||||
|
callback(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
connect: function() {
|
||||||
|
if ( !this.connected ) {
|
||||||
|
if ( this.messageListenerCallback === null ) {
|
||||||
|
this.messageListenerCallback = this.messageListener.bind(this);
|
||||||
|
}
|
||||||
|
addMessageListener(this.messageListenerCallback);
|
||||||
|
this.connected = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
disconnect: function() {
|
||||||
|
if ( this.connected ) {
|
||||||
|
removeMessageListener();
|
||||||
|
this.connected = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
messageListener: function(msg) {
|
||||||
|
var details = JSON.parse(msg);
|
||||||
|
if ( !details ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
removeMessageListener();
|
if ( details.broadcast ) {
|
||||||
this.connector = null;
|
this.sendToListeners(details.msg);
|
||||||
this.channels = {};
|
|
||||||
this.listeners = {};
|
|
||||||
},
|
|
||||||
|
|
||||||
channel: function(channelName, callback) {
|
|
||||||
if ( !channelName ) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.channels[channelName] = {
|
if ( details.requestId ) {
|
||||||
channelName: channelName,
|
var listener = this.pending.get(details.requestId);
|
||||||
listener: typeof callback === 'function' ? callback : null,
|
if ( listener !== undefined ) {
|
||||||
send: function(message, callback) {
|
this.pending.delete(details.requestId);
|
||||||
if ( !vAPI.messaging.connector ) {
|
listener(details.msg);
|
||||||
vAPI.messaging.setup();
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
message = {
|
|
||||||
channelName: self._sandboxId_ + '|' + this.channelName,
|
|
||||||
msg: message
|
|
||||||
};
|
|
||||||
|
|
||||||
if ( callback ) {
|
|
||||||
message.requestId = vAPI.messaging.requestId++;
|
|
||||||
vAPI.messaging.listeners[message.requestId] = callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
sendAsyncMessage('umatrix:background', message);
|
|
||||||
},
|
|
||||||
close: function() {
|
|
||||||
delete vAPI.messaging.channels[this.channelName];
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
messageListenerCallback: null,
|
||||||
|
|
||||||
|
builtinListener: function(msg) {
|
||||||
|
if ( typeof msg.cmd === 'string' && msg.cmd === 'injectScript' ) {
|
||||||
|
var details = msg.details;
|
||||||
|
if ( !details.allFrames && window !== window.top ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.injectScript(details.file);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
send: function(channelName, message, callback) {
|
||||||
|
this.connect()
|
||||||
|
|
||||||
|
message = {
|
||||||
|
channelName: self._sandboxId_ + '|' + channelName,
|
||||||
|
msg: message
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.channels[channelName];
|
if ( callback ) {
|
||||||
|
message.requestId = this.requestId++;
|
||||||
|
this.pending.set(message.requestId, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendAsyncMessage('umatrix:background', message);
|
||||||
},
|
},
|
||||||
|
|
||||||
toggleListener: function({type, persisted}) {
|
toggleListener: function({type, persisted}) {
|
||||||
if ( !vAPI.messaging.connector ) {
|
if ( type === 'pagehide' && !persisted ) {
|
||||||
|
vAPI.shutdown.exec();
|
||||||
|
this.shutdown();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( type === 'pagehide' ) {
|
if ( type === 'pagehide' ) {
|
||||||
removeMessageListener();
|
this.disconnect();
|
||||||
return;
|
} else /* if ( type === 'pageshow' ) */ {
|
||||||
|
this.connect();
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
toggleListenerCallback: null,
|
||||||
|
|
||||||
if ( persisted ) {
|
sendToListeners: function(msg) {
|
||||||
addMessageListener(vAPI.messaging.connector);
|
for ( var listener of this.listeners ) {
|
||||||
|
listener(msg);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
addListener: function(listener) {
|
||||||
|
this.listeners.add(listener);
|
||||||
|
this.connect()
|
||||||
|
},
|
||||||
|
|
||||||
|
removeListener: function(listener) {
|
||||||
|
this.listeners.delete(listener);
|
||||||
|
},
|
||||||
|
|
||||||
|
removeAllListeners: function() {
|
||||||
|
this.disconnect();
|
||||||
|
this.listeners.clear();;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener('pagehide', vAPI.messaging.toggleListener, true);
|
vAPI.messaging.setup()
|
||||||
window.addEventListener('pageshow', vAPI.messaging.toggleListener, true);
|
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
@ -206,7 +213,9 @@ window.addEventListener('pageshow', vAPI.messaging.toggleListener, true);
|
||||||
// we are not a top window (because element picker can still
|
// we are not a top window (because element picker can still
|
||||||
// be injected in top window).
|
// be injected in top window).
|
||||||
if ( window !== window.top ) {
|
if ( window !== window.top ) {
|
||||||
// Can anything be done?
|
vAPI.shutdown.add(function() {
|
||||||
|
vAPI = null;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
Loading…
Reference in a new issue