2014-11-24 12:00:27 -07:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-12-17 13:33:53 -07:00
|
|
|
µBlock - a browser extension to block requests.
|
2014-11-24 12:00:27 -07: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
|
|
|
|
*/
|
|
|
|
|
2015-07-03 11:32:00 -06:00
|
|
|
/* global addMessageListener, removeMessageListener, sendAsyncMessage, outerShutdown */
|
2014-12-16 05:44:34 -07:00
|
|
|
|
2014-11-24 12:00:27 -07:00
|
|
|
// For non background pages
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-08 13:18:05 -07:00
|
|
|
(function(self) {
|
2014-11-24 12:00:27 -07:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-09-30 07:35:24 -06:00
|
|
|
// Not all sandboxes are given an rpc function, so assign a dummy one if it is
|
|
|
|
// missing -- this avoids the need for testing before use.
|
2015-09-30 07:33:38 -06:00
|
|
|
|
|
|
|
self.rpc = self.rpc || function(){};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-08 11:34:28 -07:00
|
|
|
var vAPI = self.vAPI = self.vAPI || {};
|
2014-11-24 12:00:27 -07:00
|
|
|
vAPI.firefox = true;
|
2015-06-04 07:37:53 -06:00
|
|
|
vAPI.sessionId = String.fromCharCode(Date.now() % 26 + 97) +
|
2015-02-08 11:34:28 -07:00
|
|
|
Math.random().toString(36).slice(2);
|
2014-11-24 12:00:27 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-17 11:49:43 -06:00
|
|
|
vAPI.setTimeout = vAPI.setTimeout || function(callback, delay, extra) {
|
|
|
|
return setTimeout(function(a) { callback(a); }, delay, extra);
|
2015-05-17 11:02:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-08 04:15:10 -06:00
|
|
|
vAPI.shutdown = (function() {
|
|
|
|
var jobs = [];
|
|
|
|
|
|
|
|
var add = function(job) {
|
|
|
|
jobs.push(job);
|
|
|
|
};
|
|
|
|
|
|
|
|
var exec = function() {
|
|
|
|
//console.debug('Shutting down...');
|
|
|
|
var job;
|
2015-08-18 09:44:24 -06:00
|
|
|
while ( (job = jobs.pop()) ) {
|
2015-04-08 04:15:10 -06:00
|
|
|
job();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
add: add,
|
|
|
|
exec: exec
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-09-30 07:33:38 -06:00
|
|
|
(function() {
|
|
|
|
var hostname = location.hostname;
|
|
|
|
if ( !hostname ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var filters = self.rpc({
|
|
|
|
fnName: 'getScriptTagFilters',
|
|
|
|
url: location.href,
|
|
|
|
hostname: hostname
|
|
|
|
});
|
|
|
|
if ( typeof filters !== 'string' || filters === '' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var reFilters = new RegExp(filters);
|
|
|
|
document.addEventListener('beforescriptexecute', function(ev) {
|
|
|
|
if ( reFilters.test(ev.target.textContent) ) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-29 08:46:20 -06:00
|
|
|
vAPI.messaging = {
|
|
|
|
channels: {},
|
|
|
|
pending: {},
|
2015-07-07 09:03:26 -06:00
|
|
|
pendingCount: 0,
|
2015-06-29 08:46:20 -06:00
|
|
|
auxProcessId: 1,
|
|
|
|
connected: false,
|
|
|
|
connector: function(msg) {
|
|
|
|
messagingConnector(JSON.parse(msg));
|
|
|
|
},
|
2015-06-25 22:08:41 -06:00
|
|
|
|
2015-07-03 05:00:29 -06:00
|
|
|
builtinChannelHandler: function(msg) {
|
|
|
|
if ( msg.cmd === 'injectScript' ) {
|
2015-07-03 11:32:00 -06:00
|
|
|
// injectScript is not always present.
|
|
|
|
// - See contentObserver.initContentScripts in frameModule.js
|
|
|
|
if ( typeof self.injectScript !== 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-03 05:00:29 -06:00
|
|
|
var details = msg.details;
|
|
|
|
if ( !details.allFrames && window !== window.top ) {
|
|
|
|
return;
|
2015-06-29 08:46:20 -06:00
|
|
|
}
|
2015-07-03 11:32:00 -06:00
|
|
|
self.injectScript(details.file);
|
2015-07-03 05:00:29 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( msg.cmd === 'shutdownSandbox' ) {
|
|
|
|
vAPI.shutdown.exec();
|
2015-07-03 11:32:00 -06:00
|
|
|
vAPI.messaging.shutdown();
|
|
|
|
if ( typeof self.outerShutdown === 'function' ) {
|
|
|
|
outerShutdown();
|
|
|
|
}
|
2015-07-03 05:00:29 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setup: function() {
|
|
|
|
this.channels['vAPI'] = new MessagingChannel('vAPI', this.builtinChannelHandler);
|
|
|
|
window.addEventListener('pagehide', this.toggleListener, true);
|
|
|
|
window.addEventListener('pageshow', this.toggleListener, true);
|
2015-06-29 08:46:20 -06:00
|
|
|
},
|
2015-06-25 22:08:41 -06:00
|
|
|
|
2015-07-03 11:32:00 -06:00
|
|
|
shutdown: function() {
|
|
|
|
this.close();
|
|
|
|
window.removeEventListener('pagehide', this.toggleListener, true);
|
|
|
|
window.removeEventListener('pageshow', this.toggleListener, true);
|
|
|
|
vAPI.messaging = null;
|
|
|
|
},
|
|
|
|
|
2015-06-29 08:46:20 -06:00
|
|
|
close: function() {
|
2015-07-03 05:00:29 -06:00
|
|
|
this.disconnect();
|
2015-06-29 08:46:20 -06:00
|
|
|
this.channels = {};
|
2015-07-07 09:03:26 -06:00
|
|
|
// service pending callbacks
|
|
|
|
var pending = this.pending, listener;
|
2015-06-29 08:46:20 -06:00
|
|
|
this.pending = {};
|
2015-07-07 09:03:26 -06:00
|
|
|
this.pendingCount = 0;
|
|
|
|
for ( var auxId in pending ) {
|
|
|
|
if ( this.pending.hasOwnProperty(auxId) ) {
|
|
|
|
listener = pending[auxId];
|
|
|
|
if ( typeof listener === 'function' ) {
|
|
|
|
listener(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 08:46:20 -06:00
|
|
|
},
|
2015-06-25 22:08:41 -06:00
|
|
|
|
2015-06-29 08:46:20 -06:00
|
|
|
channel: function(channelName, callback) {
|
|
|
|
if ( !channelName ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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-06-28 15:42:08 -06:00
|
|
|
|
2015-07-03 05:00:29 -06:00
|
|
|
connect: function() {
|
|
|
|
if ( !this.connected ) {
|
|
|
|
addMessageListener(this.connector);
|
|
|
|
this.connected = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
disconnect: function() {
|
|
|
|
if ( this.connected ) {
|
|
|
|
removeMessageListener();
|
|
|
|
this.connected = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-29 08:46:20 -06:00
|
|
|
toggleListener: function({type, persisted}) {
|
2015-07-03 11:32:00 -06:00
|
|
|
var me = vAPI.messaging;
|
|
|
|
if ( !me ) {
|
2015-06-29 08:46:20 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( type === 'pagehide' ) {
|
2015-07-03 11:32:00 -06:00
|
|
|
if ( !persisted ) {
|
|
|
|
vAPI.shutdown.exec();
|
|
|
|
vAPI.messaging.shutdown();
|
|
|
|
if ( typeof self.outerShutdown === 'function' ) {
|
|
|
|
outerShutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
me.disconnect();
|
2015-06-29 08:46:20 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-03 11:32:00 -06:00
|
|
|
me.connect();
|
2015-06-25 22:08:41 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-29 08:46:20 -06:00
|
|
|
var messagingConnector = function(details) {
|
|
|
|
if ( !details ) {
|
2014-11-24 12:00:27 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-25 22:08:41 -06:00
|
|
|
var messaging = vAPI.messaging;
|
2015-07-03 05:00:29 -06:00
|
|
|
|
|
|
|
// Sandbox might have been shutdown
|
|
|
|
if ( !messaging ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-25 22:08:41 -06:00
|
|
|
var channels = messaging.channels;
|
|
|
|
var channel;
|
2014-11-24 12:00:27 -07:00
|
|
|
|
2015-06-25 22:08:41 -06:00
|
|
|
// Sent to all channels
|
2015-08-18 09:44:24 -06:00
|
|
|
if ( details.broadcast && !details.channelName ) {
|
2014-11-24 12:00:27 -07:00
|
|
|
for ( channel in channels ) {
|
2015-06-25 22:08:41 -06:00
|
|
|
if ( channels[channel] instanceof MessagingChannel === false ) {
|
2014-11-24 12:00:27 -07:00
|
|
|
continue;
|
|
|
|
}
|
2015-06-29 08:46:20 -06:00
|
|
|
channels[channel].sendToListeners(details.msg);
|
2014-11-24 12:00:27 -07:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-25 22:08:41 -06:00
|
|
|
// Response to specific message previously sent
|
2015-06-29 08:46:20 -06:00
|
|
|
if ( details.auxProcessId ) {
|
|
|
|
var listener = messaging.pending[details.auxProcessId];
|
|
|
|
delete messaging.pending[details.auxProcessId];
|
|
|
|
delete details.auxProcessId; // TODO: why?
|
2015-06-25 22:08:41 -06:00
|
|
|
if ( listener ) {
|
2015-07-07 09:03:26 -06:00
|
|
|
messaging.pendingCount -= 1;
|
2015-06-29 08:46:20 -06:00
|
|
|
listener(details.msg);
|
2015-06-25 22:08:41 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sent to a specific channel
|
2015-06-29 08:46:20 -06:00
|
|
|
var response;
|
|
|
|
channel = channels[details.channelName];
|
2015-06-25 22:08:41 -06:00
|
|
|
if ( channel instanceof MessagingChannel ) {
|
2015-06-29 08:46:20 -06:00
|
|
|
response = channel.sendToListeners(details.msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Respond back if required
|
|
|
|
if ( details.mainProcessId !== undefined ) {
|
|
|
|
sendAsyncMessage('ublock0:background', {
|
|
|
|
mainProcessId: details.mainProcessId,
|
|
|
|
msg: response
|
|
|
|
});
|
2014-11-24 12:00:27 -07:00
|
|
|
}
|
2015-06-25 22:08:41 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-11-24 12:00:27 -07:00
|
|
|
|
2015-06-25 22:08:41 -06:00
|
|
|
var MessagingChannel = function(name, callback) {
|
|
|
|
this.channelName = name;
|
2015-06-29 08:46:20 -06:00
|
|
|
this.listeners = typeof callback === 'function' ? [callback] : [];
|
2015-06-25 22:08:41 -06:00
|
|
|
this.refCount = 1;
|
|
|
|
if ( typeof callback === 'function' ) {
|
2015-07-03 05:00:29 -06:00
|
|
|
vAPI.messaging.connect();
|
2014-11-24 12:00:27 -07:00
|
|
|
}
|
2015-06-25 22:08:41 -06:00
|
|
|
};
|
2014-11-24 12:00:27 -07:00
|
|
|
|
2015-06-25 22:08:41 -06:00
|
|
|
MessagingChannel.prototype.send = function(message, callback) {
|
2015-06-29 08:46:20 -06:00
|
|
|
this.sendTo(message, undefined, undefined, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
MessagingChannel.prototype.sendTo = function(message, toTabId, toChannel, callback) {
|
2015-06-25 22:08:41 -06:00
|
|
|
var messaging = vAPI.messaging;
|
2015-09-30 07:33:38 -06:00
|
|
|
if ( !messaging ) {
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2015-07-07 09:03:26 -06:00
|
|
|
// Too large a gap between the last request and the last response means
|
|
|
|
// the main process is no longer reachable: memory leaks and bad
|
|
|
|
// performance become a risk -- especially for long-lived, dynamic
|
|
|
|
// pages. Guard against this.
|
|
|
|
if ( messaging.pendingCount > 25 ) {
|
2015-07-12 17:33:12 -06:00
|
|
|
//console.error('uBlock> Sigh. Main process is sulking. Will try to patch things up.');
|
2015-07-07 09:03:26 -06:00
|
|
|
messaging.close();
|
|
|
|
}
|
2015-07-03 05:00:29 -06:00
|
|
|
messaging.connect();
|
2015-06-29 08:46:20 -06:00
|
|
|
var auxProcessId;
|
2015-06-25 22:08:41 -06:00
|
|
|
if ( callback ) {
|
2015-06-29 08:46:20 -06:00
|
|
|
auxProcessId = messaging.auxProcessId++;
|
|
|
|
messaging.pending[auxProcessId] = callback;
|
2015-07-07 09:03:26 -06:00
|
|
|
messaging.pendingCount += 1;
|
2014-11-24 12:00:27 -07:00
|
|
|
}
|
2015-06-25 22:08:41 -06:00
|
|
|
sendAsyncMessage('ublock0:background', {
|
|
|
|
channelName: self._sandboxId_ + '|' + this.channelName,
|
2015-06-29 08:46:20 -06:00
|
|
|
auxProcessId: auxProcessId,
|
|
|
|
toTabId: toTabId,
|
|
|
|
toChannel: toChannel,
|
2015-06-25 22:08:41 -06:00
|
|
|
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;
|
|
|
|
}
|
2015-06-29 08:46:20 -06:00
|
|
|
if ( this.listeners.indexOf(callback) !== -1 ) {
|
|
|
|
throw new Error('Duplicate listener.');
|
|
|
|
}
|
|
|
|
this.listeners.push(callback);
|
2015-07-03 05:00:29 -06:00
|
|
|
vAPI.messaging.connect();
|
2015-06-25 22:08:41 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
MessagingChannel.prototype.removeListener = function(callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-29 08:46:20 -06:00
|
|
|
var pos = this.listeners.indexOf(callback);
|
|
|
|
if ( pos === -1 ) {
|
|
|
|
throw new Error('Listener not found.');
|
|
|
|
}
|
|
|
|
this.listeners.splice(pos, 1);
|
2014-11-24 12:00:27 -07:00
|
|
|
};
|
|
|
|
|
2015-06-28 15:42:08 -06:00
|
|
|
MessagingChannel.prototype.removeAllListeners = function() {
|
2015-06-29 08:46:20 -06:00
|
|
|
this.listeners = [];
|
2015-06-28 15:42:08 -06:00
|
|
|
};
|
|
|
|
|
2015-06-29 08:46:20 -06:00
|
|
|
MessagingChannel.prototype.sendToListeners = function(msg) {
|
|
|
|
var response;
|
|
|
|
var listeners = this.listeners;
|
|
|
|
for ( var i = 0, n = listeners.length; i < n; i++ ) {
|
|
|
|
response = listeners[i](msg);
|
|
|
|
if ( response !== undefined ) {
|
|
|
|
break;
|
2015-01-26 12:26:45 -07:00
|
|
|
}
|
2015-01-08 13:18:05 -07:00
|
|
|
}
|
2015-06-29 08:46:20 -06:00
|
|
|
return response;
|
2015-01-08 13:18:05 -07:00
|
|
|
};
|
|
|
|
|
2015-06-29 08:46:20 -06:00
|
|
|
// https://www.youtube.com/watch?v=Cg0cmhjdiLs
|
2015-01-08 13:18:05 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-07-03 05:00:29 -06:00
|
|
|
vAPI.messaging.setup();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-08 04:15:10 -06: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 08:32:40 -06:00
|
|
|
// Can anything be done?
|
2015-04-08 04:15:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-08 13:18:05 -07:00
|
|
|
})(this);
|
2014-11-24 12:00:27 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|