mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-10 09:07:54 +01:00
new WebExtensions platform: starting point = Chromium platform
This commit is contained in:
parent
afc13b9953
commit
607e44b2d6
14 changed files with 2457 additions and 0 deletions
BIN
platform/webext/img/browsericons/icon19-off.png
Normal file
BIN
platform/webext/img/browsericons/icon19-off.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 954 B |
BIN
platform/webext/img/browsericons/icon19.png
Normal file
BIN
platform/webext/img/browsericons/icon19.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 995 B |
BIN
platform/webext/img/browsericons/icon38-off.png
Normal file
BIN
platform/webext/img/browsericons/icon38-off.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.7 KiB |
BIN
platform/webext/img/browsericons/icon38.png
Normal file
BIN
platform/webext/img/browsericons/icon38.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.8 KiB |
8
platform/webext/is-webrtc-supported.html
Normal file
8
platform/webext/is-webrtc-supported.html
Normal file
|
@ -0,0 +1,8 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title></title>
|
||||
<script async src="js/is-webrtc-supported.js"></script>
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
52
platform/webext/is-webrtc-supported.js
Normal file
52
platform/webext/is-webrtc-supported.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*******************************************************************************
|
||||
|
||||
uBlock Origin - a browser extension to block requests.
|
||||
Copyright (C) 2015 Raymond Hill
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
// https://github.com/gorhill/uBlock/issues/533#issuecomment-164292868
|
||||
// If WebRTC is supported, there won't be an exception if we
|
||||
// try to instanciate a peer connection object.
|
||||
|
||||
// https://github.com/gorhill/uBlock/issues/533#issuecomment-168097594
|
||||
// Because Chromium leaks WebRTC connections after they have been closed
|
||||
// and forgotten, we need to test for WebRTC support inside an iframe, this
|
||||
// way the closed and forgottetn WebRTC connections are properly garbage
|
||||
// collected.
|
||||
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
var pc = null;
|
||||
try {
|
||||
var PC = self.RTCPeerConnection || self.webkitRTCPeerConnection;
|
||||
if ( PC ) {
|
||||
pc = new PC(null);
|
||||
}
|
||||
} catch (ex) {
|
||||
console.error(ex);
|
||||
}
|
||||
if ( pc !== null ) {
|
||||
pc.close();
|
||||
}
|
||||
|
||||
window.top.postMessage(
|
||||
pc !== null ? 'webRTCSupported' : 'webRTCNotSupported',
|
||||
window.location.origin
|
||||
);
|
||||
})();
|
11
platform/webext/managed_storage.json
Normal file
11
platform/webext/managed_storage.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-03/schema#",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"adminSettings": {
|
||||
"title": "A valid JSON string compliant with uBO's backup format.",
|
||||
"description": "All entries present will overwrite local settings.",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
64
platform/webext/manifest.json
Normal file
64
platform/webext/manifest.json
Normal file
|
@ -0,0 +1,64 @@
|
|||
{
|
||||
"manifest_version": 2,
|
||||
|
||||
"name": "uBlock Origin",
|
||||
"version": "1.9.15.3",
|
||||
|
||||
"default_locale": "en",
|
||||
"description": "__MSG_extShortDesc__",
|
||||
"icons": {
|
||||
"16": "img/icon_16.png",
|
||||
"128": "img/icon_128.png"
|
||||
},
|
||||
|
||||
"browser_action": {
|
||||
"default_icon": {
|
||||
"19": "img/browsericons/icon19.png",
|
||||
"38": "img/browsericons/icon38.png"
|
||||
},
|
||||
"default_title": "uBlock Origin",
|
||||
"default_popup": "popup.html"
|
||||
},
|
||||
|
||||
"author": "All uBlock Origin contributors",
|
||||
"background": {
|
||||
"page": "background.html"
|
||||
},
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": ["http://*/*", "https://*/*"],
|
||||
"js": ["js/vapi-client.js", "js/contentscript.js"],
|
||||
"run_at": "document_start",
|
||||
"all_frames": true
|
||||
},
|
||||
{
|
||||
"matches": ["http://*/*", "https://*/*"],
|
||||
"js": ["js/scriptlets/subscriber.js"],
|
||||
"run_at": "document_idle",
|
||||
"all_frames": false
|
||||
}
|
||||
],
|
||||
"incognito": "split",
|
||||
"minimum_chrome_version": "26.0",
|
||||
"optional_permissions": [ "file:///*" ],
|
||||
"options_page": "dashboard.html",
|
||||
"options_ui": {
|
||||
"page": "options_ui.html"
|
||||
},
|
||||
"permissions": [
|
||||
"contextMenus",
|
||||
"privacy",
|
||||
"storage",
|
||||
"tabs",
|
||||
"unlimitedStorage",
|
||||
"webNavigation",
|
||||
"webRequest",
|
||||
"webRequestBlocking",
|
||||
"http://*/*",
|
||||
"https://*/*"
|
||||
],
|
||||
"short_name": "uBlock₀",
|
||||
"storage": {
|
||||
"managed_schema": "managed_storage.json"
|
||||
}
|
||||
}
|
11
platform/webext/options_ui.html
Normal file
11
platform/webext/options_ui.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="js/vapi-client.js"></script>
|
||||
<script src="js/options_ui.js"></script>
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
<a href="dashboard.html" target="uBO">Dashboard</a>
|
||||
</body>
|
||||
</html>
|
47
platform/webext/options_ui.js
Normal file
47
platform/webext/options_ui.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
/*******************************************************************************
|
||||
|
||||
µBlock - a browser extension to block requests.
|
||||
Copyright (C) 2015 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
|
||||
*/
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
(function() {
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
'use strict';
|
||||
|
||||
vAPI.messaging.send(
|
||||
'default',
|
||||
{
|
||||
what: 'gotoURL',
|
||||
details: {
|
||||
url: 'dashboard.html',
|
||||
select: true,
|
||||
index: -1
|
||||
}
|
||||
}
|
||||
);
|
||||
window.close();
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
})();
|
||||
|
||||
/******************************************************************************/
|
214
platform/webext/polyfill.js
Normal file
214
platform/webext/polyfill.js
Normal file
|
@ -0,0 +1,214 @@
|
|||
/*******************************************************************************
|
||||
|
||||
uBlock Origin - a browser extension to block requests.
|
||||
Copyright (C) 2014-2016 The uBlock Origin 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
|
||||
*/
|
||||
|
||||
// For background page or non-background pages
|
||||
|
||||
'use strict';
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
// https://github.com/gorhill/uBlock/issues/1067
|
||||
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
|
||||
// Firefox 17/Chromium 41 supports `startsWith`.
|
||||
|
||||
if ( String.prototype.startsWith instanceof Function === false ) {
|
||||
String.prototype.startsWith = function(needle, pos) {
|
||||
if ( typeof pos !== 'number' ) {
|
||||
pos = 0;
|
||||
}
|
||||
return this.lastIndexOf(needle, pos) === pos;
|
||||
};
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// https://github.com/gorhill/uBlock/issues/1067
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
|
||||
// Firefox 17/Chromium 41 supports `endsWith`.
|
||||
|
||||
if ( String.prototype.endsWith instanceof Function === false ) {
|
||||
String.prototype.endsWith = function(needle, pos) {
|
||||
if ( typeof pos !== 'number' ) {
|
||||
pos = this.length;
|
||||
}
|
||||
pos -= needle.length;
|
||||
return this.indexOf(needle, pos) === pos;
|
||||
};
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// https://github.com/gorhill/uBlock/issues/1070
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#Browser_compatibility
|
||||
// This polyfill is designed to fulfill *only* what uBlock Origin needs -- this
|
||||
// is not an accurate API of the real Set() type.
|
||||
|
||||
if ( self.Set instanceof Function === false ) {
|
||||
self.Set = function(iter) {
|
||||
this.clear();
|
||||
if ( Array.isArray(iter) ) {
|
||||
for ( var i = 0, n = iter.length; i < n; i++ ) {
|
||||
this.add(iter[i]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
self.Set.polyfill = true;
|
||||
|
||||
self.Set.prototype.clear = function() {
|
||||
this._set = Object.create(null);
|
||||
this.size = 0;
|
||||
// Iterator stuff
|
||||
this._values = undefined;
|
||||
this._i = undefined;
|
||||
this.value = undefined;
|
||||
this.done = true;
|
||||
};
|
||||
|
||||
self.Set.prototype.add = function(k) {
|
||||
if ( this._set[k] === undefined ) {
|
||||
this._set[k] = true;
|
||||
this.size += 1;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
self.Set.prototype.delete = function(k) {
|
||||
if ( this._set[k] !== undefined ) {
|
||||
delete this._set[k];
|
||||
this.size -= 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
self.Set.prototype.has = function(k) {
|
||||
return this._set[k] !== undefined;
|
||||
};
|
||||
|
||||
self.Set.prototype.next = function() {
|
||||
if ( this._i < this.size ) {
|
||||
this.value = this._values[this._i++];
|
||||
} else {
|
||||
this._values = undefined;
|
||||
this.value = undefined;
|
||||
this.done = true;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
self.Set.prototype.values = function() {
|
||||
this._values = Object.keys(this._set);
|
||||
this._i = 0;
|
||||
this.value = undefined;
|
||||
this.done = false;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// https://github.com/gorhill/uBlock/issues/1070
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#Browser_compatibility
|
||||
// This polyfill is designed to fulfill *only* what uBlock Origin needs -- this
|
||||
// is not an accurate API of the real Map() type.
|
||||
|
||||
if ( self.Map instanceof Function === false ) {
|
||||
self.Map = function(iter) {
|
||||
this.clear();
|
||||
if ( Array.isArray(iter) ) {
|
||||
for ( var i = 0, n = iter.length, entry; i < n; i++ ) {
|
||||
entry = iter[i];
|
||||
this.set(entry[0], entry[1]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
self.Map.polyfill = true;
|
||||
|
||||
self.Map.prototype.clear = function() {
|
||||
this._map = Object.create(null);
|
||||
this.size = 0;
|
||||
// Iterator stuff
|
||||
this._keys = undefined;
|
||||
this._i = undefined;
|
||||
this.value = undefined;
|
||||
this.done = true;
|
||||
};
|
||||
|
||||
self.Map.prototype.delete = function(k) {
|
||||
if ( this._map[k] !== undefined ) {
|
||||
delete this._map[k];
|
||||
this.size -= 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
self.Map.prototype.entries = function() {
|
||||
this._keys = Object.keys(this._map);
|
||||
this._i = 0;
|
||||
this.value = [ undefined, undefined ];
|
||||
this.done = false;
|
||||
return this;
|
||||
};
|
||||
|
||||
self.Map.prototype.get = function(k) {
|
||||
return this._map[k];
|
||||
};
|
||||
|
||||
self.Map.prototype.has = function(k) {
|
||||
return this._map[k] !== undefined;
|
||||
};
|
||||
|
||||
self.Map.prototype.next = function() {
|
||||
if ( this._i < this.size ) {
|
||||
var key = this._keys[this._i++];
|
||||
this.value[0] = key;
|
||||
this.value[1] = this._map[key];
|
||||
} else {
|
||||
this._keys = undefined;
|
||||
this.value = undefined;
|
||||
this.done = true;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
self.Map.prototype.set = function(k, v) {
|
||||
if ( v !== undefined ) {
|
||||
if ( this._map[k] === undefined ) {
|
||||
this.size += 1;
|
||||
}
|
||||
this._map[k] = v;
|
||||
} else {
|
||||
if ( this._map[k] !== undefined ) {
|
||||
this.size -= 1;
|
||||
}
|
||||
delete this._map[k];
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
1424
platform/webext/vapi-background.js
Normal file
1424
platform/webext/vapi-background.js
Normal file
File diff suppressed because it is too large
Load diff
525
platform/webext/vapi-client.js
Normal file
525
platform/webext/vapi-client.js
Normal file
|
@ -0,0 +1,525 @@
|
|||
/*******************************************************************************
|
||||
|
||||
uBlock Origin - a browser extension to block requests.
|
||||
Copyright (C) 2014-2016 The uBlock Origin 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
|
||||
*/
|
||||
|
||||
/* global HTMLDocument, XMLDocument */
|
||||
|
||||
'use strict';
|
||||
|
||||
// For non background pages
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
(function(self) {
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// https://github.com/gorhill/uBlock/issues/1124
|
||||
// Looks like `contentType` is on track to be standardized:
|
||||
// https://dom.spec.whatwg.org/#concept-document-content-type
|
||||
// https://forums.lanik.us/viewtopic.php?f=64&t=31522
|
||||
// Skip text/plain documents.
|
||||
var contentType = document.contentType || '';
|
||||
if ( /^image\/|^text\/plain/.test(contentType) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
var vAPI = self.vAPI = self.vAPI || {};
|
||||
var chrome = self.chrome;
|
||||
|
||||
// https://github.com/chrisaljoudi/uBlock/issues/456
|
||||
// Already injected?
|
||||
if ( vAPI.sessionId ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// Support minimally working Set() for legacy Chromium.
|
||||
|
||||
if ( self.Set instanceof Function ) {
|
||||
self.createSet = function() {
|
||||
return new Set();
|
||||
};
|
||||
} else {
|
||||
self.createSet = (function() {
|
||||
//console.log('Polyfilling for ES6-like Set().');
|
||||
var PrimitiveSet = function() {
|
||||
this.clear();
|
||||
};
|
||||
PrimitiveSet.prototype = {
|
||||
add: function(k) {
|
||||
if ( this._set[k] === undefined ) {
|
||||
this._set[k] = true;
|
||||
this.size += 1;
|
||||
}
|
||||
return this;
|
||||
},
|
||||
clear: function() {
|
||||
this._set = Object.create(null);
|
||||
this.size = 0;
|
||||
this._values = undefined;
|
||||
this._i = undefined;
|
||||
this.value = undefined;
|
||||
this.done = true;
|
||||
},
|
||||
delete: function(k) {
|
||||
if ( this._set[k] === undefined ) { return false; }
|
||||
delete this._set[k];
|
||||
this.size -= 1;
|
||||
return true;
|
||||
},
|
||||
has: function(k) {
|
||||
return this._set[k] !== undefined;
|
||||
},
|
||||
next: function() {
|
||||
if ( this._i < this.size ) {
|
||||
this.value = this._values[this._i++];
|
||||
} else {
|
||||
this._values = undefined;
|
||||
this.value = undefined;
|
||||
this.done = true;
|
||||
}
|
||||
return this;
|
||||
},
|
||||
polyfill: true,
|
||||
values: function() {
|
||||
this._values = Object.keys(this._set);
|
||||
this._i = 0;
|
||||
this.value = undefined;
|
||||
this.done = false;
|
||||
return this;
|
||||
}
|
||||
};
|
||||
var ReferenceSet = function() {
|
||||
this.clear();
|
||||
};
|
||||
ReferenceSet.prototype = {
|
||||
add: function(k) {
|
||||
if ( this._set.indexOf(k) === -1 ) {
|
||||
this._set.push(k);
|
||||
}
|
||||
},
|
||||
clear: function() {
|
||||
this._set = [];
|
||||
this._i = 0;
|
||||
this.value = undefined;
|
||||
this.done = true;
|
||||
},
|
||||
delete: function(k) {
|
||||
var pos = this._set.indexOf(k);
|
||||
if ( pos === -1 ) { return false; }
|
||||
this._set.splice(pos, 1);
|
||||
return true;
|
||||
},
|
||||
has: function(k) {
|
||||
return this._set.indexOf(k) !== -1;
|
||||
},
|
||||
next: function() {
|
||||
if ( this._i === this._set.length ) {
|
||||
this.value = undefined;
|
||||
this.done = true;
|
||||
} else {
|
||||
this.value = this._set[this._i];
|
||||
this._i += 1;
|
||||
}
|
||||
return this;
|
||||
},
|
||||
polyfill: true,
|
||||
values: function() {
|
||||
this._i = 0;
|
||||
this.done = false;
|
||||
return this;
|
||||
}
|
||||
};
|
||||
Object.defineProperty(ReferenceSet.prototype, 'size', {
|
||||
get: function() { return this._set.length; }
|
||||
});
|
||||
return function(type) {
|
||||
return type === 'object' ? new ReferenceSet() : new PrimitiveSet();
|
||||
};
|
||||
})();
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
var referenceCounter = 0;
|
||||
|
||||
vAPI.lock = function() {
|
||||
referenceCounter += 1;
|
||||
};
|
||||
|
||||
vAPI.unlock = function() {
|
||||
referenceCounter -= 1;
|
||||
if ( referenceCounter === 0 ) {
|
||||
// Eventually there will be code here to flush the javascript code
|
||||
// from this file out of memory when it ends up unused.
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.executionCost = {
|
||||
start: function(){},
|
||||
stop: function(){}
|
||||
};
|
||||
/*
|
||||
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 (%s): %sms/%s%% (%s: %sms)',
|
||||
context,
|
||||
this.tcost.toFixed(1),
|
||||
percent.toFixed(1),
|
||||
mark,
|
||||
tcost.toFixed(2)
|
||||
);
|
||||
}
|
||||
};
|
||||
*/
|
||||
vAPI.executionCost.start();
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.randomToken = function() {
|
||||
return String.fromCharCode(Date.now() % 26 + 97) +
|
||||
Math.floor(Math.random() * 982451653 + 982451653).toString(36);
|
||||
};
|
||||
|
||||
vAPI.sessionId = vAPI.randomToken();
|
||||
vAPI.chrome = true;
|
||||
vAPI.setTimeout = vAPI.setTimeout || self.setTimeout.bind(self);
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.shutdown = {
|
||||
jobs: [],
|
||||
add: function(job) {
|
||||
this.jobs.push(job);
|
||||
},
|
||||
exec: function() {
|
||||
var job;
|
||||
while ( (job = this.jobs.pop()) ) {
|
||||
job();
|
||||
}
|
||||
},
|
||||
remove: function(job) {
|
||||
var pos;
|
||||
while ( (pos = this.jobs.indexOf(job)) !== -1 ) {
|
||||
this.jobs.splice(pos, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.messaging = {
|
||||
port: null,
|
||||
portTimer: null,
|
||||
portTimerDelay: 10000,
|
||||
channels: Object.create(null),
|
||||
channelCount: 0,
|
||||
pending: Object.create(null),
|
||||
pendingCount: 0,
|
||||
auxProcessId: 1,
|
||||
shuttingDown: false,
|
||||
|
||||
shutdown: function() {
|
||||
this.shuttingDown = true;
|
||||
this.destroyPort();
|
||||
},
|
||||
|
||||
disconnectListener: function() {
|
||||
this.port = null;
|
||||
vAPI.shutdown.exec();
|
||||
},
|
||||
disconnectListenerCallback: null,
|
||||
|
||||
messageListener: function(details) {
|
||||
if ( !details ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Sent to all channels
|
||||
if ( details.broadcast === true && !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;
|
||||
}
|
||||
var port = this.connect();
|
||||
if ( port !== null ) {
|
||||
port.postMessage({
|
||||
mainProcessId: details.mainProcessId,
|
||||
msg: response
|
||||
});
|
||||
}
|
||||
},
|
||||
messageListenerCallback: null,
|
||||
|
||||
portPoller: function() {
|
||||
this.portTimer = null;
|
||||
if ( this.port !== null ) {
|
||||
if ( this.channelCount !== 0 || this.pendingCount !== 0 ) {
|
||||
this.portTimer = vAPI.setTimeout(this.portPollerCallback, this.portTimerDelay);
|
||||
this.portTimerDelay = Math.min(this.portTimerDelay * 2, 60 * 60 * 1000);
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.destroyPort();
|
||||
},
|
||||
portPollerCallback: null,
|
||||
|
||||
destroyPort: function() {
|
||||
if ( this.portTimer !== null ) {
|
||||
clearTimeout(this.portTimer);
|
||||
this.portTimer = null;
|
||||
}
|
||||
var port = this.port;
|
||||
if ( port !== null ) {
|
||||
port.disconnect();
|
||||
port.onMessage.removeListener(this.messageListenerCallback);
|
||||
port.onDisconnect.removeListener(this.disconnectListenerCallback);
|
||||
this.port = null;
|
||||
}
|
||||
if ( this.channelCount !== 0 ) {
|
||||
this.channels = Object.create(null);
|
||||
this.channelCount = 0;
|
||||
}
|
||||
// service pending callbacks
|
||||
if ( this.pendingCount !== 0 ) {
|
||||
var pending = this.pending, callback;
|
||||
this.pending = Object.create(null);
|
||||
this.pendingCount = 0;
|
||||
for ( var auxId in pending ) {
|
||||
callback = pending[auxId];
|
||||
if ( typeof callback === 'function' ) {
|
||||
callback(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
createPort: function() {
|
||||
if ( this.shuttingDown ) {
|
||||
return null;
|
||||
}
|
||||
if ( this.messageListenerCallback === null ) {
|
||||
this.messageListenerCallback = this.messageListener.bind(this);
|
||||
this.disconnectListenerCallback = this.disconnectListener.bind(this);
|
||||
this.portPollerCallback = this.portPoller.bind(this);
|
||||
}
|
||||
try {
|
||||
this.port = chrome.runtime.connect({name: vAPI.sessionId}) || null;
|
||||
} catch (ex) {
|
||||
this.port = null;
|
||||
}
|
||||
if ( this.port !== null ) {
|
||||
this.port.onMessage.addListener(this.messageListenerCallback);
|
||||
this.port.onDisconnect.addListener(this.disconnectListenerCallback);
|
||||
}
|
||||
this.portTimerDelay = 10000;
|
||||
if ( this.portTimer === null ) {
|
||||
this.portTimer = vAPI.setTimeout(this.portPollerCallback, this.portTimerDelay);
|
||||
}
|
||||
return this.port;
|
||||
},
|
||||
|
||||
connect: function() {
|
||||
return this.port !== null ? this.port : this.createPort();
|
||||
},
|
||||
|
||||
send: function(channelName, message, callback) {
|
||||
this.sendTo(channelName, message, undefined, undefined, callback);
|
||||
},
|
||||
|
||||
sendTo: function(channelName, message, toTabId, toChannel, callback) {
|
||||
// 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();
|
||||
}
|
||||
var port = this.connect();
|
||||
if ( port === null ) {
|
||||
if ( typeof callback === 'function' ) {
|
||||
callback();
|
||||
}
|
||||
return;
|
||||
}
|
||||
var auxProcessId;
|
||||
if ( callback ) {
|
||||
auxProcessId = this.auxProcessId++;
|
||||
this.pending[auxProcessId] = callback;
|
||||
this.pendingCount += 1;
|
||||
}
|
||||
port.postMessage({
|
||||
channelName: channelName,
|
||||
auxProcessId: auxProcessId,
|
||||
toTabId: toTabId,
|
||||
toChannel: toChannel,
|
||||
msg: message
|
||||
});
|
||||
},
|
||||
|
||||
addChannelListener: function(channelName, callback) {
|
||||
if ( typeof callback !== 'function' ) {
|
||||
return;
|
||||
}
|
||||
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();
|
||||
},
|
||||
|
||||
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;
|
||||
}
|
||||
},
|
||||
|
||||
removeAllChannelListeners: function(channelName) {
|
||||
var listeners = this.channels[channelName];
|
||||
if ( listeners === undefined ) {
|
||||
return;
|
||||
}
|
||||
delete this.channels[channelName];
|
||||
this.channelCount -= 1;
|
||||
},
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.shutdown.add(function() {
|
||||
vAPI.messaging.shutdown();
|
||||
delete window.vAPI;
|
||||
});
|
||||
|
||||
// https://www.youtube.com/watch?v=rT5zCHn0tsg
|
||||
// https://www.youtube.com/watch?v=E-jS4e3zacI
|
||||
|
||||
vAPI.executionCost.stop('vapi-client.js');
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
})(this);
|
||||
|
||||
/******************************************************************************/
|
101
platform/webext/vapi-common.js
Normal file
101
platform/webext/vapi-common.js
Normal file
|
@ -0,0 +1,101 @@
|
|||
/*******************************************************************************
|
||||
|
||||
uBlock Origin - a browser extension to block requests.
|
||||
Copyright (C) 2014-2016 The uBlock Origin 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
|
||||
*/
|
||||
|
||||
// For background page or non-background pages
|
||||
|
||||
'use strict';
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
(function() {
|
||||
|
||||
var vAPI = self.vAPI = self.vAPI || {};
|
||||
var chrome = self.chrome;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.setTimeout = vAPI.setTimeout || self.setTimeout.bind(self);
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// http://www.w3.org/International/questions/qa-scripts#directions
|
||||
|
||||
var setScriptDirection = function(language) {
|
||||
document.body.setAttribute(
|
||||
'dir',
|
||||
['ar', 'he', 'fa', 'ps', 'ur'].indexOf(language) !== -1 ? 'rtl' : 'ltr'
|
||||
);
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.download = function(details) {
|
||||
if ( !details.url ) {
|
||||
return;
|
||||
}
|
||||
|
||||
var a = document.createElement('a');
|
||||
a.href = details.url;
|
||||
a.setAttribute('download', details.filename || '');
|
||||
a.dispatchEvent(new MouseEvent('click'));
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.insertHTML = function(node, html) {
|
||||
node.innerHTML = html;
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.getURL = chrome.runtime.getURL;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.i18n = chrome.i18n.getMessage;
|
||||
|
||||
setScriptDirection(vAPI.i18n('@@ui_locale'));
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.closePopup = function() {
|
||||
window.open('','_self').close();
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// A localStorage-like object which should be accessible from the
|
||||
// background page or auxiliary pages.
|
||||
// This storage is optional, but it is nice to have, for a more polished user
|
||||
// experience.
|
||||
|
||||
// This can throw in some contexts (like in devtool).
|
||||
try {
|
||||
vAPI.localStorage = window.localStorage;
|
||||
} catch (ex) {
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
})();
|
||||
|
||||
/******************************************************************************/
|
Loading…
Reference in a new issue