uBlock/platform/mv3/extension/js/settings.js

328 lines
10 KiB
JavaScript
Raw Permalink Normal View History

/*******************************************************************************
uBlock Origin Lite - a comprehensive, MV3-compliant content blocker
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
*/
import { browser, i18n, sendMessage } from './ext.js';
import { dom, qs$ } from './dom.js';
import { hashFromIterable } from './dashboard.js';
import { renderFilterLists } from './filter-lists.js';
/******************************************************************************/
let cachedRulesetData = {};
/******************************************************************************/
function renderAdminRules() {
const { disabledFeatures: forbid = [] } = cachedRulesetData;
if ( forbid.length === 0 ) { return; }
dom.body.dataset.forbid = forbid.join(' ');
if ( forbid.includes('dashboard') ) {
dom.body.dataset.pane = 'about';
}
}
/******************************************************************************/
function renderWidgets() {
if ( cachedRulesetData.firstRun ) {
dom.cl.add(dom.body, 'firstRun');
}
renderDefaultMode();
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', '');
}
}
{
const input = qs$('#strictBlockMode input[type="checkbox"]');
const canStrictBlock = cachedRulesetData.hasOmnipotence;
input.checked = canStrictBlock && cachedRulesetData.strictBlockMode;
dom.attr(input, 'disabled', canStrictBlock ? null : '');
}
{
[mv3] Add support for custom DNR rules This feature is hidden behind the "Developer mode" setting in the dashboard. When "Developer mode" is enabled, a tab named "Develop" will become available in the dashboard. This tab is meant to contain tools for technical users. At the moment, the "Develop" pane allows to create custom DNR rules through a (CodeMirror-based) editor. For the sake of convenience, the DNR rule must be entered in YAML-like format. The format is not really full compliant YAML, just YAML-like, and very strict in order to ensure the parser stays simple enough. Lines starting with `#` are comments and will be ignored by the parser. Any line which do not match the parser's expectation will be marked as invalid, and the whole DNR rule containing such invalid lines will be discarded. There must not be empty lines inside a rule definition. Each DNR rule must be separated with a `---` line, which is known as a YAML document separator. String values must not be quoted, otherwise the quotes will be considered part of the value. There is one exception: `''` will be parsed as "an empty string". The editor will attempt to auto-complete known DNR keywords. That feature will improve over time. Though the parser will identify some errors, not all invalid DNR rules are currently identified by the parser, and these will be reported when the rules are registered through the DNR API. Better identifying invalid DNR rules at edit time will improve over time. The editor will report `regexFilter` values which are not supported by the DNR engine on the current platform. The editor reacts to instances of `regexFilter: ...` to report whether a regex value is supported. This means you can test for a regex value by using `# regexFilter: ...` so that you do not have to create an actual DNR rules just for the sake of testing. Custom DNR rules can be exported into a JSON file (a format known by the DNR API as a "static ruleset"). JSON-based ruleset can be imported, the content will be converted to YAML-like syntax. The editor will attempt to convert to YAML pasted content which can be JSON-parsed. It's possible to paste partially or wholly JSON-based rulesets. When disabling "Developer mode", all custom DNR rules will be unregistered from the DNR API. The DNR rules content will be left intact in such case. Existing DNR rules will be registered into the DNR API when re-enabling "Developer mode". Administrators can prevent "Developer mode" from being enabled by adding `develop` token to `disabledFeatures` setting. Related discussion: https://github.com/uBlockOrigin/uBOL-home/discussions/323 The main motivation is to give list maintainers a tool to assist with resolving filter issues. Custom DNR rules can assist in crafting and validating filters meant to work with uBOL. A secondary motivation is to provide technical users the ability to further customize their content blocker. More conveniences will be added over time, this is a first version.
2025-05-29 13:06:02 +00:00
const state = Boolean(cachedRulesetData.developerMode) &&
cachedRulesetData.disabledFeatures?.includes('develop') !== true;
dom.body.dataset.develop = `${state}`;
dom.prop('#developerMode input[type="checkbox"]', 'checked', state);
}
}
/******************************************************************************/
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);
}
}
/******************************************************************************/
async function onFilteringModeChange(ev) {
const input = ev.target;
const newLevel = parseInt(input.value, 10);
switch ( newLevel ) {
case 1: {
const actualLevel = await sendMessage({
what: 'setDefaultFilteringMode',
level: newLevel,
});
cachedRulesetData.defaultFilteringMode = actualLevel;
break;
}
case 2:
case 3: {
const granted = await browser.permissions.request({
origins: [ '<all_urls>' ],
});
if ( granted ) {
const actualLevel = await sendMessage({
what: 'setDefaultFilteringMode',
level: newLevel,
});
cachedRulesetData.defaultFilteringMode = actualLevel;
cachedRulesetData.hasOmnipotence = true;
}
break;
}
default:
break;
}
renderFilterLists(cachedRulesetData);
renderWidgets();
}
dom.on(
'#defaultFilteringMode',
'change',
'.filteringModeCard input[type="radio"]',
ev => { onFilteringModeChange(ev); }
);
/******************************************************************************/
async function backupSettings() {
const api = await import('./backup-restore.js');
const data = await api.backupToObject(cachedRulesetData);
if ( data instanceof Object === false ) { return; }
const json = JSON.stringify(data, null, 2) + '\n';
const a = document.createElement('a');
a.href = `data:text/plain;charset=utf-8,${encodeURIComponent(json)}`;
dom.attr(a, 'download', 'my-ubol-settings.json');
dom.attr(a, 'type', 'application/json');
a.click();
}
async function restoreSettings() {
const promise = new Promise(resolve => {
const input = qs$('section[data-pane="settings"] input[type="file"]');
input.onchange = ev => {
dom.cl.add(dom.body, 'busy');
input.onchange = null;
const file = ev.target.files[0];
if ( file === undefined || file.name === '' ) { return resolve(); }
const fr = new FileReader();
fr.onload = ( ) => {
fr.onload = null;
if ( typeof fr.result !== 'string' ) { return resolve(); }
let data;
try {
data = JSON.parse(fr.result);
} catch {
}
if ( data instanceof Object === false ) { return resolve(); }
import('./backup-restore.js').then(api => {
resolve(api.restoreFromObject(data));
});
};
fr.readAsText(file);
};
input.oncancel = ( ) => {
resolve();
};
// Reset to empty string, this will ensure a change event is properly
// triggered if the user pick a file, even if it's the same as the last
// one picked.
input.value = '';
input.click();
});
await promise;
dom.cl.remove(dom.body, 'busy');
}
async function resetSettings() {
const response = self.confirm(i18n.getMessage('resetToDefaultConfirm'));
if ( response !== true ) { return; }
dom.cl.add(dom.body, 'busy');
const api = await import('./backup-restore.js');
await api.restoreFromObject({});
dom.cl.remove(dom.body, 'busy');
}
/******************************************************************************/
dom.on('#autoReload input[type="checkbox"]', 'change', ev => {
sendMessage({
what: 'setAutoReload',
state: ev.target.checked,
});
});
dom.on('#showBlockedCount input[type="checkbox"]', 'change', ev => {
sendMessage({
what: 'setShowBlockedCount',
state: ev.target.checked,
});
});
dom.on('#strictBlockMode input[type="checkbox"]', 'change', ev => {
sendMessage({
what: 'setStrictBlockMode',
state: ev.target.checked,
});
});
dom.on('#developerMode input[type="checkbox"]', 'change', ev => {
[mv3] Add support for custom DNR rules This feature is hidden behind the "Developer mode" setting in the dashboard. When "Developer mode" is enabled, a tab named "Develop" will become available in the dashboard. This tab is meant to contain tools for technical users. At the moment, the "Develop" pane allows to create custom DNR rules through a (CodeMirror-based) editor. For the sake of convenience, the DNR rule must be entered in YAML-like format. The format is not really full compliant YAML, just YAML-like, and very strict in order to ensure the parser stays simple enough. Lines starting with `#` are comments and will be ignored by the parser. Any line which do not match the parser's expectation will be marked as invalid, and the whole DNR rule containing such invalid lines will be discarded. There must not be empty lines inside a rule definition. Each DNR rule must be separated with a `---` line, which is known as a YAML document separator. String values must not be quoted, otherwise the quotes will be considered part of the value. There is one exception: `''` will be parsed as "an empty string". The editor will attempt to auto-complete known DNR keywords. That feature will improve over time. Though the parser will identify some errors, not all invalid DNR rules are currently identified by the parser, and these will be reported when the rules are registered through the DNR API. Better identifying invalid DNR rules at edit time will improve over time. The editor will report `regexFilter` values which are not supported by the DNR engine on the current platform. The editor reacts to instances of `regexFilter: ...` to report whether a regex value is supported. This means you can test for a regex value by using `# regexFilter: ...` so that you do not have to create an actual DNR rules just for the sake of testing. Custom DNR rules can be exported into a JSON file (a format known by the DNR API as a "static ruleset"). JSON-based ruleset can be imported, the content will be converted to YAML-like syntax. The editor will attempt to convert to YAML pasted content which can be JSON-parsed. It's possible to paste partially or wholly JSON-based rulesets. When disabling "Developer mode", all custom DNR rules will be unregistered from the DNR API. The DNR rules content will be left intact in such case. Existing DNR rules will be registered into the DNR API when re-enabling "Developer mode". Administrators can prevent "Developer mode" from being enabled by adding `develop` token to `disabledFeatures` setting. Related discussion: https://github.com/uBlockOrigin/uBOL-home/discussions/323 The main motivation is to give list maintainers a tool to assist with resolving filter issues. Custom DNR rules can assist in crafting and validating filters meant to work with uBOL. A secondary motivation is to provide technical users the ability to further customize their content blocker. More conveniences will be added over time, this is a first version.
2025-05-29 13:06:02 +00:00
const state = ev.target.checked;
sendMessage({ what: 'setDeveloperMode', state });
dom.body.dataset.develop = `${state}`;
});
dom.on('section[data-pane="settings"] [data-i18n="backupButton"]', 'click', ( ) => {
backupSettings();
});
dom.on('section[data-pane="settings"] [data-i18n="restoreButton"]', 'click', ( ) => {
restoreSettings();
});
dom.on('section[data-pane="settings"] [data-i18n="resetToDefaultButton"]', 'click', ( ) => {
resetSettings();
});
/******************************************************************************/
function listen() {
const bc = new self.BroadcastChannel('uBOL');
bc.onmessage = listen.onmessage;
}
listen.onmessage = ev => {
const message = ev.data;
if ( message instanceof Object === false ) { return; }
const local = cachedRulesetData;
let render = false;
if ( message.hasOmnipotence !== undefined ) {
if ( message.hasOmnipotence !== local.hasOmnipotence ) {
local.hasOmnipotence = message.hasOmnipotence;
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;
}
}
if ( message.showBlockedCount !== undefined ) {
if ( message.showBlockedCount !== local.showBlockedCount ) {
local.showBlockedCount = message.showBlockedCount;
render = true;
}
}
if ( message.strictBlockMode !== undefined ) {
if ( message.strictBlockMode !== local.strictBlockMode ) {
local.strictBlockMode = message.strictBlockMode;
render = true;
}
}
if ( message.developerMode !== undefined ) {
if ( message.developerMode !== local.developerMode ) {
local.developerMode = message.developerMode;
render = true;
}
}
if ( message.adminRulesets !== undefined ) {
if ( hashFromIterable(message.adminRulesets) !== hashFromIterable(local.adminRulesets) ) {
local.adminRulesets = message.adminRulesets;
render = true;
}
}
if ( message.enabledRulesets !== undefined ) {
local.enabledRulesets = message.enabledRulesets;
render = true;
}
if ( render === false ) { return; }
renderFilterLists(cachedRulesetData);
renderWidgets();
};
/******************************************************************************/
sendMessage({
what: 'getOptionsPageData',
}).then(data => {
if ( !data ) { return; }
cachedRulesetData = data;
try {
renderAdminRules();
renderFilterLists(cachedRulesetData);
renderWidgets();
} catch(reason) {
console.error(reason);
} finally {
dom.cl.remove(dom.body, 'loading');
}
listen();
}).catch(reason => {
console.error(reason);
});
/******************************************************************************/