2014-11-24 20:00:27 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-07 15:55:04 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2014-2016 The uBlock Origin authors
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
/* global HTMLDocument, XMLDocument,
|
|
|
|
addMessageListener, removeMessageListener, sendAsyncMessage, outerShutdown
|
|
|
|
*/
|
2014-12-16 13:44:34 +01:00
|
|
|
|
2016-08-14 03:45:01 +02:00
|
|
|
'use strict';
|
|
|
|
|
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
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/464
|
|
|
|
if ( document instanceof HTMLDocument === false ) {
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1528
|
|
|
|
// A XMLDocument can be a valid HTML document.
|
|
|
|
if (
|
|
|
|
document instanceof XMLDocument === false ||
|
|
|
|
document.createElement('div') instanceof HTMLDivElement === false
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-09-30 15:35:24 +02: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 15:33:38 +02:00
|
|
|
|
|
|
|
self.rpc = self.rpc || function(){};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-08 19:34:28 +01:00
|
|
|
var vAPI = self.vAPI = self.vAPI || {};
|
2016-06-30 22:10:38 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
var referenceCounter = 0;
|
|
|
|
|
|
|
|
vAPI.lock = function() {
|
|
|
|
referenceCounter += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.unlock = function() {
|
|
|
|
referenceCounter -= 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-06-30 22:10:38 +02:00
|
|
|
vAPI.executionCost = {
|
|
|
|
start: function(){},
|
|
|
|
stop: function(){}
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
vAPI.executionCost = vAPI.executionCost || {
|
|
|
|
tcost: 0,
|
|
|
|
tstart: 0,
|
|
|
|
nstart: 0,
|
|
|
|
level: 1,
|
|
|
|
start: function() {
|
|
|
|
if ( this.nstart === 0 ) {
|
|
|
|
this.tstart = window.performance.now();
|
|
|
|
}
|
|
|
|
this.nstart += 1;
|
|
|
|
},
|
|
|
|
stop: function(mark) {
|
|
|
|
this.nstart -= 1;
|
|
|
|
if ( this.nstart !== 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var tcost = window.performance.now() - this.tstart;
|
|
|
|
this.tcost += tcost;
|
|
|
|
if ( mark === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var top = window === window.top;
|
|
|
|
if ( !top && this.level < 2 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var context = window === window.top ? ' top' : 'frame';
|
|
|
|
var percent = this.tcost / window.performance.now() * 100;
|
|
|
|
console.log(
|
|
|
|
'uBO cost (' + context + '): ' +
|
|
|
|
this.tcost.toFixed(1) + 'ms/' +
|
|
|
|
percent.toFixed(1) + '% (' +
|
|
|
|
mark + ': ' + tcost.toFixed(2) + 'ms)'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
vAPI.executionCost.start();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
vAPI.firefox = true;
|
2016-07-10 01:21:46 +02:00
|
|
|
|
|
|
|
vAPI.randomToken = function() {
|
|
|
|
return String.fromCharCode(Date.now() % 26 + 97) +
|
|
|
|
Math.floor(Math.random() * 982451653 + 982451653).toString(36);
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.sessionId = vAPI.randomToken();
|
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;
|
2015-08-18 17:44:24 +02:00
|
|
|
while ( (job = jobs.pop()) ) {
|
2015-04-08 12:15:10 +02:00
|
|
|
job();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
add: add,
|
|
|
|
exec: exec
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-09-30 15:33:38 +02:00
|
|
|
(function() {
|
|
|
|
var hostname = location.hostname;
|
|
|
|
if ( !hostname ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var filters = self.rpc({
|
|
|
|
fnName: 'getScriptTagFilters',
|
2016-01-11 06:37:57 +01:00
|
|
|
rootURL: self.location.href,
|
2016-01-05 16:08:28 +01:00
|
|
|
frameURL: self.location.href,
|
|
|
|
frameHostname: hostname
|
2015-09-30 15:33:38 +02:00
|
|
|
});
|
|
|
|
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 16:46:20 +02:00
|
|
|
vAPI.messaging = {
|
2016-03-06 16:51:06 +01:00
|
|
|
channels: Object.create(null),
|
|
|
|
channelCount: 0,
|
|
|
|
pending: Object.create(null),
|
2015-07-07 17:03:26 +02:00
|
|
|
pendingCount: 0,
|
2015-06-29 16:46:20 +02:00
|
|
|
auxProcessId: 1,
|
|
|
|
connected: false,
|
2016-03-06 16:51:06 +01:00
|
|
|
|
|
|
|
messageListener: function(msg) {
|
|
|
|
var details = JSON.parse(msg);
|
|
|
|
if ( !details ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sent to all channels
|
|
|
|
if ( details.broadcast && !details.channelName ) {
|
|
|
|
for ( var channelName in this.channels ) {
|
|
|
|
this.sendToChannelListeners(channelName, details.msg);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response to specific message previously sent
|
|
|
|
if ( details.auxProcessId ) {
|
|
|
|
var listener = this.pending[details.auxProcessId];
|
|
|
|
delete this.pending[details.auxProcessId];
|
|
|
|
delete details.auxProcessId; // TODO: why?
|
|
|
|
if ( listener ) {
|
|
|
|
this.pendingCount -= 1;
|
|
|
|
listener(details.msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sent to a specific channel
|
|
|
|
var response = this.sendToChannelListeners(details.channelName, details.msg);
|
|
|
|
|
|
|
|
// Respond back if required
|
|
|
|
if ( details.mainProcessId === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sendAsyncMessage('ublock0:background', {
|
|
|
|
mainProcessId: details.mainProcessId,
|
|
|
|
msg: response
|
|
|
|
});
|
2015-06-29 16:46:20 +02:00
|
|
|
},
|
2015-06-26 06:08:41 +02:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
builtinListener: function(msg) {
|
2015-07-03 13:00:29 +02:00
|
|
|
if ( msg.cmd === 'injectScript' ) {
|
2015-07-03 19:32:00 +02:00
|
|
|
// injectScript is not always present.
|
|
|
|
// - See contentObserver.initContentScripts in frameModule.js
|
|
|
|
if ( typeof self.injectScript !== 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-03 13:00:29 +02:00
|
|
|
var details = msg.details;
|
2016-03-07 15:55:04 +01:00
|
|
|
// Whether to inject in all child frames. Default to only top frame.
|
|
|
|
var allFrames = details.allFrames || false;
|
|
|
|
if ( allFrames !== true && window !== window.top ) {
|
2015-07-03 13:00:29 +02:00
|
|
|
return;
|
2015-06-29 16:46:20 +02:00
|
|
|
}
|
2015-10-28 14:35:13 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/876
|
|
|
|
// Enforce `details.runAt`. Default to `document_end`.
|
2016-03-07 15:55:04 +01:00
|
|
|
var runAt = details.runAt || 'document_end';
|
|
|
|
if ( runAt === 'document_start' || document.readyState !== 'loading' ) {
|
2015-10-28 14:35:13 +01:00
|
|
|
self.injectScript(details.file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var injectScriptDelayed = function() {
|
|
|
|
document.removeEventListener('DOMContentLoaded', injectScriptDelayed);
|
|
|
|
self.injectScript(details.file);
|
|
|
|
};
|
|
|
|
document.addEventListener('DOMContentLoaded', injectScriptDelayed);
|
2015-07-03 13:00:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( msg.cmd === 'shutdownSandbox' ) {
|
|
|
|
vAPI.shutdown.exec();
|
2016-03-06 16:51:06 +01:00
|
|
|
this.stop();
|
2015-07-03 19:32:00 +02:00
|
|
|
if ( typeof self.outerShutdown === 'function' ) {
|
|
|
|
outerShutdown();
|
|
|
|
}
|
2016-04-21 20:34:20 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1573
|
|
|
|
// Will let uBO's own web pages close themselves. `window.top` is
|
|
|
|
// used on the assumption that uBO's own web pages will never be
|
|
|
|
// embedded in anything else than its own documents.
|
|
|
|
try {
|
|
|
|
var top = window.top;
|
|
|
|
if ( top.location.href.startsWith(vAPI.getURL('')) ) {
|
|
|
|
top.close();
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
}
|
2015-07-03 13:00:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
toggleListener: function({type, persisted}) {
|
|
|
|
if ( type === 'pagehide' && !persisted ) {
|
|
|
|
vAPI.shutdown.exec();
|
|
|
|
this.stop();
|
|
|
|
if ( typeof self.outerShutdown === 'function' ) {
|
|
|
|
outerShutdown();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( type === 'pagehide' ) {
|
|
|
|
this.disconnect();
|
|
|
|
} else /* if ( type === 'pageshow' ) */ {
|
|
|
|
this.connect();
|
|
|
|
}
|
2015-06-29 16:46:20 +02:00
|
|
|
},
|
2016-03-06 16:51:06 +01:00
|
|
|
toggleListenerCallback: null,
|
2015-06-26 06:08:41 +02:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
start: function() {
|
|
|
|
this.addChannelListener('vAPI', this.builtinListener.bind(this));
|
|
|
|
if ( this.toggleListenerCallback === null ) {
|
|
|
|
this.toggleListenerCallback = this.toggleListener.bind(this);
|
|
|
|
}
|
|
|
|
window.addEventListener('pagehide', this.toggleListenerCallback, true);
|
|
|
|
window.addEventListener('pageshow', this.toggleListenerCallback, true);
|
2015-07-03 19:32:00 +02:00
|
|
|
},
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
stop: function() {
|
|
|
|
if ( this.toggleListenerCallback !== null ) {
|
|
|
|
window.removeEventListener('pagehide', this.toggleListenerCallback, true);
|
|
|
|
window.removeEventListener('pageshow', this.toggleListenerCallback, true);
|
|
|
|
}
|
2015-07-03 13:00:29 +02:00
|
|
|
this.disconnect();
|
2016-03-06 16:51:06 +01:00
|
|
|
this.channels = Object.create(null);
|
|
|
|
this.channelCount = 0;
|
2015-07-07 17:03:26 +02:00
|
|
|
// service pending callbacks
|
2016-03-06 16:51:06 +01:00
|
|
|
var pending = this.pending, callback;
|
|
|
|
this.pending = Object.create(null);
|
2015-07-07 17:03:26 +02:00
|
|
|
this.pendingCount = 0;
|
|
|
|
for ( var auxId in pending ) {
|
2016-03-09 23:13:22 +01:00
|
|
|
callback = pending[auxId];
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
callback(null);
|
2015-07-07 17:03:26 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-29 16:46:20 +02:00
|
|
|
},
|
2015-06-26 06:08:41 +02:00
|
|
|
|
2015-07-03 13:00:29 +02:00
|
|
|
connect: function() {
|
|
|
|
if ( !this.connected ) {
|
2016-03-06 16:51:06 +01:00
|
|
|
addMessageListener(this.messageListener.bind(this));
|
2015-07-03 13:00:29 +02:00
|
|
|
this.connected = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
disconnect: function() {
|
|
|
|
if ( this.connected ) {
|
|
|
|
removeMessageListener();
|
|
|
|
this.connected = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
send: function(channelName, message, callback) {
|
|
|
|
this.sendTo(channelName, message, undefined, undefined, callback);
|
|
|
|
},
|
2015-06-29 16:46:20 +02:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
sendTo: function(channelName, message, toTabId, toChannel, callback) {
|
|
|
|
if ( !this.connected ) {
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
callback();
|
2015-07-03 19:32:00 +02:00
|
|
|
}
|
2015-06-29 16:46:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-03-06 16:51:06 +01: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 ( this.pendingCount > 25 ) {
|
|
|
|
vAPI.shutdown.exec();
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
2016-03-06 16:51:06 +01:00
|
|
|
this.connect();
|
|
|
|
var auxProcessId;
|
|
|
|
if ( callback ) {
|
|
|
|
auxProcessId = this.auxProcessId++;
|
|
|
|
this.pending[auxProcessId] = callback;
|
|
|
|
this.pendingCount += 1;
|
2015-06-26 06:08:41 +02:00
|
|
|
}
|
2015-06-29 16:46:20 +02:00
|
|
|
sendAsyncMessage('ublock0:background', {
|
2016-03-06 16:51:06 +01:00
|
|
|
channelName: self._sandboxId_ + '|' + channelName,
|
|
|
|
auxProcessId: auxProcessId,
|
|
|
|
toTabId: toTabId,
|
|
|
|
toChannel: toChannel,
|
|
|
|
msg: message
|
2015-06-29 16:46:20 +02:00
|
|
|
});
|
2016-03-06 16:51:06 +01:00
|
|
|
},
|
2015-06-29 16:46:20 +02:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
addChannelListener: function(channelName, callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
return;
|
2015-09-30 15:33:38 +02:00
|
|
|
}
|
2016-03-06 16:51:06 +01:00
|
|
|
var listeners = this.channels[channelName];
|
|
|
|
if ( listeners !== undefined && listeners.indexOf(callback) !== -1 ) {
|
|
|
|
console.error('Duplicate listener on channel "%s"', channelName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( listeners === undefined ) {
|
|
|
|
this.channels[channelName] = [callback];
|
|
|
|
this.channelCount += 1;
|
|
|
|
} else {
|
|
|
|
listeners.push(callback);
|
|
|
|
}
|
|
|
|
this.connect();
|
|
|
|
},
|
2015-06-26 06:08:41 +02:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
removeChannelListener: function(channelName, callback) {
|
|
|
|
if ( typeof callback !== 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var listeners = this.channels[channelName];
|
|
|
|
if ( listeners === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var pos = this.listeners.indexOf(callback);
|
|
|
|
if ( pos === -1 ) {
|
|
|
|
console.error('Listener not found on channel "%s"', channelName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
listeners.splice(pos, 1);
|
|
|
|
if ( listeners.length === 0 ) {
|
|
|
|
delete this.channels[channelName];
|
|
|
|
this.channelCount -= 1;
|
|
|
|
}
|
|
|
|
},
|
2014-11-24 20:00:27 +01:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
removeAllChannelListeners: function(channelName) {
|
|
|
|
var listeners = this.channels[channelName];
|
|
|
|
if ( listeners === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete this.channels[channelName];
|
|
|
|
this.channelCount -= 1;
|
|
|
|
},
|
2015-06-28 23:42:08 +02:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
sendToChannelListeners: function(channelName, msg) {
|
|
|
|
var listeners = this.channels[channelName];
|
|
|
|
if ( listeners === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var response;
|
|
|
|
for ( var i = 0, n = listeners.length; i < n; i++ ) {
|
|
|
|
response = listeners[i](msg);
|
|
|
|
if ( response !== undefined ) {
|
|
|
|
break;
|
|
|
|
}
|
2015-01-26 20:26:45 +01:00
|
|
|
}
|
2016-03-06 16:51:06 +01:00
|
|
|
return response;
|
2015-01-08 21:18:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
vAPI.messaging.start();
|
2015-01-08 21:18:05 +01:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
// https://www.youtube.com/watch?v=Cg0cmhjdiLs
|
2015-07-03 13:00:29 +02: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
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-06-30 22:10:38 +02:00
|
|
|
vAPI.executionCost.stop('vapi-client.js');
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
})(this);
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|