2017-09-02 04:11:33 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-12-13 10:30:54 -07:00
|
|
|
Copyright (C) 2017-present Raymond Hill
|
2017-09-02 04:11:33 -06:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
// For background page
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-10-28 07:58:25 -06:00
|
|
|
(function() {
|
2019-01-28 14:12:26 -07:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
|
|
|
|
if ( vAPI.webextFlavor.soup.has('firefox') === false ) { return; }
|
2017-09-02 04:11:33 -06:00
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2950
|
2018-04-05 05:29:15 -06:00
|
|
|
// Firefox 56 does not normalize URLs to ASCII, uBO must do this itself.
|
2017-09-02 04:11:33 -06:00
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=945240
|
2018-12-16 08:51:25 -07:00
|
|
|
const evalMustPunycode = function() {
|
2018-04-05 05:29:15 -06:00
|
|
|
return vAPI.webextFlavor.soup.has('firefox') &&
|
|
|
|
vAPI.webextFlavor.major < 57;
|
2018-04-04 10:42:01 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let mustPunycode = evalMustPunycode();
|
|
|
|
|
|
|
|
// The real actual webextFlavor value may not be set in stone, so listen
|
|
|
|
// for possible future changes.
|
2018-12-16 08:51:25 -07:00
|
|
|
window.addEventListener('webextFlavor', ( ) => {
|
2018-04-04 10:42:01 -06:00
|
|
|
mustPunycode = evalMustPunycode();
|
|
|
|
}, { once: true });
|
2017-09-02 04:11:33 -06:00
|
|
|
|
2018-12-16 08:51:25 -07:00
|
|
|
const denormalizeTypes = function(aa) {
|
2017-09-02 04:11:33 -06:00
|
|
|
if ( aa.length === 0 ) {
|
2018-10-28 07:58:25 -06:00
|
|
|
return Array.from(vAPI.net.validTypes);
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
2018-12-16 08:51:25 -07:00
|
|
|
const out = new Set();
|
|
|
|
let i = aa.length;
|
2017-09-02 04:11:33 -06:00
|
|
|
while ( i-- ) {
|
2018-03-31 16:47:56 -06:00
|
|
|
let type = aa[i];
|
2018-10-28 07:58:25 -06:00
|
|
|
if ( vAPI.net.validTypes.has(type) ) {
|
2018-01-16 07:13:51 -07:00
|
|
|
out.add(type);
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
2018-10-28 07:58:25 -06:00
|
|
|
if ( type === 'image' && vAPI.net.validTypes.has('imageset') ) {
|
2018-01-16 07:13:51 -07:00
|
|
|
out.add('imageset');
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
2018-12-16 08:51:25 -07:00
|
|
|
if ( type === 'sub_frame' ) {
|
|
|
|
out.add('object');
|
|
|
|
}
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
2018-01-16 07:13:51 -07:00
|
|
|
return Array.from(out);
|
2017-09-02 04:11:33 -06:00
|
|
|
};
|
|
|
|
|
2018-12-16 08:51:25 -07:00
|
|
|
const punycode = self.punycode;
|
|
|
|
const reAsciiHostname = /^https?:\/\/[0-9a-z_.:@-]+[/?#]/;
|
|
|
|
const parsedURL = new URL('about:blank');
|
2017-09-02 04:11:33 -06:00
|
|
|
|
2018-10-28 07:58:25 -06:00
|
|
|
vAPI.net.normalizeDetails = function(details) {
|
2017-09-13 11:08:31 -06:00
|
|
|
if ( mustPunycode && !reAsciiHostname.test(details.url) ) {
|
2017-09-02 04:11:33 -06:00
|
|
|
parsedURL.href = details.url;
|
|
|
|
details.url = details.url.replace(
|
|
|
|
parsedURL.hostname,
|
|
|
|
punycode.toASCII(parsedURL.hostname)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-16 08:51:25 -07:00
|
|
|
const type = details.type;
|
2017-09-02 04:11:33 -06:00
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1493
|
2018-12-16 08:51:25 -07:00
|
|
|
// Chromium 49+/WebExtensions support a new request type: `ping`,
|
|
|
|
// which is fired as a result of using `navigator.sendBeacon`.
|
2017-09-02 04:11:33 -06:00
|
|
|
if ( type === 'ping' ) {
|
|
|
|
details.type = 'beacon';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( type === 'imageset' ) {
|
|
|
|
details.type = 'image';
|
|
|
|
return;
|
|
|
|
}
|
2018-12-16 08:51:25 -07:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-02 04:11:33 -06:00
|
|
|
};
|
|
|
|
|
2018-10-28 07:58:25 -06:00
|
|
|
vAPI.net.denormalizeFilters = function(filters) {
|
2018-12-16 08:51:25 -07:00
|
|
|
const urls = filters.urls || [ '<all_urls>' ];
|
2018-10-28 07:58:25 -06:00
|
|
|
let types = filters.types;
|
|
|
|
if ( Array.isArray(types) ) {
|
|
|
|
types = denormalizeTypes(types);
|
2018-03-31 16:47:56 -06:00
|
|
|
}
|
2017-09-02 04:11:33 -06:00
|
|
|
if (
|
2018-10-28 07:58:25 -06:00
|
|
|
(vAPI.net.validTypes.has('websocket')) &&
|
2017-09-02 04:11:33 -06:00
|
|
|
(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://*/*');
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 07:58:25 -06:00
|
|
|
return { types, urls };
|
2017-10-18 13:00:22 -06:00
|
|
|
};
|
2018-10-28 07:58:25 -06:00
|
|
|
})();
|
2017-09-02 04:11:33 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2018-12-23 15:59:31 -07:00
|
|
|
|
|
|
|
// Related issues:
|
2018-12-25 11:29:41 -07:00
|
|
|
// - https://github.com/gorhill/uBlock/issues/1327
|
2018-12-23 15:59:31 -07:00
|
|
|
// - https://github.com/uBlockOrigin/uBlock-issues/issues/128
|
|
|
|
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1503721
|
|
|
|
|
2019-01-28 14:12:26 -07:00
|
|
|
vAPI.net.onBeforeReady = vAPI.net.onBeforeReady || (function() {
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
|
|
|
|
if ( vAPI.webextFlavor.soup.has('firefox') === false ) { return; }
|
|
|
|
|
2019-02-12 05:13:37 -07:00
|
|
|
let pendingSet;
|
2018-12-23 15:59:31 -07:00
|
|
|
|
|
|
|
const handler = function(details) {
|
2019-02-12 05:13:37 -07:00
|
|
|
if ( pendingSet === undefined ) { return; }
|
2018-12-23 15:59:31 -07:00
|
|
|
if ( details.tabId < 0 ) { return; }
|
|
|
|
|
|
|
|
const pending = {
|
|
|
|
details: Object.assign({}, details),
|
|
|
|
resolve: undefined,
|
|
|
|
promise: undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
pending.promise = new Promise(function(resolve) {
|
|
|
|
pending.resolve = resolve;
|
|
|
|
});
|
|
|
|
|
2019-02-12 05:13:37 -07:00
|
|
|
pendingSet.push(pending);
|
2018-12-23 15:59:31 -07:00
|
|
|
|
|
|
|
return pending.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
start: function() {
|
2019-02-12 05:13:37 -07:00
|
|
|
pendingSet = [];
|
2018-12-23 15:59:31 -07:00
|
|
|
browser.webRequest.onBeforeRequest.addListener(
|
|
|
|
handler,
|
|
|
|
{ urls: [ 'http://*/*', 'https://*/*' ] },
|
|
|
|
[ 'blocking' ]
|
|
|
|
);
|
|
|
|
},
|
|
|
|
stop: function(resolver) {
|
2019-02-12 05:13:37 -07:00
|
|
|
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));
|
2018-12-23 15:59:31 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|