Browse Source

Bring uMatrix up to date

Notably:
- Import logger improvements from uBO
- Import CNAME uncloaking from uBO
- Import more improvements from uBO
- Make use of modern JS features

This should un-stall further development of uMatrix.
pull/1014/head
Raymond Hill 3 years ago
parent
commit
9b292304d3
No known key found for this signature in database GPG Key ID: 25E1490B761470C2
  1. 4
      .jshintrc
  2. 2
      dist/version
  3. 2
      platform/chromium/manifest.json
  4. 1841
      platform/chromium/vapi-background.js
  5. 308
      platform/chromium/vapi-client-extra.js
  6. 273
      platform/chromium/vapi-client.js
  7. 237
      platform/chromium/vapi-common.js
  8. 230
      platform/chromium/vapi-webrequest.js
  9. 86
      platform/chromium/vapi.js
  10. 176
      platform/chromium/webext.js
  11. 1
      platform/firefox/manifest.json
  12. 263
      platform/firefox/vapi-cachestorage.js
  13. 316
      platform/firefox/vapi-webrequest.js
  14. 24
      platform/firefox/webext.js
  15. 187
      src/_locales/en/messages.json
  16. 7
      src/about.html
  17. 33
      src/asset-viewer.html
  18. 23
      src/background.html
  19. 68
      src/css/codemirror.css
  20. 34
      src/css/common.css
  21. 6
      src/css/dashboard.css
  22. 778
      src/css/logger-ui.css
  23. 62
      src/css/popup.css
  24. 22
      src/css/raw-settings.css
  25. 9
      src/dashboard.html
  26. 2
      src/hosts-files.html
  27. 3
      src/img/fontawesome/fontawesome-defs.svg
  28. 124
      src/js/about.js
  29. 51
      src/js/asset-viewer.js
  30. 1113
      src/js/assets.js
  31. 49
      src/js/background.js
  32. 25
      src/js/browsercache.js
  33. 465
      src/js/cachestorage.js
  34. 104
      src/js/cloud-ui.js
  35. 37
      src/js/codemirror/mode/raw-settings.js
  36. 336
      src/js/codemirror/search.js
  37. 34
      src/js/console.js
  38. 39
      src/js/contentscript-start.js
  39. 192
      src/js/contentscript.js
  40. 293
      src/js/cookies.js
  41. 133
      src/js/dashboard-common.js
  42. 56
      src/js/dashboard.js
  43. 310
      src/js/filtering-context.js
  44. 760
      src/js/hntrie.js
  45. 200
      src/js/hosts-files.js
  46. 9
      src/js/httpsb.js
  47. 228
      src/js/i18n.js
  48. 4
      src/js/liquid-dict.js
  49. 2722
      src/js/logger-ui.js
  50. 36
      src/js/logger.js
  51. 210
      src/js/lz4.js
  52. 56
      src/js/main-blocked.js
  53. 310
      src/js/matrix.js
  54. 776
      src/js/messaging.js
  55. 133
      src/js/pagestats.js
  56. 1117
      src/js/popup.js
  57. 112
      src/js/raw-settings.js
  58. 114
      src/js/settings.js
  59. 120
      src/js/start.js
  60. 908
      src/js/storage.js
  61. 549
      src/js/tab.js
  62. 543
      src/js/traffic.js
  63. 171
      src/js/uritools.js
  64. 240
      src/js/user-rules.js
  65. 128
      src/js/utils.js
  66. 24
      src/js/wasm/README.md
  67. BIN
      src/js/wasm/hntrie.wasm
  68. 710
      src/js/wasm/hntrie.wat
  69. 127
      src/lib/codemirror/addon/display/panel.js
  70. 122
      src/lib/codemirror/addon/scroll/annotatescrollbar.js
  71. 8
      src/lib/codemirror/addon/search/matchesonscrollbar.css
  72. 97
      src/lib/codemirror/addon/search/matchesonscrollbar.js
  73. 293
      src/lib/codemirror/addon/search/searchcursor.js
  74. 18882
      src/lib/codemirror/lib/codemirror.js
  75. 52
      src/lib/lz4/README.md
  76. 151
      src/lib/lz4/lz4-block-codec-any.js
  77. 297
      src/lib/lz4/lz4-block-codec-js.js
  78. 195
      src/lib/lz4/lz4-block-codec-wasm.js
  79. BIN
      src/lib/lz4/lz4-block-codec.wasm
  80. 745
      src/lib/lz4/lz4-block-codec.wat
  81. 343
      src/lib/publicsuffixlist.js
  82. 647
      src/lib/publicsuffixlist/publicsuffixlist.js
  83. 29
      src/lib/publicsuffixlist/wasm/README.md
  84. BIN
      src/lib/publicsuffixlist/wasm/publicsuffixlist.wasm
  85. 317
      src/lib/publicsuffixlist/wasm/publicsuffixlist.wat
  86. 171
      src/logger-ui.html
  87. 1
      src/main-blocked.html
  88. 20
      src/popup.html
  89. 15
      src/raw-settings.html
  90. 1
      src/settings.html
  91. 3
      src/user-rules.html

4
.jshintrc

