uBlock/platform/mv3/extension/js/background.js
Raymond Hill 9339a75952
[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 09:06:02 -04:00

561 lines
16 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 {
MODE_BASIC,
MODE_OPTIMAL,
getDefaultFilteringMode,
getFilteringMode,
getTrustedSites,
setDefaultFilteringMode,
setFilteringMode,
setTrustedSites,
syncWithBrowserPermissions,
} from './mode-manager.js';
import {
adminReadEx,
getAdminRulesets,
loadAdminConfig,
} from './admin.js';
import {
broadcastMessage,
gotoURL,
hasBroadHostPermissions,
hostnamesFromMatches,
} from './utils.js';
import {
browser,
localRead, localRemove, localWrite,
runtime,
} from './ext.js';
import {
enableRulesets,
excludeFromStrictBlock,
getEnabledRulesetsDetails,
getRulesetDetails,
patchDefaultRulesets,
setStrictBlockMode,
updateDynamicRules,
updateSessionRules,
updateUserRules,
} from './ruleset-manager.js';
import {
getMatchedRules,
isSideloaded,
toggleDeveloperMode,
ubolLog,
} from './debug.js';
import {
loadRulesetConfig,
process,
rulesetConfig,
saveRulesetConfig,
} from './config.js';
import { dnr } from './ext-compat.js';
import { registerInjectables } from './scripting-manager.js';
import { toggleToolbarIcon } from './action.js';
/******************************************************************************/
const UBOL_ORIGIN = runtime.getURL('').replace(/\/$/, '').toLowerCase();
const canShowBlockedCount = typeof dnr.setExtensionActionOptions === 'function';
let pendingPermissionRequest;
/******************************************************************************/
function getCurrentVersion() {
return runtime.getManifest().version;
}
/******************************************************************************/
async function onPermissionsRemoved() {
const modified = await syncWithBrowserPermissions();
if ( modified === false ) { return false; }
registerInjectables();
return true;
}
// https://github.com/uBlockOrigin/uBOL-home/issues/280
async function onPermissionsAdded(permissions) {
const details = pendingPermissionRequest;
pendingPermissionRequest = undefined;
if ( details === undefined ) {
const modified = await syncWithBrowserPermissions();
if ( modified === false ) { return; }
return Promise.all([
updateSessionRules(),
registerInjectables(),
]);
}
const defaultMode = await getDefaultFilteringMode();
if ( defaultMode >= MODE_OPTIMAL ) { return; }
if ( Array.isArray(permissions.origins) === false ) { return; }
const hostnames = hostnamesFromMatches(permissions.origins);
if ( hostnames.includes(details.hostname) === false ) { return; }
const beforeLevel = await getFilteringMode(details.hostname);
if ( beforeLevel === details.afterLevel ) { return; }
const afterLevel = await setFilteringMode(details.hostname, details.afterLevel);
if ( afterLevel !== details.afterLevel ) { return; }
await registerInjectables();
if ( rulesetConfig.autoReload ) {
self.setTimeout(( ) => {
browser.tabs.update(details.tabId, {
url: details.url,
});
}, 437);
}
}
/******************************************************************************/
function setDeveloperMode(state) {
rulesetConfig.developerMode = state === true;
toggleDeveloperMode(rulesetConfig.developerMode);
return Promise.all([
updateUserRules(),
saveRulesetConfig(),
]);
}
/******************************************************************************/
function onMessage(request, sender, callback) {
// Does not require trusted origin.
switch ( request.what ) {
case 'insertCSS': {
const tabId = sender?.tab?.id ?? false;
const frameId = sender?.frameId ?? false;
if ( tabId === false || frameId === false ) { return; }
browser.scripting.insertCSS({
css: request.css,
origin: 'USER',
target: { tabId, frameIds: [ frameId ] },
}).catch(reason => {
console.log(reason);
});
return false;
}
case 'removeCSS': {
const tabId = sender?.tab?.id ?? false;
const frameId = sender?.frameId ?? false;
if ( tabId === false || frameId === false ) { return; }
browser.scripting.removeCSS({
css: request.css,
origin: 'USER',
target: { tabId, frameIds: [ frameId ] },
}).catch(reason => {
console.log(reason);
});
return false;
}
case 'toggleToolbarIcon': {
const tabId = sender?.tab?.id ?? false;
if ( tabId ) {
toggleToolbarIcon(tabId);
}
return false;
}
default:
break;
}
// Does require trusted origin.
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/MessageSender
// Firefox API does not set `sender.origin`
if ( sender.origin !== undefined ) {
if ( sender.origin.toLowerCase() !== UBOL_ORIGIN ) { return; }
}
switch ( request.what ) {
case 'applyRulesets': {
enableRulesets(request.enabledRulesets).then(( ) => {
rulesetConfig.enabledRulesets = request.enabledRulesets;
return saveRulesetConfig();
}).then(( ) => {
registerInjectables();
callback();
return dnr.getEnabledRulesets();
}).then(enabledRulesets => {
broadcastMessage({ enabledRulesets });
});
return true;
}
case 'getOptionsPageData': {
Promise.all([
hasBroadHostPermissions(),
getDefaultFilteringMode(),
getTrustedSites(),
getRulesetDetails(),
dnr.getEnabledRulesets(),
getAdminRulesets(),
adminReadEx('disabledFeatures'),
]).then(results => {
const [
hasOmnipotence,
defaultFilteringMode,
trustedSites,
rulesetDetails,
enabledRulesets,
adminRulesets,
disabledFeatures,
] = results;
callback({
hasOmnipotence,
defaultFilteringMode,
trustedSites: Array.from(trustedSites),
enabledRulesets,
adminRulesets,
maxNumberOfEnabledRulesets: dnr.MAX_NUMBER_OF_ENABLED_STATIC_RULESETS,
rulesetDetails: Array.from(rulesetDetails.values()),
autoReload: rulesetConfig.autoReload,
showBlockedCount: rulesetConfig.showBlockedCount,
canShowBlockedCount,
strictBlockMode: rulesetConfig.strictBlockMode,
firstRun: process.firstRun,
isSideloaded,
developerMode: rulesetConfig.developerMode,
disabledFeatures,
});
process.firstRun = false;
});
return true;
}
case 'setAutoReload':
rulesetConfig.autoReload = request.state && true || false;
saveRulesetConfig().then(( ) => {
callback();
broadcastMessage({ autoReload: rulesetConfig.autoReload });
});
return true;
case 'setShowBlockedCount':
rulesetConfig.showBlockedCount = request.state && true || false;
if ( canShowBlockedCount ) {
dnr.setExtensionActionOptions({
displayActionCountAsBadgeText: rulesetConfig.showBlockedCount,
});
}
saveRulesetConfig().then(( ) => {
callback();
broadcastMessage({ showBlockedCount: rulesetConfig.showBlockedCount });
});
return true;
case 'setStrictBlockMode':
setStrictBlockMode(request.state).then(( ) => {
callback();
broadcastMessage({ strictBlockMode: rulesetConfig.strictBlockMode });
});
return true;
case 'setDeveloperMode':
setDeveloperMode(request.state).then(( ) => {
callback();
});
return true;
case 'popupPanelData': {
Promise.all([
hasBroadHostPermissions(),
getFilteringMode(request.hostname),
getEnabledRulesetsDetails(),
adminReadEx('disabledFeatures'),
]).then(results => {
const [
hasOmnipotence,
level,
rulesetDetails,
disabledFeatures,
] = results;
callback({
hasOmnipotence,
level,
autoReload: rulesetConfig.autoReload,
rulesetDetails,
isSideloaded,
developerMode: rulesetConfig.developerMode,
disabledFeatures,
});
});
return true;
}
case 'getFilteringMode': {
getFilteringMode(request.hostname).then(actualLevel => {
callback(actualLevel);
});
return true;
}
case 'gotoURL':
gotoURL(request.url, request.type);
break;
case 'setFilteringMode': {
getFilteringMode(request.hostname).then(beforeLevel => {
if ( request.level === beforeLevel ) { return beforeLevel; }
return setFilteringMode(request.hostname, request.level);
}).then(afterLevel => {
registerInjectables();
callback(afterLevel);
});
return true;
}
case 'setPendingFilteringMode':
pendingPermissionRequest = request;
break;
case 'getDefaultFilteringMode': {
getDefaultFilteringMode().then(level => {
callback(level);
});
return true;
}
case 'setDefaultFilteringMode': {
getDefaultFilteringMode().then(beforeLevel =>
setDefaultFilteringMode(request.level).then(afterLevel =>
({ beforeLevel, afterLevel })
)
).then(({ beforeLevel, afterLevel }) => {
if ( afterLevel !== beforeLevel ) {
registerInjectables();
}
callback(afterLevel);
});
return true;
}
case 'setTrustedSites':
setTrustedSites(request.hostnames).then(( ) => {
registerInjectables();
return Promise.all([
getDefaultFilteringMode(),
getTrustedSites(),
]);
}).then(results => {
callback({
defaultFilteringMode: results[0],
trustedSites: Array.from(results[1]),
});
});
return true;
case 'excludeFromStrictBlock': {
excludeFromStrictBlock(request.hostname, request.permanent).then(( ) => {
callback();
});
return true;
}
case 'getMatchedRules':
getMatchedRules(request.tabId).then(entries => {
callback(entries);
});
return true;
case 'showMatchedRules':
browser.windows.create({
type: 'popup',
url: `/matched-rules.html?tab=${request.tabId}`,
});
break;
case 'updateUserDnrRules':
updateUserRules().then(result => {
callback(result);
});
return true;
default:
break;
}
return false;
}
/******************************************************************************/
function onCommand(command, tab) {
switch ( command ) {
case 'enter-zapper-mode': {
if ( browser.scripting === undefined ) { return; }
browser.scripting.executeScript({
files: [ '/js/scripting/zapper.js' ],
target: { tabId: tab.id },
});
break;
}
default:
break;
}
}
/******************************************************************************/
async function startSession() {
const currentVersion = getCurrentVersion();
const isNewVersion = currentVersion !== rulesetConfig.version;
// Admin settings override user settings
await loadAdminConfig();
// The default rulesets may have changed, find out new ruleset to enable,
// obsolete ruleset to remove.
if ( isNewVersion ) {
ubolLog(`Version change: ${rulesetConfig.version} => ${currentVersion}`);
rulesetConfig.version = currentVersion;
await patchDefaultRulesets();
saveRulesetConfig();
}
const rulesetsUpdated = await enableRulesets(rulesetConfig.enabledRulesets);
// We need to update the regex rules only when ruleset version changes.
if ( rulesetsUpdated === false ) {
if ( isNewVersion ) {
updateDynamicRules();
} else {
updateSessionRules();
}
}
// Permissions may have been removed while the extension was disabled
await syncWithBrowserPermissions();
// Unsure whether the browser remembers correctly registered css/scripts
// after we quit the browser. For now uBOL will check unconditionally at
// launch time whether content css/scripts are properly registered.
registerInjectables();
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/declarativeNetRequest
// Firefox API does not support `dnr.setExtensionActionOptions`
if ( canShowBlockedCount ) {
dnr.setExtensionActionOptions({
displayActionCountAsBadgeText: rulesetConfig.showBlockedCount,
});
}
// Switch to basic filtering if uBOL doesn't have broad permissions at
// install time.
if ( process.firstRun ) {
const enableOptimal = await hasBroadHostPermissions();
if ( enableOptimal === false ) {
const afterLevel = await setDefaultFilteringMode(MODE_BASIC);
if ( afterLevel === MODE_BASIC ) {
registerInjectables();
process.firstRun = false;
}
}
}
// Required to ensure up to date properties are available when needed
adminReadEx('disabledFeatures').then(items => {
if ( Array.isArray(items) === false ) { return; }
if ( items.includes('develop') ) {
if ( rulesetConfig.developerMode ) {
setDeveloperMode(false);
}
}
});
}
/******************************************************************************/
async function start() {
await loadRulesetConfig();
if ( process.wakeupRun === false ) {
await startSession();
}
toggleDeveloperMode(rulesetConfig.developerMode);
}
/******************************************************************************/
// https://github.com/uBlockOrigin/uBOL-home/issues/199
// Force a restart of the extension once when an "internal error" occurs
const isFullyInitialized = start().then(( ) => {
localRemove('goodStart');
return false;
}).catch(reason => {
console.trace(reason);
if ( process.wakeupRun ) { return; }
return localRead('goodStart').then(goodStart => {
if ( goodStart === false ) {
localRemove('goodStart');
return false;
}
return localWrite('goodStart', false).then(( ) => true);
});
}).then(restart => {
if ( restart !== true ) { return; }
runtime.reload();
});
runtime.onMessage.addListener((request, sender, callback) => {
isFullyInitialized.then(( ) => {
const r = onMessage(request, sender, callback);
if ( r !== true ) { callback(); }
});
return true;
});
browser.permissions.onRemoved.addListener((...args) => {
isFullyInitialized.then(( ) => {
onPermissionsRemoved(...args);
});
});
browser.permissions.onAdded.addListener((...args) => {
isFullyInitialized.then(( ) => {
onPermissionsAdded(...args);
});
});
browser.commands.onCommand.addListener((...args) => {
isFullyInitialized.then(( ) => {
onCommand(...args);
});
});