1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-06 02:42:33 +01:00
uBlock/platform/firefox/vapi-webrequest.js

123 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-09-02 12:11:33 +02:00
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
2018-02-26 19:59:16 +01:00
Copyright (C) 2017-2018 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
*/
// For background page
'use strict';
/******************************************************************************/
(function() {
2017-09-02 12:11:33 +02:00
// https://github.com/gorhill/uBlock/issues/2950
// Firefox 56 does not normalize URLs to ASCII, uBO must do this itself.
2017-09-02 12:11:33 +02:00
// https://bugzilla.mozilla.org/show_bug.cgi?id=945240
2018-04-04 18:42:01 +02:00
let evalMustPunycode = function() {
return vAPI.webextFlavor.soup.has('firefox') &&
vAPI.webextFlavor.major < 57;
2018-04-04 18:42:01 +02:00
};
let mustPunycode = evalMustPunycode();
// The real actual webextFlavor value may not be set in stone, so listen
// for possible future changes.
window.addEventListener('webextFlavor', function() {
mustPunycode = evalMustPunycode();
}, { once: true });
2017-09-02 12:11:33 +02:00
let denormalizeTypes = function(aa) {
2017-09-02 12:11:33 +02:00
if ( aa.length === 0 ) {
return Array.from(vAPI.net.validTypes);
2017-09-02 12:11:33 +02:00
}
let out = new Set(),
2018-01-16 15:13:51 +01:00
i = aa.length;
2017-09-02 12:11:33 +02:00
while ( i-- ) {
let type = aa[i];
if ( vAPI.net.validTypes.has(type) ) {
2018-01-16 15:13:51 +01:00
out.add(type);
2017-09-02 12:11:33 +02:00
}
if ( type === 'image' && vAPI.net.validTypes.has('imageset') ) {
2018-01-16 15:13:51 +01:00
out.add('imageset');
2017-09-02 12:11:33 +02:00
}
}
2018-01-16 15:13:51 +01:00
return Array.from(out);
2017-09-02 12:11:33 +02:00
};
let punycode = self.punycode;
let reAsciiHostname = /^https?:\/\/[0-9a-z_.:@-]+[/?#]/;
let parsedURL = new URL('about:blank');
2017-09-02 12:11:33 +02:00
vAPI.net.normalizeDetails = function(details) {
2018-02-26 19:59:16 +01:00
if (
details.tabId === vAPI.noTabId &&
typeof details.documentUrl === 'string'
2018-02-26 19:59:16 +01:00
) {
details.tabId = vAPI.anyTabId;
}
2017-09-02 12:11:33 +02:00
if ( mustPunycode && !reAsciiHostname.test(details.url) ) {
2017-09-02 12:11:33 +02:00
parsedURL.href = details.url;
details.url = details.url.replace(
parsedURL.hostname,
punycode.toASCII(parsedURL.hostname)
);
}
let type = details.type;
2017-09-02 12:11:33 +02:00
// 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;
}
};
vAPI.net.denormalizeFilters = function(filters) {
let urls = filters.urls || [ '<all_urls>' ];
let types = filters.types;
if ( Array.isArray(types) ) {
types = denormalizeTypes(types);
}
2017-09-02 12:11:33 +02:00
if (
(vAPI.net.validTypes.has('websocket')) &&
2017-09-02 12:11:33 +02: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://*/*');
}
}
return { types, urls };
2017-10-18 21:00:22 +02:00
};
})();
2017-09-02 12:11:33 +02:00
/******************************************************************************/