@ -2,11 +2,13 @@
"browser": true,
"devel": true,
"eqeqeq": true,
"esnext": true,
"esversion": 8,
"globals": {
"browser": false, // global variable in Firefox, Edge
"self": false,
"chrome": false,
"log": false,
"webext": false,
"vAPI": false,
"µMatrix": false
},

2
dist/version

@ -1 +1 @@
1.4.0
1.4.1.0

2
platform/chromium/manifest.json

@ -22,7 +22,7 @@
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["/js/vapi-client.js", "/js/contentscript-start.js"],
"js": ["/js/vapi.js", "/js/vapi-client.js", "/js/contentscript-start.js"],
"run_at": "document_start",
"all_frames": true
},

1841
platform/chromium/vapi-background.js

File diff suppressed because it is too large

308
platform/chromium/vapi-client-extra.js

@ -0,0 +1,308 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2019-present 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
*/
// For non-background page
'use strict';
/******************************************************************************/
// Direct messaging connection ability
(( ) => {
// >>>>>>>> start of private namespace
if (
typeof vAPI !== 'object' ||
vAPI.messaging instanceof Object === false ||
vAPI.MessagingConnection instanceof Function
) {
return;
}
const listeners = new Set();
const connections = new Map();
vAPI.MessagingConnection = class {
constructor(handler, details) {
this.messaging = vAPI.messaging;
this.handler = handler;
this.id = details.id;
this.to = details.to;
this.toToken = details.toToken;
this.from = details.from;
this.fromToken = details.fromToken;
this.checkTimer = undefined;
// On Firefox it appears ports are not automatically disconnected
// when navigating to another page.
const ctor = vAPI.MessagingConnection;
if ( ctor.pagehide !== undefined ) { return; }
ctor.pagehide = ( ) => {
for ( const connection of connections.values() ) {
connection.disconnect();
connection.handler(
connection.toDetails('connectionBroken')
);
}
};
window.addEventListener('pagehide', ctor.pagehide);
}
toDetails(what, payload) {
return {
what: what,
id: this.id,
from: this.from,
fromToken: this.fromToken,
to: this.to,
toToken: this.toToken,
payload: payload
};
}
disconnect() {
if ( this.checkTimer !== undefined ) {
clearTimeout(this.checkTimer);
this.checkTimer = undefined;
}
connections.delete(this.id);
const port = this.messaging.getPort();
if ( port === null ) { return; }
port.postMessage({
channel: 'vapi',
msg: this.toDetails('connectionBroken'),
});
}
checkAsync() {
if ( this.checkTimer !== undefined ) {
clearTimeout(this.checkTimer);
}
this.checkTimer = vAPI.setTimeout(
( ) => { this.check(); },
499
);
}
check() {
this.checkTimer = undefined;
if ( connections.has(this.id) === false ) { return; }
const port = this.messaging.getPort();
if ( port === null ) { return; }
port.postMessage({
channel: 'vapi',
msg: this.toDetails('connectionCheck'),
});
this.checkAsync();
}
receive(details) {
switch ( details.what ) {
case 'connectionAccepted':
this.toToken = details.toToken;
this.handler(details);
this.checkAsync();
break;
case 'connectionBroken':
connections.delete(this.id);
this.handler(details);
break;
case 'connectionMessage':
this.handler(details);
this.checkAsync();
break;
case 'connectionCheck':
const port = this.messaging.getPort();
if ( port === null ) { return; }
if ( connections.has(this.id) ) {
this.checkAsync();
} else {
details.what = 'connectionBroken';
port.postMessage({ channel: 'vapi', msg: details });
}
break;
case 'connectionRefused':
connections.delete(this.id);
this.handler(details);
break;
}
}
send(payload) {
const port = this.messaging.getPort();
if ( port === null ) { return; }
port.postMessage({
channel: 'vapi',
msg: this.toDetails('connectionMessage', payload),
});
}
static addListener(listener) {
listeners.add(listener);
}
static async connectTo(from, to, handler) {
const port = vAPI.messaging.getPort();
if ( port === null ) { return; }
const connection = new vAPI.MessagingConnection(handler, {
id: `${from}-${to}-${vAPI.sessionId}`,
to: to,
from: from,
fromToken: port.name
});
connections.set(connection.id, connection);
port.postMessage({
channel: 'vapi',
msg: {
what: 'connectionRequested',
id: connection.id,
from: from,
fromToken: port.name,
to: to,
}
});
return connection.id;
}
static disconnectFrom(connectionId) {
const connection = connections.get(connectionId);
if ( connection === undefined ) { return; }
connection.disconnect();
}
static sendTo(connectionId, payload) {
const connection = connections.get(connectionId);
if ( connection === undefined ) { return; }
connection.send(payload);
}
static canDestroyPort() {
return listeners.length === 0 && connections.size === 0;
}
static mustDestroyPort() {
if ( connections.size === 0 ) { return; }
for ( const connection of connections.values() ) {
connection.receive({ what: 'connectionBroken' });
}
connections.clear();
}
static canProcessMessage(details) {
if ( details.channel !== 'vapi' ) { return; }
switch ( details.msg.what ) {
case 'connectionAccepted':
case 'connectionBroken':
case 'connectionCheck':
case 'connectionMessage':
case 'connectionRefused': {
const connection = connections.get(details.msg.id);
if ( connection === undefined ) { break; }
connection.receive(details.msg);
return true;
}
case 'connectionRequested':
if ( listeners.length === 0 ) { return; }
const port = vAPI.messaging.getPort();
if ( port === null ) { break; }
let listener, result;
for ( listener of listeners ) {
result = listener(details.msg);
if ( result !== undefined ) { break; }
}
if ( result === undefined ) { break; }
if ( result === true ) {
details.msg.what = 'connectionAccepted';
details.msg.toToken = port.name;
const connection = new vAPI.MessagingConnection(
listener,
details.msg
);
connections.set(connection.id, connection);
} else {
details.msg.what = 'connectionRefused';
}
port.postMessage(details);
return true;
default:
break;
}
}
};
vAPI.messaging.extensions.push(vAPI.MessagingConnection);
// <<<<<<<< end of private namespace
})();
/******************************************************************************/
// Broadcast listening ability
(( ) => {
// >>>>>>>> start of private namespace
if (
typeof vAPI !== 'object' ||
vAPI.messaging instanceof Object === false ||
vAPI.broadcastListener instanceof Object
) {
return;
}
const listeners = new Set();
vAPI.broadcastListener = {
add: function(listener) {
listeners.add(listener);
vAPI.messaging.getPort();
},
remove: function(listener) {
listeners.delete(listener);
},
canDestroyPort() {
return listeners.size === 0;
},
mustDestroyPort() {
listeners.clear();
},
canProcessMessage(details) {
if ( details.broadcast === false ) { return; }
for ( const listener of listeners ) {
listener(details.msg);
}
},
};
vAPI.messaging.extensions.push(vAPI.broadcastListener);
// <<<<<<<< end of private namespace
})();
/******************************************************************************/
/*******************************************************************************
DO NOT:
- Remove the following code
- Add code beyond the following code
Reason:
- https://github.com/gorhill/uBlock/pull/3721
- uBO never uses the return value from injected content scripts
**/
void 0;

