2022-09-13 23:44:24 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2023-12-04 18:10:34 +01:00
|
|
|
uBlock Origin Lite - a comprehensive, MV3-compliant content blocker
|
2022-09-13 23:44:24 +02:00
|
|
|
Copyright (C) 2014-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
|
|
|
|
*/
|
|
|
|
|
2024-03-21 18:57:50 +01:00
|
|
|
import { browser, localRead, localWrite, sendMessage } from './ext.js';
|
2022-09-13 23:44:24 +02:00
|
|
|
import { dom, qs$, qsa$ } from './dom.js';
|
2024-03-21 18:57:50 +01:00
|
|
|
import { i18n, i18n$ } from './i18n.js';
|
2023-11-29 03:13:44 +01:00
|
|
|
import punycode from './punycode.js';
|
2022-09-13 23:44:24 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-30 01:51:33 +02:00
|
|
|
const rulesetMap = new Map();
|
2022-09-13 23:44:24 +02:00
|
|
|
let cachedRulesetData = {};
|
|
|
|
let hideUnusedSet = new Set([ 'regions' ]);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-30 01:51:33 +02:00
|
|
|
function renderNumber(value) {
|
2022-09-13 23:44:24 +02:00
|
|
|
return value.toLocaleString();
|
2022-09-30 01:51:33 +02:00
|
|
|
}
|
|
|
|
|
2023-11-29 22:11:22 +01:00
|
|
|
function hashFromIterable(iter) {
|
|
|
|
return Array.from(iter).sort().join('\n');
|
|
|
|
}
|
|
|
|
|
2022-09-30 01:51:33 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function rulesetStats(rulesetId) {
|
2022-11-05 16:09:06 +01:00
|
|
|
const hasOmnipotence = cachedRulesetData.defaultFilteringMode > 1;
|
2022-09-30 01:51:33 +02:00
|
|
|
const rulesetDetails = rulesetMap.get(rulesetId);
|
|
|
|
if ( rulesetDetails === undefined ) { return; }
|
|
|
|
const { rules, filters } = rulesetDetails;
|
2022-10-16 18:05:24 +02:00
|
|
|
let ruleCount = rules.plain + rules.regex;
|
2022-11-05 16:09:06 +01:00
|
|
|
if ( hasOmnipotence ) {
|
2023-07-10 17:56:57 +02:00
|
|
|
ruleCount += rules.removeparam + rules.redirect + rules.modifyHeaders;
|
2022-09-30 01:51:33 +02:00
|
|
|
}
|
|
|
|
const filterCount = filters.accepted;
|
|
|
|
return { ruleCount, filterCount };
|
|
|
|
}
|
2022-09-13 23:44:24 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-11-29 22:11:22 +01:00
|
|
|
function renderFilterLists() {
|
2022-09-13 23:44:24 +02:00
|
|
|
const { enabledRulesets, rulesetDetails } = cachedRulesetData;
|
|
|
|
const listGroupTemplate = qs$('#templates .groupEntry');
|
|
|
|
const listEntryTemplate = qs$('#templates .listEntry');
|
|
|
|
const listStatsTemplate = i18n$('perRulesetStats');
|
|
|
|
const groupNames = new Map([ [ 'user', '' ] ]);
|
|
|
|
|
|
|
|
const liFromListEntry = function(ruleset, li, hideUnused) {
|
|
|
|
if ( !li ) {
|
2022-11-12 15:51:22 +01:00
|
|
|
li = dom.clone(listEntryTemplate);
|
2022-09-13 23:44:24 +02:00
|
|
|
}
|
|
|
|
const on = enabledRulesets.includes(ruleset.id);
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.cl.toggle(li, 'checked', on);
|
2023-11-29 22:11:22 +01:00
|
|
|
dom.cl.toggle(li, 'unused', hideUnused && !on);
|
|
|
|
qs$(li, 'input[type="checkbox"]').checked = on;
|
2022-09-13 23:44:24 +02:00
|
|
|
if ( dom.attr(li, 'data-listkey') !== ruleset.id ) {
|
|
|
|
dom.attr(li, 'data-listkey', ruleset.id);
|
2023-06-11 15:20:10 +02:00
|
|
|
qs$(li, '.listname').append(i18n.patchUnicodeFlags(ruleset.name));
|
2022-09-15 19:14:08 +02:00
|
|
|
dom.cl.remove(li, 'toRemove');
|
|
|
|
if ( ruleset.homeURL ) {
|
|
|
|
dom.cl.add(li, 'support');
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.attr(qs$(li, 'a.support'), 'href', ruleset.homeURL);
|
2022-09-13 23:44:24 +02:00
|
|
|
} else {
|
2022-09-15 19:14:08 +02:00
|
|
|
dom.cl.remove(li, 'support');
|
2022-09-13 23:44:24 +02:00
|
|
|
}
|
|
|
|
if ( ruleset.instructionURL ) {
|
2022-09-15 19:14:08 +02:00
|
|
|
dom.cl.add(li, 'mustread');
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.attr(qs$(li, 'a.mustread'), 'href', ruleset.instructionURL);
|
2022-09-13 23:44:24 +02:00
|
|
|
} else {
|
2022-09-15 19:14:08 +02:00
|
|
|
dom.cl.remove(li, 'mustread');
|
2022-09-13 23:44:24 +02:00
|
|
|
}
|
2023-11-29 22:11:22 +01:00
|
|
|
dom.cl.toggle(li, 'isDefault', ruleset.id === 'default');
|
2022-09-13 23:44:24 +02:00
|
|
|
}
|
2022-09-30 01:51:33 +02:00
|
|
|
const stats = rulesetStats(ruleset.id);
|
2022-09-13 23:44:24 +02:00
|
|
|
li.title = listStatsTemplate
|
2022-09-30 01:51:33 +02:00
|
|
|
.replace('{{ruleCount}}', renderNumber(stats.ruleCount))
|
|
|
|
.replace('{{filterCount}}', renderNumber(stats.filterCount));
|
|
|
|
dom.attr(
|
2022-11-12 15:51:22 +01:00
|
|
|
qs$(li, '.input.checkbox'),
|
2022-09-30 01:51:33 +02:00
|
|
|
'disabled',
|
|
|
|
stats.ruleCount === 0 ? '' : null
|
|
|
|
);
|
|
|
|
dom.cl.remove(li, 'discard');
|
2022-09-13 23:44:24 +02:00
|
|
|
return li;
|
|
|
|
};
|
|
|
|
|
|
|
|
const listEntryCountFromGroup = function(groupRulesets) {
|
|
|
|
if ( Array.isArray(groupRulesets) === false ) { return ''; }
|
|
|
|
let count = 0,
|
|
|
|
total = 0;
|
|
|
|
for ( const ruleset of groupRulesets ) {
|
|
|
|
if ( enabledRulesets.includes(ruleset.id) ) {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
total += 1;
|
|
|
|
}
|
|
|
|
return total !== 0 ?
|
|
|
|
`(${count.toLocaleString()}/${total.toLocaleString()})` :
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
const liFromListGroup = function(groupKey, groupRulesets) {
|
|
|
|
let liGroup = qs$(`#lists > .groupEntry[data-groupkey="${groupKey}"]`);
|
|
|
|
if ( liGroup === null ) {
|
2022-11-12 15:51:22 +01:00
|
|
|
liGroup = dom.clone(listGroupTemplate);
|
2022-09-13 23:44:24 +02:00
|
|
|
let groupName = groupNames.get(groupKey);
|
|
|
|
if ( groupName === undefined ) {
|
|
|
|
groupName = i18n$('3pGroup' + groupKey.charAt(0).toUpperCase() + groupKey.slice(1));
|
|
|
|
groupNames.set(groupKey, groupName);
|
|
|
|
}
|
|
|
|
if ( groupName !== '' ) {
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.text(qs$(liGroup, '.geName'), groupName);
|
2022-09-13 23:44:24 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-12 15:51:22 +01:00
|
|
|
if ( qs$(liGroup, '.geName:empty') === null ) {
|
|
|
|
dom.text(
|
|
|
|
qs$(liGroup, '.geCount'),
|
|
|
|
listEntryCountFromGroup(groupRulesets)
|
|
|
|
);
|
2022-09-13 23:44:24 +02:00
|
|
|
}
|
|
|
|
const hideUnused = mustHideUnusedLists(groupKey);
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.cl.toggle(liGroup, 'hideUnused', hideUnused);
|
|
|
|
const ulGroup = qs$(liGroup, '.listEntries');
|
2022-09-13 23:44:24 +02:00
|
|
|
if ( !groupRulesets ) { return liGroup; }
|
|
|
|
groupRulesets.sort(function(a, b) {
|
|
|
|
return (a.name || '').localeCompare(b.name || '');
|
|
|
|
});
|
|
|
|
for ( let i = 0; i < groupRulesets.length; i++ ) {
|
|
|
|
const liEntry = liFromListEntry(
|
|
|
|
groupRulesets[i],
|
|
|
|
ulGroup.children[i],
|
|
|
|
hideUnused
|
|
|
|
);
|
|
|
|
if ( liEntry.parentElement === null ) {
|
|
|
|
ulGroup.appendChild(liEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return liGroup;
|
|
|
|
};
|
|
|
|
|
2023-06-04 03:47:40 +02:00
|
|
|
// Visually split the filter lists in groups
|
2022-09-13 23:44:24 +02:00
|
|
|
const ulLists = qs$('#lists');
|
|
|
|
const groups = new Map([
|
|
|
|
[
|
|
|
|
'default',
|
|
|
|
rulesetDetails.filter(ruleset =>
|
|
|
|
ruleset.id === 'default'
|
|
|
|
),
|
|
|
|
],
|
2023-06-04 03:47:40 +02:00
|
|
|
[
|
|
|
|
'annoyances',
|
|
|
|
rulesetDetails.filter(ruleset =>
|
|
|
|
ruleset.group === 'annoyances'
|
|
|
|
),
|
|
|
|
],
|
2022-09-13 23:44:24 +02:00
|
|
|
[
|
|
|
|
'misc',
|
|
|
|
rulesetDetails.filter(ruleset =>
|
2023-06-04 03:47:40 +02:00
|
|
|
ruleset.id !== 'default' &&
|
|
|
|
ruleset.group === undefined &&
|
|
|
|
typeof ruleset.lang !== 'string'
|
2022-09-13 23:44:24 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'regions',
|
|
|
|
rulesetDetails.filter(ruleset =>
|
|
|
|
typeof ruleset.lang === 'string'
|
|
|
|
),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
dom.cl.toggle(dom.body, 'hideUnused', mustHideUnusedLists('*'));
|
2022-09-13 23:44:24 +02:00
|
|
|
|
|
|
|
for ( const [ groupKey, groupRulesets ] of groups ) {
|
2022-11-12 15:51:22 +01:00
|
|
|
const liGroup = liFromListGroup(groupKey, groupRulesets);
|
|
|
|
dom.attr(liGroup, 'data-groupkey', groupKey);
|
2022-09-13 23:44:24 +02:00
|
|
|
if ( liGroup.parentElement === null ) {
|
|
|
|
ulLists.appendChild(liGroup);
|
|
|
|
}
|
|
|
|
}
|
2022-09-28 01:51:38 +02:00
|
|
|
}
|
2022-09-13 23:44:24 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-11-29 22:11:22 +01:00
|
|
|
function renderWidgets() {
|
2022-10-10 18:28:24 +02:00
|
|
|
if ( cachedRulesetData.firstRun ) {
|
|
|
|
dom.cl.add(dom.body, 'firstRun');
|
|
|
|
}
|
2022-09-30 15:18:52 +02:00
|
|
|
|
2023-11-29 22:11:22 +01:00
|
|
|
renderDefaultMode();
|
|
|
|
renderTrustedSites();
|
2022-09-28 01:51:38 +02:00
|
|
|
|
2024-03-21 18:57:50 +01:00
|
|
|
qs$('#autoReload input[type="checkbox"]').checked = cachedRulesetData.autoReload;
|
|
|
|
|
|
|
|
{
|
|
|
|
const input = qs$('#showBlockedCount input[type="checkbox"]');
|
|
|
|
if ( cachedRulesetData.canShowBlockedCount ) {
|
|
|
|
input.checked = cachedRulesetData.showBlockedCount;
|
|
|
|
} else {
|
|
|
|
input.checked = false;
|
|
|
|
dom.attr(input, 'disabled', '');
|
|
|
|
}
|
|
|
|
}
|
2022-09-13 23:44:24 +02:00
|
|
|
|
|
|
|
// Compute total counts
|
2023-07-09 15:25:38 +02:00
|
|
|
let rulesetCount = 0;
|
2022-09-13 23:44:24 +02:00
|
|
|
let filterCount = 0;
|
|
|
|
let ruleCount = 0;
|
|
|
|
for ( const liEntry of qsa$('#lists .listEntry[data-listkey]') ) {
|
2023-07-09 15:25:38 +02:00
|
|
|
if ( qs$(liEntry, 'input[type="checkbox"]:checked') === null ) { continue; }
|
|
|
|
rulesetCount += 1;
|
2022-09-30 01:51:33 +02:00
|
|
|
const stats = rulesetStats(liEntry.dataset.listkey);
|
|
|
|
if ( stats === undefined ) { continue; }
|
|
|
|
ruleCount += stats.ruleCount;
|
|
|
|
filterCount += stats.filterCount;
|
2022-09-13 23:44:24 +02:00
|
|
|
}
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.text('#listsOfBlockedHostsPrompt', i18n$('perRulesetStats')
|
2022-09-13 23:44:24 +02:00
|
|
|
.replace('{{ruleCount}}', ruleCount.toLocaleString())
|
2022-11-12 15:51:22 +01:00
|
|
|
.replace('{{filterCount}}', filterCount.toLocaleString())
|
|
|
|
);
|
2023-07-09 15:25:38 +02:00
|
|
|
|
|
|
|
dom.cl.toggle(dom.body, 'noMoreRuleset',
|
|
|
|
rulesetCount === cachedRulesetData.maxNumberOfEnabledRulesets
|
|
|
|
);
|
2023-11-29 22:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function renderDefaultMode() {
|
|
|
|
const defaultLevel = cachedRulesetData.defaultFilteringMode;
|
|
|
|
if ( defaultLevel !== 0 ) {
|
|
|
|
qs$(`.filteringModeCard input[type="radio"][value="${defaultLevel}"]`).checked = true;
|
|
|
|
} else {
|
|
|
|
dom.prop('.filteringModeCard input[type="radio"]', 'checked', false);
|
|
|
|
}
|
|
|
|
}
|
2022-09-13 23:44:24 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-10 18:28:24 +02:00
|
|
|
async function onFilteringModeChange(ev) {
|
2022-09-28 01:51:38 +02:00
|
|
|
const input = ev.target;
|
2022-10-10 18:28:24 +02:00
|
|
|
const newLevel = parseInt(input.value, 10);
|
2022-09-28 02:19:59 +02:00
|
|
|
|
2022-10-10 18:28:24 +02:00
|
|
|
switch ( newLevel ) {
|
|
|
|
case 1: { // Revoke broad permissions
|
2023-06-13 17:40:41 +02:00
|
|
|
await browser.permissions.remove({
|
2022-09-28 02:19:59 +02:00
|
|
|
origins: [ '<all_urls>' ]
|
|
|
|
});
|
2023-06-13 17:40:41 +02:00
|
|
|
cachedRulesetData.defaultFilteringMode = 1;
|
2022-10-10 18:28:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
case 3: { // Request broad permissions
|
2023-06-13 17:40:41 +02:00
|
|
|
const granted = await browser.permissions.request({
|
2022-09-28 02:19:59 +02:00
|
|
|
origins: [ '<all_urls>' ]
|
2022-10-10 18:28:24 +02:00
|
|
|
});
|
2023-06-13 17:40:41 +02:00
|
|
|
if ( granted ) {
|
|
|
|
const actualLevel = await sendMessage({
|
|
|
|
what: 'setDefaultFilteringMode',
|
|
|
|
level: newLevel,
|
|
|
|
});
|
|
|
|
cachedRulesetData.defaultFilteringMode = actualLevel;
|
|
|
|
}
|
2022-10-10 18:28:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-11-29 22:11:22 +01:00
|
|
|
renderFilterLists();
|
2022-09-30 01:51:33 +02:00
|
|
|
renderWidgets();
|
2022-09-28 01:51:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dom.on(
|
2022-11-12 15:51:22 +01:00
|
|
|
'#defaultFilteringMode',
|
2022-09-28 01:51:38 +02:00
|
|
|
'change',
|
2022-10-10 18:28:24 +02:00
|
|
|
'.filteringModeCard input[type="radio"]',
|
|
|
|
ev => { onFilteringModeChange(ev); }
|
2022-09-28 01:51:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2024-03-21 18:57:50 +01:00
|
|
|
dom.on('#autoReload input[type="checkbox"]', 'change', ev => {
|
2022-10-10 18:28:24 +02:00
|
|
|
sendMessage({
|
|
|
|
what: 'setAutoReload',
|
|
|
|
state: ev.target.checked,
|
|
|
|
});
|
|
|
|
});
|
2022-09-13 23:44:24 +02:00
|
|
|
|
2024-03-21 18:57:50 +01:00
|
|
|
dom.on('#showBlockedCount input[type="checkbox"]', 'change', ev => {
|
|
|
|
sendMessage({
|
|
|
|
what: 'setShowBlockedCount',
|
|
|
|
state: ev.target.checked,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-11-29 22:11:22 +01:00
|
|
|
function renderTrustedSites() {
|
2023-11-29 03:13:44 +01:00
|
|
|
const textarea = qs$('#trustedSites');
|
2023-11-29 22:11:22 +01:00
|
|
|
const hostnames = cachedRulesetData.trustedSites;
|
2023-11-29 03:13:44 +01:00
|
|
|
textarea.value = hostnames.map(hn => punycode.toUnicode(hn)).join('\n');
|
|
|
|
if ( textarea.value !== '' ) {
|
|
|
|
textarea.value += '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function changeTrustedSites() {
|
2023-11-29 22:11:22 +01:00
|
|
|
const hostnames = getStagedTrustedSites();
|
|
|
|
const hash = hashFromIterable(cachedRulesetData.trustedSites);
|
|
|
|
if ( hashFromIterable(hostnames) === hash ) { return; }
|
|
|
|
sendMessage({
|
|
|
|
what: 'setTrustedSites',
|
|
|
|
hostnames,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStagedTrustedSites() {
|
2023-11-29 03:13:44 +01:00
|
|
|
const textarea = qs$('#trustedSites');
|
2023-11-29 22:11:22 +01:00
|
|
|
return textarea.value.split(/\s/).map(hn => {
|
2023-11-29 03:13:44 +01:00
|
|
|
try {
|
|
|
|
return punycode.toASCII(
|
|
|
|
(new URL(`https://${hn}/`)).hostname
|
|
|
|
);
|
|
|
|
} catch(_) {
|
|
|
|
}
|
|
|
|
return '';
|
2023-11-29 22:11:22 +01:00
|
|
|
}).filter(hn => hn !== '');
|
2023-11-29 03:13:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dom.on('#trustedSites', 'blur', changeTrustedSites);
|
|
|
|
|
|
|
|
self.addEventListener('beforeunload', changeTrustedSites);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-28 01:51:38 +02:00
|
|
|
async function applyEnabledRulesets() {
|
2022-09-13 23:44:24 +02:00
|
|
|
const enabledRulesets = [];
|
|
|
|
for ( const liEntry of qsa$('#lists .listEntry[data-listkey]') ) {
|
2023-07-09 15:25:38 +02:00
|
|
|
const checked = qs$(liEntry, 'input[type="checkbox"]:checked') !== null;
|
|
|
|
dom.cl.toggle(liEntry, 'checked', checked);
|
|
|
|
if ( checked === false ) { continue; }
|
2022-09-13 23:44:24 +02:00
|
|
|
enabledRulesets.push(liEntry.dataset.listkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
await sendMessage({
|
|
|
|
what: 'applyRulesets',
|
|
|
|
enabledRulesets,
|
|
|
|
});
|
|
|
|
|
|
|
|
renderWidgets();
|
2022-09-28 01:51:38 +02:00
|
|
|
}
|
2022-09-13 23:44:24 +02:00
|
|
|
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.on('#lists', 'change', '.listEntry input[type="checkbox"]', ( ) => {
|
2022-10-10 18:28:24 +02:00
|
|
|
applyEnabledRulesets();
|
|
|
|
});
|
2022-09-13 23:44:24 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Collapsing of unused lists.
|
|
|
|
|
2022-09-28 01:51:38 +02:00
|
|
|
function mustHideUnusedLists(which) {
|
2022-09-13 23:44:24 +02:00
|
|
|
const hideAll = hideUnusedSet.has('*');
|
|
|
|
if ( which === '*' ) { return hideAll; }
|
|
|
|
return hideUnusedSet.has(which) !== hideAll;
|
2022-09-28 01:51:38 +02:00
|
|
|
}
|
2022-09-13 23:44:24 +02:00
|
|
|
|
2022-09-28 01:51:38 +02:00
|
|
|
function toggleHideUnusedLists(which) {
|
2022-09-13 23:44:24 +02:00
|
|
|
const doesHideAll = hideUnusedSet.has('*');
|
|
|
|
let groupSelector;
|
|
|
|
let mustHide;
|
|
|
|
if ( which === '*' ) {
|
|
|
|
mustHide = doesHideAll === false;
|
|
|
|
groupSelector = '';
|
|
|
|
hideUnusedSet.clear();
|
|
|
|
if ( mustHide ) {
|
|
|
|
hideUnusedSet.add(which);
|
|
|
|
}
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.cl.toggle(dom.body, 'hideUnused', mustHide);
|
|
|
|
dom.cl.toggle('.groupEntry[data-groupkey]', 'hideUnused', mustHide);
|
2022-09-13 23:44:24 +02:00
|
|
|
} else {
|
|
|
|
const doesHide = hideUnusedSet.has(which);
|
|
|
|
if ( doesHide ) {
|
|
|
|
hideUnusedSet.delete(which);
|
|
|
|
} else {
|
|
|
|
hideUnusedSet.add(which);
|
|
|
|
}
|
|
|
|
mustHide = doesHide === doesHideAll;
|
|
|
|
groupSelector = `.groupEntry[data-groupkey="${which}"]`;
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.cl.toggle(groupSelector, 'hideUnused', mustHide);
|
2022-09-13 23:44:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for ( const elem of qsa$(`#lists ${groupSelector} .listEntry[data-listkey] input[type="checkbox"]:not(:checked)`) ) {
|
2022-09-15 19:14:08 +02:00
|
|
|
dom.cl.toggle(
|
2022-09-13 23:44:24 +02:00
|
|
|
elem.closest('.listEntry[data-listkey]'),
|
|
|
|
'unused',
|
|
|
|
mustHide
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-05 15:15:59 +02:00
|
|
|
localWrite('hideUnusedFilterLists', Array.from(hideUnusedSet));
|
2022-09-28 01:51:38 +02:00
|
|
|
}
|
2022-09-13 23:44:24 +02:00
|
|
|
|
2022-11-12 15:51:22 +01:00
|
|
|
dom.on('#lists', 'click', '.groupEntry[data-groupkey] > .geDetails', ev => {
|
|
|
|
toggleHideUnusedLists(
|
|
|
|
dom.attr(ev.target.closest('[data-groupkey]'), 'data-groupkey')
|
|
|
|
);
|
|
|
|
});
|
2022-09-13 23:44:24 +02:00
|
|
|
|
|
|
|
// Initialize from saved state.
|
2023-06-05 15:15:59 +02:00
|
|
|
localRead('hideUnusedFilterLists').then(value => {
|
|
|
|
if ( Array.isArray(value) === false ) { return; }
|
|
|
|
hideUnusedSet = new Set(value);
|
2022-09-13 23:44:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-11-29 22:11:22 +01:00
|
|
|
const bc = new self.BroadcastChannel('uBOL');
|
|
|
|
|
|
|
|
bc.onmessage = ev => {
|
|
|
|
const message = ev.data;
|
|
|
|
if ( message instanceof Object === false ) { return; }
|
|
|
|
const local = cachedRulesetData;
|
|
|
|
let render = false;
|
|
|
|
|
|
|
|
// Keep added sites which have not yet been committed
|
|
|
|
if ( message.trustedSites !== undefined ) {
|
|
|
|
if ( hashFromIterable(message.trustedSites) !== hashFromIterable(local.trustedSites) ) {
|
|
|
|
const current = new Set(local.trustedSites);
|
|
|
|
const staged = new Set(getStagedTrustedSites());
|
|
|
|
for ( const hn of staged ) {
|
|
|
|
if ( current.has(hn) === false ) { continue; }
|
|
|
|
staged.delete(hn);
|
|
|
|
}
|
|
|
|
const combined = Array.from(new Set([ ...message.trustedSites, ...staged ]));
|
|
|
|
local.trustedSites = combined;
|
|
|
|
render = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( message.defaultFilteringMode !== undefined ) {
|
|
|
|
if ( message.defaultFilteringMode !== local.defaultFilteringMode ) {
|
|
|
|
local.defaultFilteringMode = message.defaultFilteringMode;
|
|
|
|
render = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( message.autoReload !== undefined ) {
|
|
|
|
if ( message.autoReload !== local.autoReload ) {
|
|
|
|
local.autoReload = message.autoReload;
|
|
|
|
render = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-21 18:57:50 +01:00
|
|
|
if ( message.showBlockedCount !== undefined ) {
|
|
|
|
if ( message.showBlockedCount !== local.showBlockedCount ) {
|
|
|
|
local.showBlockedCount = message.showBlockedCount;
|
|
|
|
render = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 22:11:22 +01:00
|
|
|
if ( message.enabledRulesets !== undefined ) {
|
|
|
|
if ( hashFromIterable(message.enabledRulesets) !== hashFromIterable(local.enabledRulesets) ) {
|
|
|
|
local.enabledRulesets = message.enabledRulesets;
|
|
|
|
render = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( render === false ) { return; }
|
|
|
|
renderFilterLists();
|
|
|
|
renderWidgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
sendMessage({
|
2022-10-10 18:28:24 +02:00
|
|
|
what: 'getOptionsPageData',
|
2022-09-13 23:44:24 +02:00
|
|
|
}).then(data => {
|
|
|
|
if ( !data ) { return; }
|
|
|
|
cachedRulesetData = data;
|
2022-09-30 01:51:33 +02:00
|
|
|
rulesetMap.clear();
|
|
|
|
cachedRulesetData.rulesetDetails.forEach(rule => rulesetMap.set(rule.id, rule));
|
2022-09-13 23:44:24 +02:00
|
|
|
try {
|
|
|
|
renderFilterLists();
|
2023-11-29 03:13:44 +01:00
|
|
|
renderWidgets();
|
2022-09-13 23:44:24 +02:00
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
}).catch(reason => {
|
|
|
|
console.trace(reason);
|
|
|
|
});
|
|
|
|
|
|
|
|
/******************************************************************************/
|