2014-11-24 20:00:27 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
µBlock - a browser extension to block requests.
|
2014-11-24 20:00:27 +01:00
|
|
|
Copyright (C) 2014 The µBlock authors
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2014-12-16 13:44:34 +01:00
|
|
|
/* global addMessageListener, removeMessageListener, sendAsyncMessage */
|
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
// For non background pages
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
(function(self) {
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-08 19:34:28 +01:00
|
|
|
var vAPI = self.vAPI = self.vAPI || {};
|
2014-11-24 20:00:27 +01:00
|
|
|
vAPI.firefox = true;
|
2015-06-04 15:37:53 +02:00
|
|
|
vAPI.sessionId = String.fromCharCode(Date.now() % 26 + 97) +
|
2015-02-08 19:34:28 +01:00
|
|
|
Math.random().toString(36).slice(2);
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-17 19:49:43 +02:00
|
|
|
vAPI.setTimeout = vAPI.setTimeout || function(callback, delay, extra) {
|
|
|
|
return setTimeout(function(a) { callback(a); }, delay, extra);
|
2015-05-17 19:02:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-08 12:15:10 +02:00
|
|
|
vAPI.shutdown = (function() {
|
|
|
|
var jobs = [];
|
|
|
|
|
|
|
|
var add = function(job) {
|
|
|
|
jobs.push(job);
|
|
|
|
};
|
|
|
|
|
|
|
|
var exec = function() {
|
|
|
|
//console.debug('Shutting down...');
|
|
|
|
var job;
|
|
|
|
while ( job = jobs.pop() ) {
|
|
|
|
job();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
add: add,
|
|
|
|
exec: exec
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-26 06:08:41 +02:00
|
|
|
var MessagingListeners = function(callback) {
|
|
|
|
this.listeners = [];
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
this.listeners.push(callback);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MessagingListeners.prototype.add = function(callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( this.listeners.indexOf(callback) !== -1 ) {
|
|
|
|
throw new Error('Duplicate listener.');
|
|
|
|
}
|
|
|
|
this.listeners.push(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
MessagingListeners.prototype.remove = function(callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( this.listeners.indexOf(callback) === -1 ) {
|
|
|
|
throw new Error('Listener not found.');
|
|
|
|
}
|
|
|
|
this.listeners.splice(this.listeners.indexOf(callback), 1);
|
|
|
|
};
|
|
|
|
|
2015-06-28 23:42:08 +02:00
|
|
|
MessagingListeners.prototype.removeAll = function() {
|
|
|
|
this.listeners = [];
|
|
|
|
};
|
|
|
|
|
2015-06-26 06:08:41 +02:00
|
|
|
MessagingListeners.prototype.process = function(msg) {
|
|
|
|
var listeners = this.listeners;
|
|
|
|
var n = listeners.length;
|
|
|
|
for ( var i = 0; i < n; i++ ) {
|
|
|
|
listeners[i](msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
var messagingConnector = function(response) {
|
|
|
|
if ( !response ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:08:41 +02:00
|
|
|
var messaging = vAPI.messaging;
|
|
|
|
var channels = messaging.channels;
|
|
|
|
var channel;
|
2014-11-24 20:00:27 +01:00
|
|
|
|
2015-06-26 06:08:41 +02:00
|
|
|
// Sent to all channels
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( response.broadcast === true && !response.channelName ) {
|
2014-11-24 20:00:27 +01:00
|
|
|
for ( channel in channels ) {
|
2015-06-26 06:08:41 +02:00
|
|
|
if ( channels[channel] instanceof MessagingChannel === false ) {
|
2014-11-24 20:00:27 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-06-26 06:08:41 +02:00
|
|
|
channels[channel].listeners.process(response.msg);
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:08:41 +02:00
|
|
|
// Response to specific message previously sent
|
2014-11-24 20:00:27 +01:00
|
|
|
if ( response.requestId ) {
|
2015-06-26 06:08:41 +02:00
|
|
|
var listener = messaging.pending[response.requestId];
|
|
|
|
delete messaging.pending[response.requestId];
|
|
|
|
delete response.requestId; // TODO: why?
|
|
|
|
if ( listener ) {
|
|
|
|
listener(response.msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sent to a specific channel
|
|
|
|
channel = channels[response.channelName];
|
|
|
|
if ( channel instanceof MessagingChannel ) {
|
|
|
|
channel.listeners.process(response.msg);
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
2015-06-26 06:08:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-11-24 20:00:27 +01:00
|
|
|
|
2015-06-26 06:08:41 +02:00
|
|
|
var MessagingChannel = function(name, callback) {
|
|
|
|
this.channelName = name;
|
|
|
|
this.listeners = new MessagingListeners(callback);
|
|
|
|
this.refCount = 1;
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
var messaging = vAPI.messaging;
|
|
|
|
if ( !messaging.connected ) {
|
|
|
|
messaging.setup();
|
|
|
|
}
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
2015-06-26 06:08:41 +02:00
|
|
|
};
|
2014-11-24 20:00:27 +01:00
|
|
|
|
2015-06-26 06:08:41 +02:00
|
|
|
MessagingChannel.prototype.send = function(message, callback) {
|
|
|
|
var messaging = vAPI.messaging;
|
|
|
|
if ( !messaging.connected ) {
|
|
|
|
messaging.setup();
|
|
|
|
}
|
|
|
|
var requestId;
|
|
|
|
if ( callback ) {
|
|
|
|
requestId = messaging.requestId++;
|
|
|
|
messaging.pending[requestId] = callback;
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
2015-06-26 06:08:41 +02:00
|
|
|
sendAsyncMessage('ublock0:background', {
|
|
|
|
channelName: self._sandboxId_ + '|' + this.channelName,
|
|
|
|
requestId: requestId,
|
|
|
|
msg: message
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
MessagingChannel.prototype.close = function() {
|
|
|
|
this.refCount -= 1;
|
|
|
|
if ( this.refCount !== 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete vAPI.messaging.channels[this.channelName];
|
|
|
|
};
|
|
|
|
|
|
|
|
MessagingChannel.prototype.addListener = function(callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.listeners.add(callback);
|
|
|
|
var messaging = vAPI.messaging;
|
|
|
|
if ( !messaging.connected ) {
|
|
|
|
messaging.setup();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MessagingChannel.prototype.removeListener = function(callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.listeners.remove(callback);
|
2014-11-24 20:00:27 +01:00
|
|
|
};
|
|
|
|
|
2015-06-28 23:42:08 +02:00
|
|
|
MessagingChannel.prototype.removeAllListeners = function() {
|
|
|
|
this.listeners.removeAll();
|
|
|
|
};
|
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.messaging = {
|
|
|
|
channels: {},
|
2015-06-26 06:08:41 +02:00
|
|
|
pending: {},
|
2014-11-24 20:00:27 +01:00
|
|
|
requestId: 1,
|
2015-06-26 06:08:41 +02:00
|
|
|
connected: false,
|
|
|
|
connector: function(msg) {
|
|
|
|
messagingConnector(JSON.parse(msg));
|
|
|
|
},
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
setup: function() {
|
2015-01-08 21:18:05 +01:00
|
|
|
addMessageListener(this.connector);
|
2015-06-26 06:08:41 +02:00
|
|
|
this.connected = true;
|
|
|
|
this.channels['vAPI'] = new MessagingChannel('vAPI', function(msg) {
|
2014-12-24 23:11:36 +01:00
|
|
|
if ( msg.cmd === 'injectScript' ) {
|
2014-12-16 22:31:03 +01:00
|
|
|
var details = msg.details;
|
2014-12-24 23:11:36 +01:00
|
|
|
if ( !details.allFrames && window !== window.top ) {
|
2014-12-16 22:31:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-06-04 15:37:53 +02:00
|
|
|
// TODO: investigate why this happens, and if this happens
|
|
|
|
// legitimately (content scripts not injected I suspect, so
|
|
|
|
// that would make this legitimate).
|
|
|
|
// Case: open popup UI from icon in uBlock's logger
|
|
|
|
if ( typeof self.injectScript === 'function' ) {
|
|
|
|
self.injectScript(details.file);
|
|
|
|
}
|
2014-12-16 22:31:03 +01:00
|
|
|
}
|
2015-06-26 06:08:41 +02:00
|
|
|
});
|
2014-11-24 20:00:27 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
2015-06-26 06:08:41 +02:00
|
|
|
if ( !this.connected ) {
|
2014-12-24 23:11:36 +01:00
|
|
|
return;
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
2015-01-08 21:18:05 +01:00
|
|
|
removeMessageListener();
|
2015-06-26 06:08:41 +02:00
|
|
|
this.connected = false;
|
2014-12-24 23:11:36 +01:00
|
|
|
this.channels = {};
|
2015-06-26 06:08:41 +02:00
|
|
|
this.pending = {};
|
2014-11-24 20:00:27 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
channel: function(channelName, callback) {
|
|
|
|
if ( !channelName ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-26 06:08:41 +02:00
|
|
|
var channel = this.channels[channelName];
|
|
|
|
if ( channel instanceof MessagingChannel ) {
|
|
|
|
channel.addListener(callback);
|
|
|
|
channel.refCount += 1;
|
|
|
|
} else {
|
|
|
|
channel = this.channels[channelName] = new MessagingChannel(channelName, callback);
|
|
|
|
}
|
|
|
|
return channel;
|
2015-01-26 20:26:45 +01:00
|
|
|
},
|
2014-11-24 20:00:27 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
toggleListener: function({type, persisted}) {
|
2015-06-26 06:08:41 +02:00
|
|
|
if ( !vAPI.messaging.connected ) {
|
2015-01-26 20:26:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-01-08 21:18:05 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
if ( type === 'pagehide' ) {
|
|
|
|
removeMessageListener();
|
|
|
|
return;
|
|
|
|
}
|
2015-01-09 07:58:07 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
if ( persisted ) {
|
|
|
|
addMessageListener(vAPI.messaging.connector);
|
|
|
|
}
|
2015-01-08 21:18:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
window.addEventListener('pagehide', vAPI.messaging.toggleListener, true);
|
|
|
|
window.addEventListener('pageshow', vAPI.messaging.toggleListener, true);
|
2015-01-08 21:18:05 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-08 12:15:10 +02:00
|
|
|
// No need to have vAPI client linger around after shutdown if
|
|
|
|
// we are not a top window (because element picker can still
|
|
|
|
// be injected in top window).
|
|
|
|
if ( window !== window.top ) {
|
2015-05-17 16:32:40 +02:00
|
|
|
// Can anything be done?
|
2015-04-08 12:15:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
})(this);
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|