2014-11-24 12:00:27 -07:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-07 07:55:04 -07:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2017-04-01 07:41:08 -06:00
|
|
|
Copyright (C) 2014-2017 The uBlock Origin authors
|
2014-11-24 12:00:27 -07: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 11:30:43 -07:00
|
|
|
/* global HTMLDocument, XMLDocument,
|
|
|
|
addMessageListener, removeMessageListener, sendAsyncMessage, outerShutdown
|
|
|
|
*/
|
2014-12-16 05:44:34 -07:00
|
|
|
|
2016-08-13 19:45:01 -06:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-24 12:00:27 -07:00
|
|
|
// For non background pages
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-04-01 10:14:44 -06:00
|
|
|
(function(self) {
|
2014-11-24 12:00:27 -07:00
|
|
|
|
2016-01-17 11:30:43 -07: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 12:00:27 -07:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-04-01 07:41:08 -06:00
|
|
|
// https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10
|
2017-04-03 08:10:27 -06:00
|
|
|
if ( !self.vAPI || self.vAPI.uBO !== true ) {
|
2017-04-01 07:41:08 -06:00
|
|
|
self.vAPI = { uBO: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
var vAPI = self.vAPI;
|
2016-06-30 14:10:38 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-24 12:00:27 -07:00
|
|
|
vAPI.firefox = true;
|
2016-07-09 17:21:46 -06: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 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
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-10-01 10:34:25 -06:00
|
|
|
vAPI.shutdown = {
|
|
|
|
jobs: [],
|
|
|
|
add: function(job) {
|
|
|
|
this.jobs.push(job);
|
|
|
|
},
|
|
|
|
exec: function() {
|
2015-04-08 04:15:10 -06:00
|
|
|
var job;
|
2016-10-01 10:34:25 -06:00
|
|
|
while ( (job = this.jobs.pop()) ) {
|
2015-04-08 04:15:10 -06:00
|
|
|
job();
|
|
|
|
}
|
2016-10-01 10:34:25 -06:00
|
|
|
},
|
|
|
|
remove: function(job) {
|
|
|
|
var pos;
|
|
|
|
while ( (pos = this.jobs.indexOf(job)) !== -1 ) {
|
|
|
|
this.jobs.splice(pos, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-04-08 04:15:10 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-09-30 07:33:38 -06:00
|
|
|
(function() {
|
2016-09-24 12:36:08 -06:00
|
|
|
if ( !self.getScriptTagFilters ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-30 07:33:38 -06:00
|
|
|
var hostname = location.hostname;
|
|
|
|
if ( !hostname ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-24 12:36:08 -06:00
|
|
|
var filters = self.getScriptTagFilters({
|
2016-01-10 22:37:57 -07:00
|
|
|
rootURL: self.location.href,
|
2016-01-05 08:08:28 -07:00
|
|
|
frameURL: self.location.href,
|
|
|
|
frameHostname: hostname
|
2015-09-30 07:33:38 -06: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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-10-21 11:43:46 -06:00
|
|
|
var insertUserCSS = self.injectCSS || function(){},
|
|
|
|
removeUserCSS = self.removeCSS || function(){};
|
|
|
|
|
|
|
|
var processUserCSS = function(details, callback) {
|
|
|
|
var cssText;
|
|
|
|
var aa = details.add;
|
|
|
|
if ( Array.isArray(aa) ) {
|
|
|
|
for ( cssText of aa ) {
|
|
|
|
insertUserCSS(
|
|
|
|
'data:text/css;charset=utf-8,' +
|
|
|
|
encodeURIComponent(cssText)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aa = details.remove;
|
|
|
|
if ( Array.isArray(aa) ) {
|
|
|
|
for ( cssText of aa ) {
|
|
|
|
removeUserCSS(
|
|
|
|
'data:text/css;charset=utf-8,' +
|
|
|
|
encodeURIComponent(cssText)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-06-29 08:46:20 -06:00
|
|
|
vAPI.messaging = {
|
2017-11-20 06:42:32 -07:00
|
|
|
channels: new Map(),
|
|
|
|
pending: new Map(),
|
2015-06-29 08:46:20 -06:00
|
|
|
auxProcessId: 1,
|
|
|
|
connected: false,
|
2016-03-06 08:51:06 -07:00
|
|
|
|
|
|
|
messageListener: function(msg) {
|
|
|
|
var details = JSON.parse(msg);
|
2017-11-20 06:42:32 -07:00
|
|
|
if ( !details ) { return; }
|
2016-03-06 08:51:06 -07:00
|
|
|
|
|
|
|
// Sent to all channels
|
|
|
|
if ( details.broadcast && !details.channelName ) {
|
2017-11-20 06:42:32 -07:00
|
|
|
for ( var channelName of this.channels.keys() ) {
|
2016-03-06 08:51:06 -07:00
|
|
|
this.sendToChannelListeners(channelName, details.msg);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response to specific message previously sent
|
|
|
|
if ( details.auxProcessId ) {
|
2017-11-20 06:42:32 -07:00
|
|
|
var listener = this.pending.get(details.auxProcessId);
|
|
|
|
this.pending.delete(details.auxProcessId);
|
2016-03-06 08:51:06 -07:00
|
|
|
if ( listener ) {
|
|
|
|
listener(details.msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sent to a specific channel
|
2017-11-20 06:42:32 -07:00
|
|
|
this.sendToChannelListeners(details.channelName, details.msg);
|
2015-06-29 08:46:20 -06:00
|
|
|
},
|
2015-06-25 22:08:41 -06:00
|
|
|
|
2016-03-06 08:51:06 -07:00
|
|
|
builtinListener: function(msg) {
|
2015-07-03 05:00:29 -06:00
|
|
|
if ( msg.cmd === 'injectScript' ) {
|
2015-07-03 11:32:00 -06:00
|
|
|
// injectScript is not always present.
|
|
|
|
// - See contentObserver.initContentScripts in frameModule.js
|
2017-11-20 06:42:32 -07:00
|
|
|
if ( typeof self.injectScript !== 'function' ) { return; }
|
2015-07-03 05:00:29 -06:00
|
|
|
var details = msg.details;
|
2016-03-07 07:55:04 -07:00
|
|
|
// Whether to inject in all child frames. Default to only top frame.
|
|
|
|
var allFrames = details.allFrames || false;
|
2017-11-20 06:42:32 -07:00
|
|
|
if ( allFrames !== true && window !== window.top ) { return; }
|
2015-10-28 07:35:13 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/876
|
|
|
|
// Enforce `details.runAt`. Default to `document_end`.
|
2016-03-07 07:55:04 -07:00
|
|
|
var runAt = details.runAt || 'document_end';
|
|
|
|
if ( runAt === 'document_start' || document.readyState !== 'loading' ) {
|
2015-10-28 07:35:13 -06:00
|
|
|
self.injectScript(details.file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var injectScriptDelayed = function() {
|
|
|
|
document.removeEventListener('DOMContentLoaded', injectScriptDelayed);
|
|
|
|
self.injectScript(details.file);
|
|
|
|
};
|
|
|
|
document.addEventListener('DOMContentLoaded', injectScriptDelayed);
|
2015-07-03 05:00:29 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( msg.cmd === 'shutdownSandbox' ) {
|
|
|
|
vAPI.shutdown.exec();
|
2017-11-20 06:42:32 -07:00
|
|
|
vAPI.messaging.stop();
|
2015-07-03 11:32:00 -06:00
|
|
|
if ( typeof self.outerShutdown === 'function' ) {
|
|
|
|
outerShutdown();
|
|
|
|
}
|
2016-04-21 12:34:20 -06: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 05:00:29 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-06 08:51:06 -07: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 08:46:20 -06:00
|
|
|
},
|
2016-03-06 08:51:06 -07:00
|
|
|
toggleListenerCallback: null,
|
2015-06-25 22:08:41 -06:00
|
|
|
|
2016-03-06 08:51:06 -07:00
|
|
|
start: function() {
|
2017-11-20 06:42:32 -07:00
|
|
|
this.addChannelListener('vAPI', this.builtinListener);
|
2016-03-06 08:51:06 -07:00
|
|
|
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 11:32:00 -06:00
|
|
|
},
|
|
|
|
|
2016-03-06 08:51:06 -07:00
|
|
|
stop: function() {
|
|
|
|
if ( this.toggleListenerCallback !== null ) {
|
|
|
|
window.removeEventListener('pagehide', this.toggleListenerCallback, true);
|
|
|
|
window.removeEventListener('pageshow', this.toggleListenerCallback, true);
|
|
|
|
}
|
2015-07-03 05:00:29 -06:00
|
|
|
this.disconnect();
|
2017-11-20 06:42:32 -07:00
|
|
|
this.channels.clear();
|
2015-07-07 09:03:26 -06:00
|
|
|
// service pending callbacks
|
2017-11-20 06:42:32 -07:00
|
|
|
var pending = this.pending;
|
|
|
|
this.pending = new Map();
|
|
|
|
for ( var callback of pending.values() ) {
|
2016-03-09 15:13:22 -07:00
|
|
|
if ( typeof callback === 'function' ) {
|
|
|
|
callback(null);
|
2015-07-07 09:03:26 -06:00
|
|
|
}
|
|
|
|
}
|
2015-06-29 08:46:20 -06:00
|
|
|
},
|
2015-06-25 22:08:41 -06:00
|
|
|
|
2015-07-03 05:00:29 -06:00
|
|
|
connect: function() {
|
|
|
|
if ( !this.connected ) {
|
2016-03-06 08:51:06 -07:00
|
|
|
addMessageListener(this.messageListener.bind(this));
|
2015-07-03 05:00:29 -06:00
|
|
|
this.connected = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
disconnect: function() {
|
|
|
|
if ( this.connected ) {
|
|
|
|
removeMessageListener();
|
|
|
|
this.connected = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-03-06 08:51:06 -07:00
|
|
|
send: function(channelName, message, callback) {
|
2017-10-21 11:43:46 -06:00
|
|
|
// User stylesheets are handled content-side on legacy Firefox.
|
2017-11-20 06:42:32 -07:00
|
|
|
if ( channelName === 'vapi' && message.what === 'userCSS' ) {
|
2017-10-21 11:43:46 -06:00
|
|
|
return processUserCSS(message, callback);
|
|
|
|
}
|
2016-03-06 08:51:06 -07:00
|
|
|
if ( !this.connected ) {
|
2017-11-20 06:42:32 -07:00
|
|
|
if ( typeof callback === 'function' ) { callback(); }
|
2015-06-29 08:46:20 -06:00
|
|
|
return;
|
|
|
|
}
|
2016-03-06 08:51:06 -07: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 12:00:27 -07:00
|
|
|
}
|
2016-03-06 08:51:06 -07:00
|
|
|
this.connect();
|
|
|
|
var auxProcessId;
|
|
|
|
if ( callback ) {
|
|
|
|
auxProcessId = this.auxProcessId++;
|
2017-11-20 06:42:32 -07:00
|
|
|
this.pending.set(auxProcessId, callback);
|
2015-06-25 22:08:41 -06:00
|
|
|
}
|
2015-06-29 08:46:20 -06:00
|
|
|
sendAsyncMessage('ublock0:background', {
|
2016-03-06 08:51:06 -07:00
|
|
|
channelName: self._sandboxId_ + '|' + channelName,
|
|
|
|
auxProcessId: auxProcessId,
|
|
|
|
msg: message
|
2015-06-29 08:46:20 -06:00
|
|
|
});
|
2016-03-06 08:51:06 -07:00
|
|
|
},
|
2015-06-29 08:46:20 -06:00
|
|
|
|
2017-11-20 06:42:32 -07:00
|
|
|
// TODO: implement as time permits.
|
|
|
|
connectTo: function(from, to, handler) {
|
|
|
|
handler({
|
|
|
|
what: 'connectionRefused',
|
|
|
|
from: from,
|
|
|
|
to: to
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
disconnectFrom: function() {
|
|
|
|
},
|
|
|
|
|
|
|
|
sendTo: function() {
|
|
|
|
},
|
|
|
|
|
|
|
|
addChannelListener: function(channelName, listener) {
|
|
|
|
var listeners = this.channels.get(channelName);
|
2016-03-06 08:51:06 -07:00
|
|
|
if ( listeners === undefined ) {
|
2017-11-20 06:42:32 -07:00
|
|
|
this.channels.set(channelName, [ listener ]);
|
|
|
|
} else if ( listeners.indexOf(listener) === -1 ) {
|
|
|
|
listeners.push(listener);
|
2016-03-06 08:51:06 -07:00
|
|
|
}
|
|
|
|
this.connect();
|
|
|
|
},
|
2015-06-25 22:08:41 -06:00
|
|
|
|
2017-11-20 06:42:32 -07:00
|
|
|
removeChannelListener: function(channelName, listener) {
|
|
|
|
var listeners = this.channels.get(channelName);
|
|
|
|
if ( listeners === undefined ) { return; }
|
|
|
|
var pos = this.listeners.indexOf(listener);
|
|
|
|
if ( pos === -1 ) { return; }
|
2016-03-06 08:51:06 -07:00
|
|
|
listeners.splice(pos, 1);
|
|
|
|
if ( listeners.length === 0 ) {
|
2017-11-20 06:42:32 -07:00
|
|
|
this.channels.delete(channelName);
|
2016-03-06 08:51:06 -07:00
|
|
|
}
|
|
|
|
},
|
2014-11-24 12:00:27 -07:00
|
|
|
|
2016-03-06 08:51:06 -07:00
|
|
|
removeAllChannelListeners: function(channelName) {
|
2017-11-20 06:42:32 -07:00
|
|
|
this.channels.delete(channelName);
|
2016-03-06 08:51:06 -07:00
|
|
|
},
|
2015-06-28 15:42:08 -06:00
|
|
|
|
2016-03-06 08:51:06 -07:00
|
|
|
sendToChannelListeners: function(channelName, msg) {
|
2017-11-20 06:42:32 -07:00
|
|
|
var listeners = this.channels.get(channelName);
|
2017-10-21 11:43:46 -06:00
|
|
|
if ( listeners === undefined ) { return; }
|
|
|
|
listeners = listeners.slice(0);
|
2016-03-06 08:51:06 -07:00
|
|
|
var response;
|
2017-10-21 11:43:46 -06:00
|
|
|
for ( var listener of listeners ) {
|
|
|
|
response = listener(msg);
|
|
|
|
if ( response !== undefined ) { break; }
|
2015-01-26 12:26:45 -07:00
|
|
|
}
|
2016-03-06 08:51:06 -07:00
|
|
|
return response;
|
2015-01-08 13:18:05 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-06 08:51:06 -07:00
|
|
|
vAPI.messaging.start();
|
2015-01-08 13:18:05 -07:00
|
|
|
|
2016-03-06 08:51:06 -07:00
|
|
|
// https://www.youtube.com/watch?v=Cg0cmhjdiLs
|
2015-07-03 05:00:29 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2016-08-31 15:50:49 -06:00
|
|
|
|
2016-12-21 09:44:03 -07:00
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=444165
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2256
|
|
|
|
// Not the prettiest solution, but that's the safest/simplest I can think
|
|
|
|
// of at this point. If/when bugzilla issue above is solved, we will need
|
|
|
|
// version detection to decide whether the patch needs to be applied.
|
|
|
|
|
|
|
|
vAPI.iframeLoadEventPatch = function(target) {
|
|
|
|
if ( target.localName === 'iframe' ) {
|
|
|
|
target.dispatchEvent(new Event('load'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-04-01 10:14:44 -06:00
|
|
|
})(this);
|
2014-11-24 12:00:27 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|