273
platform/chromium/vapi-client.js

@ -1,7 +1,8 @@
/*******************************************************************************
uMatrix - a browser extension to block requests.
Copyright (C) 2014-2018 The uMatrix/uBlock Origin authors
uBlock Origin - a browser extension to block requests.
Copyright (C) 2014-2015 The uBlock Origin authors
Copyright (C) 2014-present 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
@ -16,60 +17,61 @@
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/uMatrix
Home: https://github.com/gorhill/uBlock
*/
// For non background pages
// For non-background page
'use strict';
/******************************************************************************/
(function(self) {
/******************************************************************************/
// https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10
if ( self.vAPI === undefined || self.vAPI.uMatrix !== true ) {
self.vAPI = { uMatrix: true };
}
var vAPI = self.vAPI;
var chrome = self.chrome;
// https://github.com/chrisaljoudi/uBlock/issues/456
// Already injected?
if ( vAPI.vapiClientInjected ) {
//console.debug('vapi-client.js already injected: skipping.');
return;
}
vAPI.vapiClientInjected = true;
// Skip if already injected.
vAPI.sessionId = String.fromCharCode(Date.now() % 26 + 97) +
Math.random().toString(36).slice(2);
// >>>>>>>> start of HUGE-IF-BLOCK
if (
typeof vAPI === 'object' &&
vAPI.randomToken instanceof Function === false
) {
/******************************************************************************/
/******************************************************************************/
vAPI.shutdown = (function() {
var jobs = [];
vAPI.randomToken = function() {
const now = Date.now();
return String.fromCharCode(now % 26 + 97) +
Math.floor((1 + Math.random()) * now).toString(36);
};
var add = function(job) {
jobs.push(job);
};
vAPI.sessionId = vAPI.randomToken();
vAPI.setTimeout = vAPI.setTimeout || self.setTimeout.bind(self);
var exec = function() {
//console.debug('Shutting down...');
var job;
while ( (job = jobs.pop()) ) {
job();
}
};
/******************************************************************************/
return {
add: add,
exec: exec
};
})();
vAPI.shutdown = {
jobs: [],
add: function(job) {
this.jobs.push(job);
},
exec: function() {
// Shutdown asynchronously, to ensure shutdown jobs are called from
// the top context.
self.requestIdleCallback(( ) => {
const jobs = this.jobs.slice();
this.jobs.length = 0;
while ( jobs.length !== 0 ) {
(jobs.pop())();
}
});
},
remove: function(job) {
let pos;
while ( (pos = this.jobs.indexOf(job)) !== -1 ) {
this.jobs.splice(pos, 1);
}
}
};
/******************************************************************************/
@ -77,9 +79,10 @@ vAPI.messaging = {
port: null,
portTimer: null,
portTimerDelay: 10000,
listeners: new Set(),
extended: undefined,
extensions: [],
msgIdGenerator: 1,
pending: new Map(),
auxProcessId: 1,
shuttingDown: false,
shutdown: function() {
@ -87,41 +90,53 @@ vAPI.messaging = {
this.destroyPort();
},
// https://github.com/uBlockOrigin/uBlock-issues/issues/403
// Spurious disconnection can happen, so do not consider such events
// as world-ending, i.e. stay around. Except for embedded frames.
disconnectListener: function() {
this.port = null;
vAPI.shutdown.exec();
if ( window !== window.top ) {
vAPI.shutdown.exec();
}
},
disconnectListenerBound: null,
messageListener: function(details) {
if ( !details ) { return; }
// Sent to all listeners
if ( details.broadcast ) {
this.sendToListeners(details.msg);
return;
}
if ( details instanceof Object === false ) { return; }
// Response to specific message previously sent
var listener;
if ( details.auxProcessId ) {
listener = this.pending.get(details.auxProcessId);
if ( listener !== undefined ) {
this.pending.delete(details.auxProcessId);
listener(details.msg);
if ( details.msgId !== undefined ) {
const resolver = this.pending.get(details.msgId);
if ( resolver !== undefined ) {
this.pending.delete(details.msgId);
resolver(details.msg);
return;
}
}
// Unhandled messages
this.extensions.every(ext => ext.canProcessMessage(details) !== true);
},
messageListenerBound: null,
canDestroyPort: function() {
return this.pending.size === 0 &&
(
this.extensions.length === 0 ||
this.extensions.every(e => e.canDestroyPort())
);
},
mustDestroyPort: function() {
if ( this.extensions.length === 0 ) { return; }
this.extensions.forEach(e => e.mustDestroyPort());
this.extensions.length = 0;
},
messageListenerCallback: null,
portPoller: function() {
this.portTimer = null;
if (
this.port !== null &&
this.listeners.size === 0 &&
this.pending.size === 0
) {
if ( this.port !== null && this.canDestroyPort() ) {
return this.destroyPort();
}
this.portTimer = vAPI.setTimeout(this.portPollerBound, this.portTimerDelay);
@ -134,48 +149,50 @@ vAPI.messaging = {
clearTimeout(this.portTimer);
this.portTimer = null;
}
var port = this.port;
const port = this.port;
if ( port !== null ) {
port.disconnect();
port.onMessage.removeListener(this.messageListenerCallback);
port.onMessage.removeListener(this.messageListenerBound);
port.onDisconnect.removeListener(this.disconnectListenerBound);
this.port = null;
}
this.listeners.clear();
this.mustDestroyPort();
// service pending callbacks
if ( this.pending.size !== 0 ) {
var pending = this.pending;
const pending = this.pending;
this.pending = new Map();
for ( var callback of pending.values() ) {
if ( typeof callback === 'function' ) {
callback(null);
}
for ( const resolver of pending.values() ) {
resolver();
}
}
},
createPort: function() {
if ( this.shuttingDown ) { return null; }
if ( this.messageListenerCallback === null ) {
this.messageListenerCallback = this.messageListener.bind(this);
if ( this.messageListenerBound === null ) {
this.messageListenerBound = this.messageListener.bind(this);
this.disconnectListenerBound = this.disconnectListener.bind(this);
this.portPollerBound = this.portPoller.bind(this);
}
try {
this.port = chrome.runtime.connect({name: vAPI.sessionId}) || null;
this.port = browser.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.disconnectListenerBound);
this.portTimerDelay = 10000;
if ( this.portTimer === null ) {
this.portTimer = vAPI.setTimeout(
this.portPollerBound,
this.portTimerDelay
);
}
// Not having a valid port at this point means the main process is
// not available: no point keeping the content scripts alive.
if ( this.port === null ) {
vAPI.shutdown.exec();
return null;
}
this.port.onMessage.addListener(this.messageListenerBound);
this.port.onDisconnect.addListener(this.disconnectListenerBound);
this.portTimerDelay = 10000;
if ( this.portTimer === null ) {
this.portTimer = vAPI.setTimeout(
this.portPollerBound,
this.portTimerDelay
);
}
return this.port;
},
@ -184,70 +201,68 @@ vAPI.messaging = {
return this.port !== null ? this.port : this.createPort();
},
send: function(channelName, message, callback) {
send: function(channel, msg) {
// 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.pending.size > 25 ) {
if ( this.pending.size > 50 ) {
vAPI.shutdown.exec();
}
var port = this.getPort();
const port = this.getPort();
if ( port === null ) {
if ( typeof callback === 'function' ) { callback(); }
return;
return Promise.resolve();
}
var auxProcessId;
if ( callback ) {
auxProcessId = this.auxProcessId++;
this.pending.set(auxProcessId, callback);
}
port.postMessage({
channelName: channelName,
auxProcessId: auxProcessId,
msg: message
const msgId = this.msgIdGenerator++;
const promise = new Promise(resolve => {
this.pending.set(msgId, resolve);
});
port.postMessage({ channel, msgId, msg });
return promise;
},
addListener: function(listener) {
this.listeners.add(listener);
this.getPort();
},
removeListener: function(listener) {
this.listeners.delete(listener);
},
removeAllListeners: function() {
this.listeners.clear();
},
sendToListeners: function(msg) {
for ( var listener of this.listeners ) {
listener(msg);
// Dynamically extend capabilities.
extend: function() {
if ( this.extended === undefined ) {
this.extended = vAPI.messaging.send('vapi', {
what: 'extendClient'
}).then(( ) => {
return self.vAPI instanceof Object &&
this.extensions.length !== 0;
}).catch(( ) => {
});
}
}
return this.extended;
},
};
vAPI.shutdown.add(( ) => {
vAPI.messaging.shutdown();
window.vAPI = undefined;
});
/******************************************************************************/
/******************************************************************************/
// 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 ) {
vAPI.shutdown.add(function() {
vAPI = null;
});
}
// <<<<<<<< end of HUGE-IF-BLOCK
/******************************************************************************/
vAPI.setTimeout = vAPI.setTimeout || function(callback, delay) {
setTimeout(function() { callback(); }, delay);
};
/******************************************************************************/
})(this); // jshint ignore: line
/******************************************************************************/
/*******************************************************************************
DO NOT:
- Remove the following code
- Add code beyond the following code
Reason:
- https://github.com/gorhill/uBlock/pull/3721
- uBO never uses the return value from injected content scripts
**/
void 0;

237
platform/chromium/vapi-common.js

@ -23,32 +23,14 @@
'use strict';
/******************************************************************************/
if ( self.browser instanceof Object ) {
self.chrome = self.browser;
} else {
self.browser = self.chrome;
}
/******************************************************************************/
/******************************************************************************/
(function(self) {
vAPI.T0 = Date.now();
/******************************************************************************/
// https://bugs.chromium.org/p/project-zero/issues/detail?id=1225&desc=6#c10
if ( self.vAPI === undefined || self.vAPI.uMatrix !== true ) {
self.vAPI = { uMatrix: true };
}
var vAPI = self.vAPI;
var chrome = self.chrome;
/******************************************************************************/
vAPI.setTimeout = vAPI.setTimeout || window.setTimeout.bind(window);
vAPI.setTimeout = vAPI.setTimeout || self.setTimeout.bind(self);
/******************************************************************************/
@ -57,113 +39,182 @@ vAPI.webextFlavor = {
soup: new Set()
};
(function() {
var ua = navigator.userAgent,
flavor = vAPI.webextFlavor,
soup = flavor.soup;
var dispatch = function() {
(( ) => {
const ua = navigator.userAgent;
const flavor = vAPI.webextFlavor;
const soup = flavor.soup;
const dispatch = function() {
window.dispatchEvent(new CustomEvent('webextFlavor'));
};
// This is always true.
soup.add('ublock');
soup.add('ublock').add('webext');
// Whether this is a dev build.
if ( /^\d+\.\d+\.\d+\D/.test(browser.runtime.getManifest().version) ) {
soup.add('devbuild');
}
if ( /\bMobile\b/.test(ua) ) {
soup.add('mobile');
}
// Asynchronous
var async = self.browser instanceof Object &&
typeof self.browser.runtime.getBrowserInfo === 'function';
if ( async ) {
self.browser.runtime.getBrowserInfo().then(function(info) {
flavor.major = parseInt(info.version, 10) || 0;
if (
browser instanceof Object &&
typeof browser.runtime.getBrowserInfo === 'function'
) {
browser.runtime.getBrowserInfo().then(info => {
flavor.major = parseInt(info.version, 10) || 60;
soup.add(info.vendor.toLowerCase())
.add(info.name.toLowerCase());
if ( flavor.major >= 53 ) { soup.add('user_stylesheet'); }
if ( flavor.major >= 57 ) { soup.add('html_filtering'); }
if ( soup.has('firefox') && flavor.major < 57 ) {
soup.delete('html_filtering');
}
dispatch();
});
if ( browser.runtime.getURL('').startsWith('moz-extension://') ) {
soup.add('mozilla')
.add('firefox')
.add('user_stylesheet')
.add('html_filtering');
flavor.major = 60;
}
return;
}
// Synchronous
var match = /Firefox\/([\d.]+)/.exec(ua);
if ( match !== null ) {
// Synchronous -- order of tests is important
let match;
if ( (match = /\bEdge\/(\d+)/.exec(ua)) !== null ) {
flavor.major = parseInt(match[1], 10) || 0;
soup.add('mozilla')
.add('firefox');
if ( flavor.major >= 53 ) { soup.add('user_stylesheet'); }
if ( flavor.major >= 57 ) { soup.add('html_filtering'); }
} else {
match = /OPR\/([\d.]+)/.exec(ua);
if ( match !== null ) {
var reEx = /Chrom(?:e|ium)\/([\d.]+)/;
if ( reEx.test(ua) ) { match = reEx.exec(ua); }
flavor.major = parseInt(match[1], 10) || 0;
soup.add('opera').add('chromium');
} else {
match = /Chromium\/([\d.]+)/.exec(ua);
if ( match !== null ) {
flavor.major = parseInt(match[1], 10) || 0;
soup.add('chromium');
} else {
match = /Chrome\/([\d.]+)/.exec(ua);
if ( match !== null ) {
flavor.major = parseInt(match[1], 10) || 0;
soup.add('google').add('chromium');
}
}
}
// https://github.com/gorhill/uBlock/issues/3588
if ( soup.has('chromium') && flavor.major >= 67 ) {
soup.add('user_stylesheet');
}
soup.add('microsoft').add('edge');
} else if ( (match = /\bOPR\/(\d+)/.exec(ua)) !== null ) {
const reEx = /\bChrom(?:e|ium)\/([\d.]+)/;
if ( reEx.test(ua) ) { match = reEx.exec(ua); }
flavor.major = parseInt(match[1], 10) || 0;
soup.add('opera').add('chromium');
} else if ( (match = /\bChromium\/(\d+)/.exec(ua)) !== null ) {
flavor.major = parseInt(match[1], 10) || 0;
soup.add('chromium');
} else if ( (match = /\bChrome\/(\d+)/.exec(ua)) !== null ) {
flavor.major = parseInt(match[1], 10) || 0;
soup.add('google').add('chromium');
} else if ( (match = /\bSafari\/(\d+)/.exec(ua)) !== null ) {
flavor.major = parseInt(match[1], 10) || 0;
soup.add('apple').add('safari');
}
// Don't starve potential listeners
if ( !async ) {
vAPI.setTimeout(dispatch, 97);
// https://github.com/gorhill/uBlock/issues/3588
if ( soup.has('chromium') && flavor.major >= 66 ) {
soup.add('user_stylesheet');
}
// Don't starve potential listeners
vAPI.setTimeout(dispatch, 97);
})();
/******************************************************************************/
// http://www.w3.org/International/questions/qa-scripts#directions
{
const punycode = self.punycode;
const reCommonHostnameFromURL = /^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//;
const reAuthorityFromURI = /^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/;
const reHostFromNakedAuthority = /^[0-9a-z._-]+[0-9a-z]$/i;
const reHostFromAuthority = /^(?:[^@]*@)?([^:]+)(?::\d*)?$/;
const reIPv6FromAuthority = /^(?:[^@]*@)?(\[[0-9a-f:]+\])(?::\d*)?$/i;
const reMustNormalizeHostname = /[^0-9a-z._-]/;
vAPI.hostnameFromURI = function(uri) {
let matches = reCommonHostnameFromURL.exec(uri);
if ( matches !== null ) { return matches[1]; }
matches = reAuthorityFromURI.exec(uri);
if ( matches === null ) { return ''; }
const authority = matches[1].slice(2);
if ( reHostFromNakedAuthority.test(authority) ) {
return authority.toLowerCase();
}
matches = reHostFromAuthority.exec(authority);
if ( matches === null ) {
matches = reIPv6FromAuthority.exec(authority);
if ( matches === null ) { return ''; }
}
let hostname = matches[1];
while ( hostname.endsWith('.') ) {
hostname = hostname.slice(0, -1);
}
if ( reMustNormalizeHostname.test(hostname) ) {
hostname = punycode.toASCII(hostname.toLowerCase());
}
return hostname;
};
var setScriptDirection = function(language) {
document.body.setAttribute(
'dir',
['ar', 'he', 'fa', 'ps', 'ur'].indexOf(language) !== -1 ? 'rtl' : 'ltr'
);
};
const reHostnameFromNetworkURL =
/^(?:http|ws|ftp)s?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//;
vAPI.hostnameFromNetworkURL = function(url) {
const matches = reHostnameFromNetworkURL.exec(url);
return matches !== null ? matches[1] : '';
};
const psl = self.publicSuffixList;
const reIPAddressNaive = /^\d+\.\d+\.\d+\.\d+$|^\[[\da-zA-Z:]+\]$/;
vAPI.domainFromHostname = function(hostname) {
return reIPAddressNaive.test(hostname)
? hostname
: psl.getDomain(hostname);
};
vAPI.domainFromURI = function(uri) {
return uri !== ''
? vAPI.domainFromHostname(vAPI.hostnameFromURI(uri))
: '';
};
}
/******************************************************************************/
vAPI.download = function(details) {
if ( !details.url ) {
return;
}
var a = document.createElement('a');
if ( !details.url ) { return; }
const a = document.createElement('a');
a.href = details.url;
a.setAttribute('download', details.filename || '');
a.setAttribute('type', 'text/plain');
a.dispatchEvent(new MouseEvent('click'));
};
/******************************************************************************/
vAPI.getURL = chrome.runtime.getURL;
vAPI.getURL = browser.runtime.getURL;
/******************************************************************************/
vAPI.i18n = chrome.i18n.getMessage;
vAPI.i18n = browser.i18n.getMessage;
setScriptDirection(vAPI.i18n('@@ui_locale'));
// http://www.w3.org/International/questions/qa-scripts#directions
document.body.setAttribute(
'dir',
['ar', 'he', 'fa', 'ps', 'ur'].indexOf(vAPI.i18n('@@ui_locale')) !== -1
? 'rtl'
: 'ltr'
);
/******************************************************************************/
// https://github.com/gorhill/uBlock/issues/3057
// - webNavigation.onCreatedNavigationTarget become broken on Firefox when we
// try to make the popup panel close itself using the original
// `window.open('', '_self').close()`.
vAPI.closePopup = function() {
window.close();
if ( vAPI.webextFlavor.soup.has('firefox') ) {
window.close();
return;
}
// TODO: try to figure why this was used instead of a plain window.close().
// https://github.com/gorhill/uBlock/commit/b301ac031e0c2e9a99cb6f8953319d44e22f33d2#diff-bc664f26b9c453e0d43a9379e8135c6a
window.open('', '_self').close();
};
/******************************************************************************/
@ -207,8 +258,22 @@ vAPI.localStorage = {
}
};
/******************************************************************************/
})(this);
/******************************************************************************/
/*******************************************************************************
DO NOT:
- Remove the following code
- Add code beyond the following code
Reason:
- https://github.com/gorhill/uBlock/pull/3721
- uBO never uses the return value from injected content scripts
**/
void 0;

230
platform/chromium/vapi-webrequest.js

@ -25,40 +25,17 @@
/******************************************************************************/
(function() {
(( ) => {
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
if ( vAPI.webextFlavor.soup.has('chromium') === false ) { return; }
const extToTypeMap = new Map([
['eot','font'],['otf','font'],['svg','font'],['ttf','font'],['woff','font'],['woff2','font'],
['mp3','media'],['mp4','media'],['webm','media'],
['gif','image'],['ico','image'],['jpeg','image'],['jpg','image'],['png','image'],['webp','image']
]);
// https://www.reddit.com/r/uBlockOrigin/comments/9vcrk3/bug_in_ubo_1173_betas_when_saving_files_hosted_on/
// Some types can be mapped from 'other', thus include 'other' if and
// only if the caller is interested in at least one of those types.
const denormalizeTypes = function(aa) {
if ( aa.length === 0 ) {
return Array.from(vAPI.net.validTypes);
}
const out = new Set();
let i = aa.length;
while ( i-- ) {
const type = aa[i];
if ( vAPI.net.validTypes.has(type) ) {
out.add(type);
}
}
if ( out.has('other') === false ) {
for ( const type of extToTypeMap.values() ) {
if ( out.has(type) ) {
out.add('other');
break;
}
}
}
return Array.from(out);
};
const headerValue = function(headers, name) {
const headerValue = (headers, name) => {
let i = headers.length;
while ( i-- ) {
if ( headers[i].name.toLowerCase() === name ) {
@ -70,128 +47,131 @@
const parsedURL = new URL('https://www.example.org/');
vAPI.net.normalizeDetails = function(details) {
let type = details.type;
// Extend base class to normalize as per platform.
// https://github.com/uBlockOrigin/uMatrix-issues/issues/156#issuecomment-494427094
if ( type === 'main_frame' ) {
details.documentUrl = details.url;
vAPI.Net = class extends vAPI.Net {
constructor() {
super();
this.suspendedTabIds = new Set();
}
// Chromium 63+ supports the `initiator` property, which contains
// the URL of the origin from which the network request was made.
else if (
typeof details.initiator === 'string' &&
details.initiator !== 'null'
) {
details.documentUrl = details.initiator;
}
// https://github.com/gorhill/uBlock/issues/1493
// Chromium 49+/WebExtensions support a new request type: `ping`,
// which is fired as a result of using `navigator.sendBeacon`.
if ( type === 'ping' ) {
details.type = 'beacon';
return;
}
if ( type === 'imageset' ) {
details.type = 'image';
return;
}
// The rest of the function code is to normalize type
if ( type !== 'other' ) { return; }
normalizeDetails(details) {
// Chromium 63+ supports the `initiator` property, which contains
// the URL of the origin from which the network request was made.
if (
typeof details.initiator === 'string' &&
details.initiator !== 'null'
) {
details.documentUrl = details.initiator;
}
// Try to map known "extension" part of URL to request type.
parsedURL.href = details.url;
const path = parsedURL.pathname,
pos = path.indexOf('.', path.length - 6);
if ( pos !== -1 && (type = extToTypeMap.get(path.slice(pos + 1))) ) {
details.type = type;
return;
}
let type = details.type;
// Try to extract type from response headers if present.
if ( details.responseHeaders ) {
type = headerValue(details.responseHeaders, 'content-type');
if ( type.startsWith('font/') ) {
details.type = 'font';
return;
}
if ( type.startsWith('image/') ) {
if ( type === 'imageset' ) {
details.type = 'image';
return;
}
if ( type.startsWith('audio/') || type.startsWith('video/') ) {
details.type = 'media';
// The rest of the function code is to normalize type
if ( type !== 'other' ) { return; }
// Try to map known "extension" part of URL to request type.
parsedURL.href = details.url;
const path = parsedURL.pathname,
pos = path.indexOf('.', path.length - 6);
if ( pos !== -1 && (type = extToTypeMap.get(path.slice(pos + 1))) ) {
details.type = type;
return;
}
}
};
vAPI.net.denormalizeFilters = function(filters) {
const urls = filters.urls || [ '<all_urls>' ];
let types = filters.types;
if ( Array.isArray(types) ) {
types = denormalizeTypes(types);
// Try to extract type from response headers if present.
if ( details.responseHeaders ) {
type = headerValue(details.responseHeaders, 'content-type');
if ( type.startsWith('font/') ) {
details.type = 'font';
return;
}
if ( type.startsWith('image/') ) {
details.type = 'image';
return;
}
if ( type.startsWith('audio/') || type.startsWith('video/') ) {
details.type = 'media';
return;
}
}
}
if (
(vAPI.net.validTypes.has('websocket')) &&
(types === undefined || types.indexOf('websocket') !== -1) &&
(urls.indexOf('<all_urls>') === -1)
) {
if ( urls.indexOf('ws://*/*') === -1 ) {
urls.push('ws://*/*');
// https://www.reddit.com/r/uBlockOrigin/comments/9vcrk3/
// Some types can be mapped from 'other', thus include 'other' if and
// only if the caller is interested in at least one of those types.
denormalizeTypes(types) {
if ( types.length === 0 ) {
return Array.from(this.validTypes);
}
const out = new Set();
for ( const type of types ) {
if ( this.validTypes.has(type) ) {
out.add(type);
}
}
if ( urls.indexOf('wss://*/*') === -1 ) {
urls.push('wss://*/*');
if ( out.has('other') === false ) {
for ( const type of extToTypeMap.values() ) {
if ( out.has(type) ) {
out.add('other');
break;
}
}
}
return Array.from(out);
}
suspendOneRequest(details) {
this.suspendedTabIds.add(details.tabId);
return { cancel: true };
}
unsuspendAllRequests() {
for ( const tabId of this.suspendedTabIds ) {
vAPI.tabs.reload(tabId);
}
this.suspendedTabIds.clear();
}
return { types, urls };
};
})();
/******************************************************************************/
// https://github.com/gorhill/uBlock/issues/2067
// Experimental: Block everything until uBO is fully ready.
// https://github.com/uBlockOrigin/uBlock-issues/issues/548
// Use `X-DNS-Prefetch-Control` to workaround Chromium's disregard of the
// setting "Predict network actions to improve page load performance".
vAPI.net.onBeforeReady = (function() {
let pendings;
vAPI.prefetching = (( ) => {
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
if ( vAPI.webextFlavor.soup.has('chromium') === false ) { return; }
const handler = function(details) {
if ( pendings === undefined ) { return; }
if ( details.tabId < 0 ) { return; }
let listening = false;
pendings.add(details.tabId);
//console.log(`Aborting tab ${details.tabId}: ${details.type} ${details.url}`);
return { cancel: true };
const onHeadersReceived = function(details) {
details.responseHeaders.push({
name: 'X-DNS-Prefetch-Control',
value: 'off'
});
return { responseHeaders: details.responseHeaders };
};
return {
experimental: true,
start: function() {
pendings = new Set();
browser.webRequest.onBeforeRequest.addListener(
handler,
{ urls: [ 'http://*/*', 'https://*/*' ] },
[ 'blocking' ]
return state => {
const wr = chrome.webRequest;
if ( state && listening ) {
wr.onHeadersReceived.removeListener(onHeadersReceived);
listening = false;
} else if ( !state && !listening ) {
wr.onHeadersReceived.addListener(
onHeadersReceived,
{
urls: [ 'http://*/*', 'https://*/*' ],
types: [ 'main_frame', 'sub_frame' ]
},
[ 'blocking', 'responseHeaders' ]
);
},
// https://github.com/gorhill/uBlock/issues/2067
// Force-reload tabs for which network requests were blocked
// during launch. This can happen only if tabs were "suspended".
stop: function() {
if ( pendings === undefined ) { return; }
browser.webRequest.onBeforeRequest.removeListener(handler);
for ( const tabId of pendings ) {
//console.log(`Reloading tab ${tabId}`);
vAPI.tabs.reload(tabId);
}
pendings = undefined;
},
listening = true;
}
};
})();

86
platform/chromium/vapi.js

@ -0,0 +1,86 @@
/*******************************************************************************
uMatrix - a browser extension to block requests.
Copyright (C) 2017-present 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
*/
'use strict';
/* global HTMLDocument, XMLDocument */
// For background page, auxiliary pages, and content scripts.
/******************************************************************************/
if ( self.browser instanceof Object ) {
self.chrome = self.browser;
} else {
self.browser = self.chrome;
}
/******************************************************************************/
// https://bugzilla.mozilla.org/show_bug.cgi?id=1408996#c9
var vAPI = self.vAPI; // jshint ignore:line
// https://github.com/chrisaljoudi/uBlock/issues/464
// https://github.com/chrisaljoudi/uBlock/issues/1528
// A XMLDocument can be a valid HTML document.
// 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.
if (
(
document instanceof HTMLDocument ||
document instanceof XMLDocument &&
document.createElement('div') instanceof HTMLDivElement
) &&
(
/^image\/|^text\/plain/.test(document.contentType || '') === false
) &&
(
self.vAPI instanceof Object === false || vAPI.uMatrix !== true
)
) {
vAPI = self.vAPI = { uMatrix: true };
}
/*******************************************************************************
DO NOT:
- Remove the following code
- Add code beyond the following code
Reason:
- https://github.com/gorhill/uBlock/pull/3721
- uMatrix never uses the return value from injected content scripts
**/
void 0;

176
platform/chromium/webext.js

@ -0,0 +1,176 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2019-present 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
*/
'use strict';
// `webext` is a promisified api of `chrome`. Entries are added as
// the promisification of uBO progress.
const webext = (( ) => { // jshint ignore:line
// >>>>> start of private scope
const noopFunc = ( ) => { };
const promisifyNoFail = function(thisArg, fnName, outFn = r => r) {
const fn = thisArg[fnName];
return function() {
return new Promise(resolve => {
fn.call(thisArg, ...arguments, function() {
if ( chrome.runtime.lastError instanceof Object ) {
void chrome.runtime.lastError.message;
}
resolve(outFn(...arguments));
});
});
};
};
const promisify = function(thisArg, fnName) {
const fn = thisArg[fnName];
return function() {
return new Promise((resolve, reject) => {
fn.call(thisArg, ...arguments, function() {
const lastError = chrome.runtime.lastError;
if ( lastError instanceof Object ) {
return reject(lastError.message);
}
resolve(...arguments);
});
});