mirror of
https://github.com/gorhill/uBlock.git
synced 2026-03-11 09:04:36 +00:00
Related issue: https://github.com/uBlockOrigin/uBOL-home/issues/326 uBOL now asks broad host permissions by default. Users can still choose narrow host permissions by using their browser's controls for this. For instance in Chromium, those host permissions controls are found in the "Site access" section in the detailed view of an extension. One can set "Site access" to "On click" to revoke broad host permissions, and grant host permissions to only specific site. In such mode, uBOL will still block through the DNR API, but no cosmetic or scriptlet filtering will occurs, as these requires permission to "read and change data" on websites for which higher filtering mode is desired. Some browsers do not automatically grant broad host permissions even when an extension asks for broad permissions at install time, and going forward all browsers will likely adopt this approach, and thus it no longer made sense for uBOL to default to no broad hosts permissions at install time, especially given this leads to issues with no solution -- issues solved with the new approach (e.g. like the ability to deploy uBOL in Optimal mode by default).
161 lines
5.1 KiB
JavaScript
161 lines
5.1 KiB
JavaScript
/*******************************************************************************
|
|
|
|
uBlock Origin Lite - a comprehensive, MV3-compliant content blocker
|
|
Copyright (C) 2022-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
|
|
*/
|
|
|
|
import { browser } from './ext.js';
|
|
|
|
/******************************************************************************/
|
|
|
|
function parsedURLromOrigin(origin) {
|
|
try {
|
|
return new URL(origin);
|
|
} catch {
|
|
}
|
|
}
|
|
|
|
/******************************************************************************/
|
|
|
|
const toBroaderHostname = hn => {
|
|
if ( hn === '*' ) { return ''; }
|
|
const pos = hn.indexOf('.');
|
|
return pos !== -1 ? hn.slice(pos+1) : '*';
|
|
};
|
|
|
|
/******************************************************************************/
|
|
|
|
// Is hna descendant hostname of hnb?
|
|
|
|
const isDescendantHostname = (hna, hnb) => {
|
|
if ( hnb === 'all-urls' ) { return true; }
|
|
if ( hna.endsWith(hnb) === false ) { return false; }
|
|
if ( hna === hnb ) { return false; }
|
|
return hna.charCodeAt(hna.length - hnb.length - 1) === 0x2E /* '.' */;
|
|
};
|
|
|
|
/**
|
|
* Returns whether a hostname is part of a collection, or is descendant of an
|
|
* item in the collection.
|
|
* @param hna - the hostname representing the needle.
|
|
* @param iterb - an iterable representing the haystack of hostnames.
|
|
*/
|
|
|
|
const isDescendantHostnameOfIter = (hna, iterb) => {
|
|
const setb = iterb instanceof Set ? iterb : new Set(iterb);
|
|
if ( setb.has('all-urls') || setb.has('*') ) { return true; }
|
|
let hn = hna;
|
|
while ( hn ) {
|
|
const pos = hn.indexOf('.');
|
|
if ( pos === -1 ) { break; }
|
|
hn = hn.slice(pos + 1);
|
|
if ( setb.has(hn) ) { return true; }
|
|
}
|
|
return false;
|
|
};
|
|
|
|
/**
|
|
* Returns all hostnames in the first collection which are equal or descendant
|
|
* of hostnames in the second collection.
|
|
* @param itera - an iterable which hostnames must be filtered out.
|
|
* @param iterb - an iterable which hostnames must be matched.
|
|
*/
|
|
|
|
const intersectHostnameIters = (itera, iterb) => {
|
|
const setb = iterb instanceof Set ? iterb : new Set(iterb);
|
|
if ( setb.has('all-urls') || setb.has('*') ) { return Array.from(itera); }
|
|
const out = [];
|
|
for ( const hna of itera ) {
|
|
if ( setb.has(hna) || isDescendantHostnameOfIter(hna, setb) ) {
|
|
out.push(hna);
|
|
}
|
|
}
|
|
return out;
|
|
};
|
|
|
|
const subtractHostnameIters = (itera, iterb) => {
|
|
const setb = iterb instanceof Set ? iterb : new Set(iterb);
|
|
if ( setb.has('all-urls') || setb.has('*') ) { return []; }
|
|
const out = [];
|
|
for ( const hna of itera ) {
|
|
if ( setb.has(hna) ) { continue; }
|
|
if ( isDescendantHostnameOfIter(hna, setb) ) { continue; }
|
|
out.push(hna);
|
|
}
|
|
return out;
|
|
};
|
|
|
|
/******************************************************************************/
|
|
|
|
const matchFromHostname = hn =>
|
|
hn === '*' || hn === 'all-urls' ? '<all_urls>' : `*://*.${hn}/*`;
|
|
|
|
const matchesFromHostnames = hostnames => {
|
|
const out = [];
|
|
for ( const hn of hostnames ) {
|
|
out.push(matchFromHostname(hn));
|
|
}
|
|
return out;
|
|
};
|
|
|
|
const hostnamesFromMatches = origins => {
|
|
const out = [];
|
|
for ( const origin of origins ) {
|
|
if ( origin === '<all_urls>' ) {
|
|
out.push('all-urls');
|
|
continue;
|
|
}
|
|
const match = /^\*:\/\/(?:\*\.)?([^/]+)\/\*/.exec(origin);
|
|
if ( match === null ) { continue; }
|
|
out.push(match[1]);
|
|
}
|
|
return out;
|
|
};
|
|
|
|
/******************************************************************************/
|
|
|
|
const broadcastMessage = message => {
|
|
const bc = new self.BroadcastChannel('uBOL');
|
|
bc.postMessage(message);
|
|
};
|
|
|
|
/******************************************************************************/
|
|
|
|
// https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/manifest.json/host_permissions#requested_permissions_and_user_prompts
|
|
// "Users can grant or revoke host permissions on an ad hoc basis. Therefore,
|
|
// most browsers treat host_permissions as optional."
|
|
|
|
async function hasBroadHostPermissions() {
|
|
return browser.permissions.contains({ origins: [ '<all_urls>' ] });
|
|
}
|
|
|
|
/******************************************************************************/
|
|
|
|
export {
|
|
broadcastMessage,
|
|
parsedURLromOrigin,
|
|
toBroaderHostname,
|
|
isDescendantHostname,
|
|
isDescendantHostnameOfIter,
|
|
intersectHostnameIters,
|
|
subtractHostnameIters,
|
|
matchFromHostname,
|
|
matchesFromHostnames,
|
|
hostnamesFromMatches,
|
|
hasBroadHostPermissions,
|
|
};
|