mirror of https://github.com/gorhill/uBlock.git
Add ability to suspend network request handler at will
This works only for platforms supporting the return of Promise by network listeners, i.e. only Firefox at this point. When filter lists are reloaded[1], there is a small time window in which some network requests which should have normally been blocked are not being blocked because the static network filtering engine may not have yet loaded all the filters in memory This is now addressed by suspending the network request handler when filter lists are reloaded -- again, this works only on supported platforms. [1] Examples: when a filter list update session completes; when user filters change, when adding/removing filter lists.
This commit is contained in:
parent
c1bdc123f2
commit
1dfdc40e09
|
@ -1049,9 +1049,9 @@ vAPI.messaging.broadcast = function(message) {
|
||||||
// https://github.com/gorhill/uBlock/issues/3497
|
// https://github.com/gorhill/uBlock/issues/3497
|
||||||
// Prevent web pages from interfering with uBO's element picker
|
// Prevent web pages from interfering with uBO's element picker
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/550
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/550
|
||||||
// A specific secret can be used for at most one second.
|
// Support using a new secret for every network request.
|
||||||
|
|
||||||
vAPI.warSecret = (function() {
|
vAPI.warSecret = (( ) => {
|
||||||
const generateSecret = ( ) => {
|
const generateSecret = ( ) => {
|
||||||
return Math.floor(Math.random() * 982451653 + 982451653).toString(36) +
|
return Math.floor(Math.random() * 982451653 + 982451653).toString(36) +
|
||||||
Math.floor(Math.random() * 982451653 + 982451653).toString(36);
|
Math.floor(Math.random() * 982451653 + 982451653).toString(36);
|
||||||
|
@ -1072,7 +1072,7 @@ vAPI.warSecret = (function() {
|
||||||
secrets.splice(pos, 1);
|
secrets.splice(pos, 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
chrome.webRequest.onBeforeRequest.addListener(
|
browser.webRequest.onBeforeRequest.addListener(
|
||||||
guard,
|
guard,
|
||||||
{
|
{
|
||||||
urls: [ root + 'web_accessible_resources/*' ]
|
urls: [ root + 'web_accessible_resources/*' ]
|
||||||
|
@ -1095,59 +1095,106 @@ vAPI.warSecret = (function() {
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
vAPI.net = {
|
/******************************************************************************/
|
||||||
listenerMap: new WeakMap(),
|
|
||||||
// legacy Chromium understands only these network request types.
|
vAPI.Net = class {
|
||||||
validTypes: (function() {
|
constructor() {
|
||||||
let types = new Set([
|
this.validTypes = new Set();
|
||||||
'main_frame',
|
{
|
||||||
'sub_frame',
|
const wrrt = browser.webRequest.ResourceType;
|
||||||
'stylesheet',
|
for ( const typeKey in wrrt ) {
|
||||||
'script',
|
|
||||||
'image',
|
|
||||||
'object',
|
|
||||||
'xmlhttprequest',
|
|
||||||
'other'
|
|
||||||
]);
|
|
||||||
let wrrt = browser.webRequest.ResourceType;
|
|
||||||
if ( wrrt instanceof Object ) {
|
|
||||||
for ( let typeKey in wrrt ) {
|
|
||||||
if ( wrrt.hasOwnProperty(typeKey) ) {
|
if ( wrrt.hasOwnProperty(typeKey) ) {
|
||||||
types.add(wrrt[typeKey]);
|
this.validTypes.add(wrrt[typeKey]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.suspendableListener = undefined;
|
||||||
|
this.listenerMap = new WeakMap();
|
||||||
|
this.suspendDepth = 0;
|
||||||
|
|
||||||
|
browser.webRequest.onBeforeRequest.addListener(
|
||||||
|
details => {
|
||||||
|
this.normalizeDetails(details);
|
||||||
|
if ( this.suspendDepth === 0 ) {
|
||||||
|
if ( this.suspendableListener === undefined ) { return; }
|
||||||
|
return this.suspendableListener(details);
|
||||||
|
}
|
||||||
|
if ( details.tabId < 0 ) { return; }
|
||||||
|
return this.suspendOneRequest(details);
|
||||||
|
},
|
||||||
|
this.denormalizeFilters({ urls: [ 'http://*/*', 'https://*/*' ] }),
|
||||||
|
[ 'blocking' ]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
normalizeDetails(/* details */) {
|
||||||
|
}
|
||||||
|
denormalizeFilters(filters) {
|
||||||
|
const urls = filters.urls || [ '<all_urls>' ];
|
||||||
|
let types = filters.types;
|
||||||
|
if ( Array.isArray(types) ) {
|
||||||
|
types = this.denormalizeTypes(types);
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
(this.validTypes.has('websocket')) &&
|
||||||
|
(types === undefined || types.indexOf('websocket') !== -1) &&
|
||||||
|
(urls.indexOf('<all_urls>') === -1)
|
||||||
|
) {
|
||||||
|
if ( urls.indexOf('ws://*/*') === -1 ) {
|
||||||
|
urls.push('ws://*/*');
|
||||||
|
}
|
||||||
|
if ( urls.indexOf('wss://*/*') === -1 ) {
|
||||||
|
urls.push('wss://*/*');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { types, urls };
|
||||||
|
}
|
||||||
|
denormalizeTypes(types) {
|
||||||
return types;
|
return types;
|
||||||
})(),
|
}
|
||||||
denormalizeFilters: null,
|
addListener(which, clientListener, filters, options) {
|
||||||
normalizeDetails: null,
|
const actualFilters = this.denormalizeFilters(filters);
|
||||||
addListener: function(which, clientListener, filters, options) {
|
const actualListener = this.makeNewListenerProxy(clientListener);
|
||||||
if ( typeof this.denormalizeFilters === 'function' ) {
|
|
||||||
filters = this.denormalizeFilters(filters);
|
|
||||||
}
|
|
||||||
let actualListener;
|
|
||||||
if ( typeof this.normalizeDetails === 'function' ) {
|
|
||||||
actualListener = function(details) {
|
|
||||||
vAPI.net.normalizeDetails(details);
|
|
||||||
return clientListener(details);
|
|
||||||
};
|
|
||||||
this.listenerMap.set(clientListener, actualListener);
|
|
||||||
}
|
|
||||||
browser.webRequest[which].addListener(
|
browser.webRequest[which].addListener(
|
||||||
actualListener || clientListener,
|
actualListener,
|
||||||
filters,
|
actualFilters,
|
||||||
options
|
options
|
||||||
);
|
);
|
||||||
},
|
}
|
||||||
removeListener: function(which, clientListener) {
|
setSuspendableListener(listener) {
|
||||||
let actualListener = this.listenerMap.get(clientListener);
|
this.suspendableListener = listener;
|
||||||
if ( actualListener !== undefined ) {
|
}
|
||||||
this.listenerMap.delete(clientListener);
|
removeListener(which, clientListener) {
|
||||||
|
const actualListener = this.listenerMap.get(clientListener);
|
||||||
|
if ( actualListener === undefined ) { return; }
|
||||||
|
this.listenerMap.delete(clientListener);
|
||||||
|
browser.webRequest[which].removeListener(actualListener);
|
||||||
|
}
|
||||||
|
makeNewListenerProxy(clientListener) {
|
||||||
|
const actualListener = details => {
|
||||||
|
this.normalizeDetails(details);
|
||||||
|
return clientListener(details);
|
||||||
|
};
|
||||||
|
this.listenerMap.set(clientListener, actualListener);
|
||||||
|
return actualListener;
|
||||||
|
}
|
||||||
|
suspendOneRequest() {
|
||||||
|
}
|
||||||
|
unsuspendAllRequests() {
|
||||||
|
}
|
||||||
|
suspend(force = false) {
|
||||||
|
if ( this.canSuspend() || force ) {
|
||||||
|
this.suspendDepth += 1;
|
||||||
}
|
}
|
||||||
browser.webRequest[which].removeListener(
|
}
|
||||||
actualListener || clientListener
|
unsuspend() {
|
||||||
);
|
if ( this.suspendDepth === 0 ) { return; }
|
||||||
},
|
this.suspendDepth -= 1;
|
||||||
|
if ( this.suspendDepth !== 0 ) { return; }
|
||||||
|
this.unsuspendAllRequests(this.suspendableListener);
|
||||||
|
}
|
||||||
|
canSuspend() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
|
@ -25,9 +25,9 @@
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
(function() {
|
(( ) => {
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
|
||||||
if ( vAPI.webextFlavor.soup.has('firefox') ) { return; }
|
if ( vAPI.webextFlavor.soup.has('chromium') === false ) { return; }
|
||||||
|
|
||||||
const extToTypeMap = new Map([
|
const extToTypeMap = new Map([
|
||||||
['eot','font'],['otf','font'],['svg','font'],['ttf','font'],['woff','font'],['woff2','font'],
|
['eot','font'],['otf','font'],['svg','font'],['ttf','font'],['woff','font'],['woff2','font'],
|
||||||
|
@ -35,33 +35,7 @@
|
||||||
['gif','image'],['ico','image'],['jpeg','image'],['jpg','image'],['png','image'],['webp','image']
|
['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/
|
const headerValue = (headers, name) => {
|
||||||
// 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) {
|
|
||||||
let i = headers.length;
|
let i = headers.length;
|
||||||
while ( i-- ) {
|
while ( i-- ) {
|
||||||
if ( headers[i].name.toLowerCase() === name ) {
|
if ( headers[i].name.toLowerCase() === name ) {
|
||||||
|
@ -73,124 +47,100 @@
|
||||||
|
|
||||||
const parsedURL = new URL('https://www.example.org/');
|
const parsedURL = new URL('https://www.example.org/');
|
||||||
|
|
||||||
vAPI.net.normalizeDetails = function(details) {
|
// Extend base class to normalize as per platform.
|
||||||
// Chromium 63+ supports the `initiator` property, which contains
|
|
||||||
// the URL of the origin from which the network request was made.
|
vAPI.Net = class extends vAPI.Net {
|
||||||
if (
|
constructor() {
|
||||||
typeof details.initiator === 'string' &&
|
super();
|
||||||
details.initiator !== 'null'
|
this.suspendedTabIds = new Set();
|
||||||
) {
|
|
||||||
details.documentUrl = details.initiator;
|
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
let type = details.type;
|
let type = details.type;
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/1493
|
// https://github.com/gorhill/uBlock/issues/1493
|
||||||
// Chromium 49+/WebExtensions support a new request type: `ping`,
|
// Chromium 49+/WebExtensions support a new request type: `ping`,
|
||||||
// which is fired as a result of using `navigator.sendBeacon`.
|
// which is fired as a result of using `navigator.sendBeacon`.
|
||||||
if ( type === 'ping' ) {
|
if ( type === 'ping' ) {
|
||||||
details.type = 'beacon';
|
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; }
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
return;
|
||||||
}
|
}
|
||||||
if ( type.startsWith('image/') ) {
|
|
||||||
|
if ( type === 'imageset' ) {
|
||||||
details.type = 'image';
|
details.type = 'image';
|
||||||
return;
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
vAPI.net.denormalizeFilters = function(filters) {
|
// Try to extract type from response headers if present.
|
||||||
const urls = filters.urls || [ '<all_urls>' ];
|
if ( details.responseHeaders ) {
|
||||||
let types = filters.types;
|
type = headerValue(details.responseHeaders, 'content-type');
|
||||||
if ( Array.isArray(types) ) {
|
if ( type.startsWith('font/') ) {
|
||||||
types = denormalizeTypes(types);
|
details.type = 'font';
|
||||||
}
|
return;
|
||||||
if (
|
}
|
||||||
(vAPI.net.validTypes.has('websocket')) &&
|
if ( type.startsWith('image/') ) {
|
||||||
(types === undefined || types.indexOf('websocket') !== -1) &&
|
details.type = 'image';
|
||||||
(urls.indexOf('<all_urls>') === -1)
|
return;
|
||||||
) {
|
}
|
||||||
if ( urls.indexOf('ws://*/*') === -1 ) {
|
if ( type.startsWith('audio/') || type.startsWith('video/') ) {
|
||||||
urls.push('ws://*/*');
|
details.type = 'media';
|
||||||
}
|
return;
|
||||||
if ( urls.indexOf('wss://*/*') === -1 ) {
|
}
|
||||||
urls.push('wss://*/*');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return { types, urls };
|
// 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);
|
||||||
// https://github.com/gorhill/uBlock/issues/2067
|
}
|
||||||
// Experimental: Block everything until uBO is fully ready.
|
const out = new Set();
|
||||||
|
for ( const type of types ) {
|
||||||
vAPI.net.onBeforeReady = vAPI.net.onBeforeReady || (function() {
|
if ( this.validTypes.has(type) ) {
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
|
out.add(type);
|
||||||
if ( vAPI.webextFlavor.soup.has('firefox') ) { return; }
|
}
|
||||||
|
}
|
||||||
let pendings;
|
if ( out.has('other') === false ) {
|
||||||
|
for ( const type of extToTypeMap.values() ) {
|
||||||
const handler = function(details) {
|
if ( out.has(type) ) {
|
||||||
if ( pendings === undefined ) { return; }
|
out.add('other');
|
||||||
if ( details.tabId < 0 ) { return; }
|
break;
|
||||||
|
}
|
||||||
pendings.add(details.tabId);
|
}
|
||||||
|
}
|
||||||
return { cancel: true };
|
return Array.from(out);
|
||||||
};
|
}
|
||||||
|
suspendOneRequest(details) {
|
||||||
return {
|
this.suspendedTabIds.add(details.tabId);
|
||||||
experimental: true,
|
return { cancel: true };
|
||||||
start: function() {
|
}
|
||||||
pendings = new Set();
|
unsuspendAllRequests() {
|
||||||
browser.webRequest.onBeforeRequest.addListener(
|
for ( const tabId of this.suspendedTabIds ) {
|
||||||
handler,
|
|
||||||
{ urls: [ 'http://*/*', 'https://*/*' ] },
|
|
||||||
[ 'blocking' ]
|
|
||||||
);
|
|
||||||
},
|
|
||||||
// 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 ) {
|
|
||||||
vAPI.tabs.reload(tabId);
|
vAPI.tabs.reload(tabId);
|
||||||
}
|
}
|
||||||
pendings = undefined;
|
this.suspendedTabIds.clear();
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
@ -200,7 +150,10 @@ vAPI.net.onBeforeReady = vAPI.net.onBeforeReady || (function() {
|
||||||
// Use `X-DNS-Prefetch-Control` to workaround Chromium's disregard of the
|
// Use `X-DNS-Prefetch-Control` to workaround Chromium's disregard of the
|
||||||
// setting "Predict network actions to improve page load performance".
|
// setting "Predict network actions to improve page load performance".
|
||||||
|
|
||||||
vAPI.prefetching = (function() {
|
vAPI.prefetching = (( ) => {
|
||||||
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
|
||||||
|
if ( vAPI.webextFlavor.soup.has('chromium') === false ) { return; }
|
||||||
|
|
||||||
let listening = false;
|
let listening = false;
|
||||||
|
|
||||||
const onHeadersReceived = function(details) {
|
const onHeadersReceived = function(details) {
|
||||||
|
|
|
@ -25,14 +25,14 @@
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
(function() {
|
(( ) => {
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
|
||||||
if ( vAPI.webextFlavor.soup.has('firefox') === false ) { return; }
|
if ( vAPI.webextFlavor.soup.has('firefox') === false ) { return; }
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/2950
|
// https://github.com/gorhill/uBlock/issues/2950
|
||||||
// Firefox 56 does not normalize URLs to ASCII, uBO must do this itself.
|
// Firefox 56 does not normalize URLs to ASCII, uBO must do this itself.
|
||||||
// https://bugzilla.mozilla.org/show_bug.cgi?id=945240
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=945240
|
||||||
const evalMustPunycode = function() {
|
const evalMustPunycode = ( ) => {
|
||||||
return vAPI.webextFlavor.soup.has('firefox') &&
|
return vAPI.webextFlavor.soup.has('firefox') &&
|
||||||
vAPI.webextFlavor.major < 57;
|
vAPI.webextFlavor.major < 57;
|
||||||
};
|
};
|
||||||
|
@ -45,142 +45,100 @@
|
||||||
mustPunycode = evalMustPunycode();
|
mustPunycode = evalMustPunycode();
|
||||||
}, { once: true });
|
}, { once: true });
|
||||||
|
|
||||||
const denormalizeTypes = function(aa) {
|
|
||||||
if ( aa.length === 0 ) {
|
|
||||||
return Array.from(vAPI.net.validTypes);
|
|
||||||
}
|
|
||||||
const out = new Set();
|
|
||||||
let i = aa.length;
|
|
||||||
while ( i-- ) {
|
|
||||||
let type = aa[i];
|
|
||||||
if ( vAPI.net.validTypes.has(type) ) {
|
|
||||||
out.add(type);
|
|
||||||
}
|
|
||||||
if ( type === 'image' && vAPI.net.validTypes.has('imageset') ) {
|
|
||||||
out.add('imageset');
|
|
||||||
}
|
|
||||||
if ( type === 'sub_frame' ) {
|
|
||||||
out.add('object');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Array.from(out);
|
|
||||||
};
|
|
||||||
|
|
||||||
const punycode = self.punycode;
|
const punycode = self.punycode;
|
||||||
const reAsciiHostname = /^https?:\/\/[0-9a-z_.:@-]+[/?#]/;
|
const reAsciiHostname = /^https?:\/\/[0-9a-z_.:@-]+[/?#]/;
|
||||||
const parsedURL = new URL('about:blank');
|
const parsedURL = new URL('about:blank');
|
||||||
|
|
||||||
vAPI.net.normalizeDetails = function(details) {
|
// Related issues:
|
||||||
if ( mustPunycode && !reAsciiHostname.test(details.url) ) {
|
// - https://github.com/gorhill/uBlock/issues/1327
|
||||||
parsedURL.href = details.url;
|
// - https://github.com/uBlockOrigin/uBlock-issues/issues/128
|
||||||
details.url = details.url.replace(
|
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1503721
|
||||||
parsedURL.hostname,
|
|
||||||
punycode.toASCII(parsedURL.hostname)
|
// Extend base class to normalize as per platform.
|
||||||
);
|
|
||||||
|
vAPI.Net = class extends vAPI.Net {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.pendingRequests = [];
|
||||||
}
|
}
|
||||||
|
normalizeDetails(details) {
|
||||||
|
if ( mustPunycode && !reAsciiHostname.test(details.url) ) {
|
||||||
|
parsedURL.href = details.url;
|
||||||
|
details.url = details.url.replace(
|
||||||
|
parsedURL.hostname,
|
||||||
|
punycode.toASCII(parsedURL.hostname)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const type = details.type;
|
const type = details.type;
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/1493
|
// https://github.com/gorhill/uBlock/issues/1493
|
||||||
// Chromium 49+/WebExtensions support a new request type: `ping`,
|
// Chromium 49+/WebExtensions support a new request type: `ping`,
|
||||||
// which is fired as a result of using `navigator.sendBeacon`.
|
// which is fired as a result of using `navigator.sendBeacon`.
|
||||||
if ( type === 'ping' ) {
|
if ( type === 'ping' ) {
|
||||||
details.type = 'beacon';
|
details.type = 'beacon';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( type === 'imageset' ) {
|
if ( type === 'imageset' ) {
|
||||||
details.type = 'image';
|
details.type = 'image';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/345
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/345
|
||||||
// Re-categorize an embedded object as a `sub_frame` if its
|
// Re-categorize an embedded object as a `sub_frame` if its
|
||||||
// content type is that of a HTML document.
|
// content type is that of a HTML document.
|
||||||
if ( type === 'object' && Array.isArray(details.responseHeaders) ) {
|
if ( type === 'object' && Array.isArray(details.responseHeaders) ) {
|
||||||
for ( const header of details.responseHeaders ) {
|
for ( const header of details.responseHeaders ) {
|
||||||
if ( header.name.toLowerCase() === 'content-type' ) {
|
if ( header.name.toLowerCase() === 'content-type' ) {
|
||||||
if ( header.value.startsWith('text/html') ) {
|
if ( header.value.startsWith('text/html') ) {
|
||||||
details.type = 'sub_frame';
|
details.type = 'sub_frame';
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
denormalizeTypes(types) {
|
||||||
|
if ( types.length === 0 ) {
|
||||||
vAPI.net.denormalizeFilters = function(filters) {
|
return Array.from(this.validTypes);
|
||||||
const urls = filters.urls || [ '<all_urls>' ];
|
|
||||||
let types = filters.types;
|
|
||||||
if ( Array.isArray(types) ) {
|
|
||||||
types = denormalizeTypes(types);
|
|
||||||
}
|
|
||||||
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://*/*');
|
|
||||||
}
|
}
|
||||||
if ( urls.indexOf('wss://*/*') === -1 ) {
|
const out = new Set();
|
||||||
urls.push('wss://*/*');
|
for ( const type of types ) {
|
||||||
|
if ( this.validTypes.has(type) ) {
|
||||||
|
out.add(type);
|
||||||
|
}
|
||||||
|
if ( type === 'image' && this.validTypes.has('imageset') ) {
|
||||||
|
out.add('imageset');
|
||||||
|
}
|
||||||
|
if ( type === 'sub_frame' ) {
|
||||||
|
out.add('object');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return Array.from(out);
|
||||||
}
|
}
|
||||||
return { types, urls };
|
suspendOneRequest(details) {
|
||||||
};
|
const pending = {
|
||||||
})();
|
details: Object.assign({}, details),
|
||||||
|
resolve: undefined,
|
||||||
/******************************************************************************/
|
promise: undefined
|
||||||
|
};
|
||||||
// Related issues:
|
pending.promise = new Promise(function(resolve) {
|
||||||
// - https://github.com/gorhill/uBlock/issues/1327
|
pending.resolve = resolve;
|
||||||
// - https://github.com/uBlockOrigin/uBlock-issues/issues/128
|
});
|
||||||
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1503721
|
this.pendingRequests.push(pending);
|
||||||
|
return pending.promise;
|
||||||
vAPI.net.onBeforeReady = vAPI.net.onBeforeReady || (function() {
|
}
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
|
unsuspendAllRequests(resolver) {
|
||||||
if ( vAPI.webextFlavor.soup.has('firefox') === false ) { return; }
|
const pendingRequests = this.pendingRequests;
|
||||||
|
this.pendingRequests = [];
|
||||||
let pendingSet;
|
for ( const entry of pendingRequests ) {
|
||||||
|
|
||||||
const handler = function(details) {
|
|
||||||
if ( pendingSet === undefined ) { return; }
|
|
||||||
if ( details.tabId < 0 ) { return; }
|
|
||||||
|
|
||||||
const pending = {
|
|
||||||
details: Object.assign({}, details),
|
|
||||||
resolve: undefined,
|
|
||||||
promise: undefined
|
|
||||||
};
|
|
||||||
|
|
||||||
pending.promise = new Promise(function(resolve) {
|
|
||||||
pending.resolve = resolve;
|
|
||||||
});
|
|
||||||
|
|
||||||
pendingSet.push(pending);
|
|
||||||
|
|
||||||
return pending.promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
start: function() {
|
|
||||||
pendingSet = [];
|
|
||||||
browser.webRequest.onBeforeRequest.addListener(
|
|
||||||
handler,
|
|
||||||
{ urls: [ 'http://*/*', 'https://*/*' ] },
|
|
||||||
[ 'blocking' ]
|
|
||||||
);
|
|
||||||
},
|
|
||||||
stop: function(resolver) {
|
|
||||||
if ( pendingSet === undefined ) { return; }
|
|
||||||
const resolvingSet = pendingSet; // not sure if re-entrance
|
|
||||||
pendingSet = undefined; // can occur...
|
|
||||||
for ( const entry of resolvingSet ) {
|
|
||||||
vAPI.net.normalizeDetails(entry.details);
|
|
||||||
entry.resolve(resolver(entry.details));
|
entry.resolve(resolver(entry.details));
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
canSuspend() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
|
@ -1,184 +0,0 @@
|
||||||
/*******************************************************************************
|
|
||||||
|
|
||||||
uBlock Origin - 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
|
|
||||||
*/
|
|
||||||
|
|
||||||
// For background page
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
(function() {
|
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/2950
|
|
||||||
// Firefox 56 does not normalize URLs to ASCII, uBO must do this itself.
|
|
||||||
// https://bugzilla.mozilla.org/show_bug.cgi?id=945240
|
|
||||||
const evalMustPunycode = function() {
|
|
||||||
return vAPI.webextFlavor.soup.has('firefox') &&
|
|
||||||
vAPI.webextFlavor.major < 57;
|
|
||||||
};
|
|
||||||
|
|
||||||
let mustPunycode = evalMustPunycode();
|
|
||||||
|
|
||||||
// The real actual webextFlavor value may not be set in stone, so listen
|
|
||||||
// for possible future changes.
|
|
||||||
window.addEventListener('webextFlavor', ( ) => {
|
|
||||||
mustPunycode = evalMustPunycode();
|
|
||||||
}, { once: true });
|
|
||||||
|
|
||||||
const denormalizeTypes = function(aa) {
|
|
||||||
if ( aa.length === 0 ) {
|
|
||||||
return Array.from(vAPI.net.validTypes);
|
|
||||||
}
|
|
||||||
const out = new Set();
|
|
||||||
let i = aa.length;
|
|
||||||
while ( i-- ) {
|
|
||||||
let type = aa[i];
|
|
||||||
if ( vAPI.net.validTypes.has(type) ) {
|
|
||||||
out.add(type);
|
|
||||||
}
|
|
||||||
if ( type === 'image' && vAPI.net.validTypes.has('imageset') ) {
|
|
||||||
out.add('imageset');
|
|
||||||
}
|
|
||||||
if ( type === 'sub_frame' ) {
|
|
||||||
out.add('object');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Array.from(out);
|
|
||||||
};
|
|
||||||
|
|
||||||
const punycode = self.punycode;
|
|
||||||
const reAsciiHostname = /^https?:\/\/[0-9a-z_.:@-]+[/?#]/;
|
|
||||||
const parsedURL = new URL('about:blank');
|
|
||||||
|
|
||||||
vAPI.net.normalizeDetails = function(details) {
|
|
||||||
if ( mustPunycode && !reAsciiHostname.test(details.url) ) {
|
|
||||||
parsedURL.href = details.url;
|
|
||||||
details.url = details.url.replace(
|
|
||||||
parsedURL.hostname,
|
|
||||||
punycode.toASCII(parsedURL.hostname)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const type = details.type;
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/345
|
|
||||||
// Re-categorize an embedded object as a `sub_frame` if its
|
|
||||||
// content type is that of a HTML document.
|
|
||||||
if ( type === 'object' && Array.isArray(details.responseHeaders) ) {
|
|
||||||
for ( const header of details.responseHeaders ) {
|
|
||||||
if ( header.name.toLowerCase() === 'content-type' ) {
|
|
||||||
if ( header.value.startsWith('text/html') ) {
|
|
||||||
details.type = 'sub_frame';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
vAPI.net.denormalizeFilters = function(filters) {
|
|
||||||
let urls = filters.urls || [ '<all_urls>' ];
|
|
||||||
if ( urls.indexOf('https://*/*') !== -1 ) {
|
|
||||||
urls = [ '<all_urls>' ];
|
|
||||||
}
|
|
||||||
let types = filters.types;
|
|
||||||
if ( Array.isArray(types) ) {
|
|
||||||
types = denormalizeTypes(types);
|
|
||||||
}
|
|
||||||
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://*/*');
|
|
||||||
}
|
|
||||||
if ( urls.indexOf('wss://*/*') === -1 ) {
|
|
||||||
urls.push('wss://*/*');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return { types, urls };
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
// Related issues:
|
|
||||||
// - https://github.com/gorhill/uBlock/issues/1327
|
|
||||||
// - https://github.com/uBlockOrigin/uBlock-issues/issues/128
|
|
||||||
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1503721
|
|
||||||
|
|
||||||
vAPI.net.onBeforeReady = (function() {
|
|
||||||
let pendings;
|
|
||||||
|
|
||||||
const handler = function(details) {
|
|
||||||
if ( pendings === undefined ) { return; }
|
|
||||||
if ( details.tabId < 0 ) { return; }
|
|
||||||
|
|
||||||
const pending = {
|
|
||||||
details: Object.assign({}, details),
|
|
||||||
resolve: undefined,
|
|
||||||
promise: undefined
|
|
||||||
};
|
|
||||||
|
|
||||||
pending.promise = new Promise(function(resolve) {
|
|
||||||
pending.resolve = resolve;
|
|
||||||
});
|
|
||||||
|
|
||||||
pendings.push(pending);
|
|
||||||
|
|
||||||
return pending.promise;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
start: function() {
|
|
||||||
pendings = [];
|
|
||||||
browser.webRequest.onBeforeRequest.addListener(
|
|
||||||
handler,
|
|
||||||
{ urls: [ 'http://*/*', 'https://*/*' ] },
|
|
||||||
[ 'blocking' ]
|
|
||||||
);
|
|
||||||
},
|
|
||||||
stop: function(resolver) {
|
|
||||||
if ( pendings === undefined ) { return; }
|
|
||||||
for ( const pending of pendings ) {
|
|
||||||
vAPI.net.normalizeDetails(pending.details);
|
|
||||||
pending.resolve(resolver(pending.details));
|
|
||||||
}
|
|
||||||
pendings = undefined;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
/******************************************************************************/
|
|
|
@ -660,6 +660,8 @@
|
||||||
this.staticNetFilteringEngine.freeze();
|
this.staticNetFilteringEngine.freeze();
|
||||||
this.staticExtFilteringEngine.freeze();
|
this.staticExtFilteringEngine.freeze();
|
||||||
this.redirectEngine.freeze();
|
this.redirectEngine.freeze();
|
||||||
|
vAPI.net.unsuspend();
|
||||||
|
|
||||||
vAPI.storage.set({ 'availableFilterLists': this.availableFilterLists });
|
vAPI.storage.set({ 'availableFilterLists': this.availableFilterLists });
|
||||||
|
|
||||||
vAPI.messaging.broadcast({
|
vAPI.messaging.broadcast({
|
||||||
|
@ -702,8 +704,7 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
const onFilterListsReady = lists => {
|
const onFilterListsReady = lists => {
|
||||||
this.availableFilterLists = lists;
|
vAPI.net.suspend();
|
||||||
|
|
||||||
this.redirectEngine.reset();
|
this.redirectEngine.reset();
|
||||||
this.staticExtFilteringEngine.reset();
|
this.staticExtFilteringEngine.reset();
|
||||||
this.staticNetFilteringEngine.reset();
|
this.staticNetFilteringEngine.reset();
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
|
|
||||||
// Start isolation from global scope
|
// Start isolation from global scope
|
||||||
|
|
||||||
µBlock.webRequest = (function() {
|
µBlock.webRequest = (( ) => {
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
@ -1028,26 +1028,20 @@ const strictBlockBypasser = {
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
return {
|
return {
|
||||||
start: (function() {
|
start: (( ) => {
|
||||||
|
vAPI.net = new vAPI.Net();
|
||||||
|
|
||||||
if (
|
if (
|
||||||
vAPI.net.onBeforeReady instanceof Object &&
|
vAPI.net.canSuspend() &&
|
||||||
(
|
µBlock.hiddenSettings.suspendTabsUntilReady !== 'no' ||
|
||||||
vAPI.net.onBeforeReady.experimental !== true &&
|
vAPI.net.canSuspend() !== true &&
|
||||||
µBlock.hiddenSettings.suspendTabsUntilReady !== 'no' ||
|
µBlock.hiddenSettings.suspendTabsUntilReady === 'yes'
|
||||||
vAPI.net.onBeforeReady.experimental &&
|
|
||||||
µBlock.hiddenSettings.suspendTabsUntilReady === 'yes'
|
|
||||||
)
|
|
||||||
) {
|
) {
|
||||||
vAPI.net.onBeforeReady.start();
|
vAPI.net.suspend(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return function() {
|
return function() {
|
||||||
vAPI.net.addListener(
|
vAPI.net.setSuspendableListener(onBeforeRequest);
|
||||||
'onBeforeRequest',
|
|
||||||
onBeforeRequest,
|
|
||||||
{ urls: [ 'http://*/*', 'https://*/*' ] },
|
|
||||||
[ 'blocking' ]
|
|
||||||
);
|
|
||||||
vAPI.net.addListener(
|
vAPI.net.addListener(
|
||||||
'onHeadersReceived',
|
'onHeadersReceived',
|
||||||
onHeadersReceived,
|
onHeadersReceived,
|
||||||
|
@ -1074,9 +1068,7 @@ return {
|
||||||
[ 'blocking', 'requestBody' ]
|
[ 'blocking', 'requestBody' ]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if ( vAPI.net.onBeforeReady instanceof Object ) {
|
vAPI.net.unsuspend();
|
||||||
vAPI.net.onBeforeReady.stop(onBeforeRequest);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
})(),
|
})(),
|
||||||
|
|
||||||
|
|
|
@ -24,9 +24,9 @@ cp platform/chromium/*.html $DES/
|
||||||
cp platform/chromium/*.json $DES/
|
cp platform/chromium/*.json $DES/
|
||||||
cp LICENSE.txt $DES/
|
cp LICENSE.txt $DES/
|
||||||
|
|
||||||
cp platform/thunderbird/manifest.json $DES/
|
cp platform/thunderbird/manifest.json $DES/
|
||||||
cp platform/thunderbird/vapi-webrequest.js $DES/js/
|
cp platform/firefox/vapi-webrequest.js $DES/js/
|
||||||
cp platform/firefox/vapi-usercss.js $DES/js/
|
cp platform/firefox/vapi-usercss.js $DES/js/
|
||||||
|
|
||||||
echo "*** uBlock0.thunderbird: concatenating content scripts"
|
echo "*** uBlock0.thunderbird: concatenating content scripts"
|
||||||
cat $DES/js/vapi-usercss.js > /tmp/contentscript.js
|
cat $DES/js/vapi-usercss.js > /tmp/contentscript.js
|
||||||
|
|
Loading…
Reference in New Issue