2017-09-02 12:11:33 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2023-12-04 18:10:34 +01:00
|
|
|
uBlock Origin - a comprehensive, efficient content blocker
|
2018-10-28 14:58:25 +01:00
|
|
|
Copyright (C) 2017-present Raymond Hill
|
2017-09-02 12:11:33 +02: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-16 16:29:39 +02:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1659
|
|
|
|
// Chromium fails to dispatch onCreatedNavigationTarget() events sometimes,
|
|
|
|
// so we synthetize these missing events when this happens.
|
2021-12-14 17:23:46 +01:00
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/10323
|
|
|
|
// Also mind whether the new tab is launched from an external application.
|
2021-07-16 16:29:39 +02:00
|
|
|
|
|
|
|
vAPI.Tabs = class extends vAPI.Tabs {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.tabIds = new Set();
|
|
|
|
browser.tabs.onCreated.addListener(tab => {
|
|
|
|
this.onCreatedHandler(tab);
|
|
|
|
});
|
|
|
|
}
|
2021-07-16 20:06:59 +02:00
|
|
|
|
2021-07-16 16:29:39 +02:00
|
|
|
onCreatedHandler(tab) {
|
|
|
|
if ( typeof tab.openerTabId === 'number' ) { return; }
|
|
|
|
if ( tab.index !== 0 ) { return; }
|
|
|
|
if ( tab.url !== '' ) { return; }
|
|
|
|
this.tabIds.add(tab.id);
|
|
|
|
}
|
2021-07-16 20:06:59 +02:00
|
|
|
|
2021-07-16 16:29:39 +02:00
|
|
|
onCreatedNavigationTargetHandler(details) {
|
|
|
|
this.tabIds.delete(details.tabId);
|
|
|
|
super.onCreatedNavigationTargetHandler(details);
|
|
|
|
}
|
2021-07-16 20:06:59 +02:00
|
|
|
|
2021-07-16 16:29:39 +02:00
|
|
|
onCommittedHandler(details) {
|
2021-07-16 20:06:59 +02:00
|
|
|
if ( details.frameId === 0 ) {
|
|
|
|
this.synthesizeNavigationTargetEvent(details);
|
2021-07-16 16:29:39 +02:00
|
|
|
}
|
|
|
|
super.onCommittedHandler(details);
|
|
|
|
}
|
2021-07-16 20:06:59 +02:00
|
|
|
|
2021-07-16 16:29:39 +02:00
|
|
|
onRemovedHandler(tabId, details) {
|
|
|
|
this.tabIds.delete(tabId);
|
|
|
|
super.onRemovedHandler(tabId, details);
|
|
|
|
}
|
2021-07-16 20:06:59 +02:00
|
|
|
|
|
|
|
synthesizeNavigationTargetEvent(details) {
|
|
|
|
if ( this.tabIds.has(details.tabId) === false ) { return; }
|
|
|
|
this.tabIds.delete(details.tabId);
|
2021-12-14 17:23:46 +01:00
|
|
|
const isClientRedirect =
|
|
|
|
Array.isArray(details.transitionQualifiers) &&
|
|
|
|
details.transitionQualifiers.includes('client_redirect');
|
|
|
|
const isStartPage = details.transitionType === 'start_page';
|
|
|
|
if ( isClientRedirect === false && isStartPage === false ) { return; }
|
2021-07-16 20:06:59 +02:00
|
|
|
this.onCreatedNavigationTargetHandler({
|
|
|
|
tabId: details.tabId,
|
|
|
|
sourceTabId: details.tabId,
|
|
|
|
sourceFrameId: 0,
|
|
|
|
url: details.url,
|
|
|
|
});
|
|
|
|
}
|
2021-07-16 16:29:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2019-01-28 22:12:26 +01:00
|
|
|
|
2021-07-16 16:29:39 +02:00
|
|
|
{
|
2018-11-08 22:58:45 +01:00
|
|
|
const extToTypeMap = new Map([
|
2017-09-02 12:11:33 +02:00
|
|
|
['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']
|
|
|
|
]);
|
|
|
|
|
2018-11-08 22:58:45 +01:00
|
|
|
const parsedURL = new URL('https://www.example.org/');
|
2018-10-28 14:58:25 +01:00
|
|
|
|
2024-10-02 19:39:36 +02:00
|
|
|
// Extend base class to normalize as per platform
|
2017-09-02 12:11:33 +02:00
|
|
|
|
2019-06-30 16:09:27 +02:00
|
|
|
vAPI.Net = class extends vAPI.Net {
|
|
|
|
normalizeDetails(details) {
|
|
|
|
// Chromium 63+ supports the `initiator` property, which contains
|
2024-10-02 19:39:36 +02:00
|
|
|
// the URL of the origin from which the network request was made
|
|
|
|
if ( details.initiator && details.initiator !== 'null' ) {
|
2019-06-30 16:09:27 +02:00
|
|
|
details.documentUrl = details.initiator;
|
|
|
|
}
|
2024-10-02 19:39:36 +02:00
|
|
|
const type = details.type;
|
2019-06-30 16:09:27 +02:00
|
|
|
if ( type === 'imageset' ) {
|
2017-09-02 12:11:33 +02:00
|
|
|
details.type = 'image';
|
|
|
|
return;
|
|
|
|
}
|
2019-06-30 16:09:27 +02:00
|
|
|
if ( type !== 'other' ) { return; }
|
2024-10-02 19:39:36 +02:00
|
|
|
// Try to map known "extension" part of URL to request type
|
|
|
|
if ( details.responseHeaders === undefined ) {
|
|
|
|
parsedURL.href = details.url;
|
|
|
|
const path = parsedURL.pathname;
|
|
|
|
const pos = path.indexOf('.', path.length - 6);
|
|
|
|
if ( pos !== -1 ) {
|
|
|
|
details.type = extToTypeMap.get(path.slice(pos + 1)) || type;
|
|
|
|
}
|
2017-09-02 12:11:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-10-02 19:39:36 +02:00
|
|
|
// Try to extract type from response headers
|
|
|
|
const ctype = this.headerValue(details.responseHeaders, 'content-type');
|
|
|
|
if ( ctype.startsWith('font/') ) {
|
|
|
|
details.type = 'font';
|
|
|
|
} else if ( ctype.startsWith('image/') ) {
|
|
|
|
details.type = 'image';
|
|
|
|
} else if ( ctype.startsWith('audio/') || ctype.startsWith('video/') ) {
|
|
|
|
details.type = 'media';
|
2019-06-30 16:09:27 +02:00
|
|
|
}
|
2017-09-02 12:11:33 +02:00
|
|
|
}
|
2021-07-16 20:06:59 +02:00
|
|
|
|
2019-06-30 16:09:27 +02:00
|
|
|
// https://www.reddit.com/r/uBlockOrigin/comments/9vcrk3/
|
|
|
|
// Some types can be mapped from 'other', thus include 'other' if and
|
2024-10-02 19:39:36 +02:00
|
|
|
// only if the caller is interested in at least one of those types
|
2019-06-30 16:09:27 +02:00
|
|
|
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);
|
|
|
|
}
|
2017-09-02 12:11:33 +02:00
|
|
|
}
|
2019-06-30 16:09:27 +02:00
|
|
|
if ( out.has('other') === false ) {
|
|
|
|
for ( const type of extToTypeMap.values() ) {
|
|
|
|
if ( out.has(type) ) {
|
|
|
|
out.add('other');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-02 12:11:33 +02:00
|
|
|
}
|
2019-06-30 16:09:27 +02:00
|
|
|
return Array.from(out);
|
2017-09-02 12:11:33 +02:00
|
|
|
}
|
2021-07-16 20:06:59 +02:00
|
|
|
|
2022-03-30 18:15:44 +02:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/2063
|
|
|
|
// Do not interfere with root document
|
2019-06-30 16:09:27 +02:00
|
|
|
suspendOneRequest(details) {
|
2023-04-12 22:10:00 +02:00
|
|
|
this.onBeforeSuspendableRequest(details);
|
2022-03-30 18:15:44 +02:00
|
|
|
if ( details.type === 'main_frame' ) { return; }
|
2023-04-12 22:10:00 +02:00
|
|
|
return { cancel: true };
|
2019-06-30 16:09:27 +02:00
|
|
|
}
|
2021-07-16 20:06:59 +02:00
|
|
|
|
2021-12-30 15:24:38 +01:00
|
|
|
unsuspendAllRequests(discard = false) {
|
2023-04-12 22:10:00 +02:00
|
|
|
if ( discard === true ) { return; }
|
|
|
|
const toReload = [];
|
|
|
|
for ( const tabId of this.unprocessedTabs.keys() ) {
|
|
|
|
toReload.push(tabId);
|
|
|
|
}
|
|
|
|
this.removeUnprocessedRequest();
|
|
|
|
for ( const tabId of toReload ) {
|
|
|
|
vAPI.tabs.reload(tabId);
|
2018-12-23 23:59:31 +01:00
|
|
|
}
|
2019-06-30 16:09:27 +02:00
|
|
|
}
|
2018-12-23 23:59:31 +01:00
|
|
|
};
|
2021-07-16 16:29:39 +02:00
|
|
|
}
|
2018-12-23 23:59:31 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2019-04-27 19:12:33 +02:00
|
|
|
|
|
|
|
// 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".
|
|
|
|
|
2019-06-30 16:09:27 +02:00
|
|
|
vAPI.prefetching = (( ) => {
|
2019-04-27 19:12:33 +02:00
|
|
|
let listening = false;
|
|
|
|
|
|
|
|
const onHeadersReceived = function(details) {
|
|
|
|
details.responseHeaders.push({
|
|
|
|
name: 'X-DNS-Prefetch-Control',
|
|
|
|
value: 'off'
|
|
|
|
});
|
|
|
|
return { responseHeaders: details.responseHeaders };
|
|
|
|
};
|
|
|
|
|
|
|
|
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' ]
|
|
|
|
);
|
|
|
|
listening = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2022-12-12 20:02:57 +01:00
|
|
|
|
2023-05-09 18:44:14 +02:00
|
|
|
vAPI.scriptletsInjector = ((doc, details) => {
|
2022-12-12 20:02:57 +01:00
|
|
|
let script;
|
|
|
|
try {
|
|
|
|
script = doc.createElement('script');
|
2023-05-09 18:44:14 +02:00
|
|
|
script.appendChild(doc.createTextNode(details.scriptlets));
|
2022-12-12 20:02:57 +01:00
|
|
|
(doc.head || doc.documentElement).appendChild(script);
|
2023-05-09 18:44:14 +02:00
|
|
|
self.uBO_scriptletsInjected = details.filters;
|
2022-12-12 20:02:57 +01:00
|
|
|
} catch (ex) {
|
|
|
|
}
|
|
|
|
if ( script ) {
|
|
|
|
script.remove();
|
|
|
|
script.textContent = '';
|
|
|
|
}
|
|
|
|
}).toString();
|
|
|
|
|
|
|
|
/******************************************************************************/
|