2022-09-13 21:44:24 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
2023-12-04 17:10:34 +00:00
|
|
|
uBlock Origin Lite - a comprehensive, MV3-compliant content blocker
|
2022-09-13 21:44:24 +00:00
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
|
2025-12-09 20:32:02 +00:00
|
|
|
import * as scrmgr from './scripting-manager.js';
|
|
|
|
|
|
2024-11-11 18:20:54 +00:00
|
|
|
import {
|
|
|
|
|
MODE_BASIC,
|
|
|
|
|
MODE_OPTIMAL,
|
2025-09-27 16:53:58 +00:00
|
|
|
defaultFilteringModes,
|
2024-11-11 18:20:54 +00:00
|
|
|
getDefaultFilteringMode,
|
|
|
|
|
getFilteringMode,
|
2025-06-13 16:46:05 +00:00
|
|
|
getFilteringModeDetails,
|
2025-11-29 14:41:46 +00:00
|
|
|
persistHostPermissions,
|
2024-11-11 18:20:54 +00:00
|
|
|
setDefaultFilteringMode,
|
|
|
|
|
setFilteringMode,
|
2025-06-13 16:46:05 +00:00
|
|
|
setFilteringModeDetails,
|
2024-11-11 18:20:54 +00:00
|
|
|
syncWithBrowserPermissions,
|
|
|
|
|
} from './mode-manager.js';
|
|
|
|
|
|
2025-07-06 20:43:22 +00:00
|
|
|
import {
|
2025-09-24 14:42:18 +00:00
|
|
|
addCustomFilters,
|
|
|
|
|
customFiltersFromHostname,
|
|
|
|
|
getAllCustomFilters,
|
2025-07-06 20:43:22 +00:00
|
|
|
hasCustomFilters,
|
|
|
|
|
injectCustomFilters,
|
2025-09-24 14:42:18 +00:00
|
|
|
removeAllCustomFilters,
|
|
|
|
|
removeCustomFilters,
|
2025-08-03 19:50:22 +00:00
|
|
|
startCustomFilters,
|
|
|
|
|
terminateCustomFilters,
|
2025-07-06 20:43:22 +00:00
|
|
|
} from './filter-manager.js';
|
|
|
|
|
|
2024-11-23 18:17:13 +00:00
|
|
|
import {
|
|
|
|
|
adminReadEx,
|
|
|
|
|
getAdminRulesets,
|
2025-04-25 15:25:50 +00:00
|
|
|
loadAdminConfig,
|
2024-11-23 18:17:13 +00:00
|
|
|
} from './admin.js';
|
|
|
|
|
|
2025-02-21 13:23:27 +00:00
|
|
|
import {
|
|
|
|
|
broadcastMessage,
|
2025-11-12 17:16:56 +00:00
|
|
|
hostnameFromMatch,
|
2025-02-21 13:23:27 +00:00
|
|
|
hostnamesFromMatches,
|
|
|
|
|
} from './utils.js';
|
|
|
|
|
|
2025-04-09 19:11:17 +00:00
|
|
|
import {
|
|
|
|
|
browser,
|
|
|
|
|
localRead, localRemove, localWrite,
|
|
|
|
|
runtime,
|
2025-12-09 20:32:02 +00:00
|
|
|
sessionAccessLevel,
|
2025-08-10 15:40:08 +00:00
|
|
|
webextFlavor,
|
2025-04-09 19:11:17 +00:00
|
|
|
} from './ext.js';
|
|
|
|
|
|
2025-09-27 16:53:58 +00:00
|
|
|
import {
|
|
|
|
|
defaultConfig,
|
|
|
|
|
loadRulesetConfig,
|
|
|
|
|
process,
|
|
|
|
|
rulesetConfig,
|
|
|
|
|
saveRulesetConfig,
|
|
|
|
|
} from './config.js';
|
|
|
|
|
|
2024-03-21 17:57:50 +00:00
|
|
|
import {
|
2022-09-20 12:24:01 +00:00
|
|
|
enableRulesets,
|
2024-12-03 21:41:34 +00:00
|
|
|
excludeFromStrictBlock,
|
2025-09-27 16:53:58 +00:00
|
|
|
getDefaultRulesetsFromEnv,
|
2025-06-13 16:46:05 +00:00
|
|
|
getEffectiveDynamicRules,
|
|
|
|
|
getEffectiveSessionRules,
|
2025-06-03 10:05:56 +00:00
|
|
|
getEffectiveUserRules,
|
2024-03-21 17:57:50 +00:00
|
|
|
getRulesetDetails,
|
2024-12-05 17:56:25 +00:00
|
|
|
patchDefaultRulesets,
|
2024-12-03 21:41:34 +00:00
|
|
|
setStrictBlockMode,
|
2022-09-29 23:51:33 +00:00
|
|
|
updateDynamicRules,
|
2024-12-29 21:38:56 +00:00
|
|
|
updateSessionRules,
|
[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
|
|
|
updateUserRules,
|
2022-09-20 12:24:01 +00:00
|
|
|
} from './ruleset-manager.js';
|
|
|
|
|
|
2023-06-04 15:32:55 +00:00
|
|
|
import {
|
2025-09-29 17:07:59 +00:00
|
|
|
getConsoleOutput,
|
2024-07-29 18:54:46 +00:00
|
|
|
getMatchedRules,
|
|
|
|
|
isSideloaded,
|
2024-11-21 17:54:28 +00:00
|
|
|
toggleDeveloperMode,
|
2025-08-11 21:53:59 +00:00
|
|
|
ubolErr,
|
2024-07-29 18:54:46 +00:00
|
|
|
ubolLog,
|
|
|
|
|
} from './debug.js';
|
|
|
|
|
|
2026-01-17 18:19:51 +00:00
|
|
|
import {
|
|
|
|
|
gotoURL,
|
|
|
|
|
hasBroadHostPermissions,
|
|
|
|
|
} from './ext-utils.js';
|
|
|
|
|
|
2025-04-19 17:08:59 +00:00
|
|
|
import { dnr } from './ext-compat.js';
|
2025-05-07 12:53:59 +00:00
|
|
|
import { toggleToolbarIcon } from './action.js';
|
2023-06-04 15:32:55 +00:00
|
|
|
|
2022-09-07 14:15:36 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2025-04-19 17:08:59 +00:00
|
|
|
const UBOL_ORIGIN = runtime.getURL('').replace(/\/$/, '').toLowerCase();
|
2024-03-21 17:57:50 +00:00
|
|
|
const canShowBlockedCount = typeof dnr.setExtensionActionOptions === 'function';
|
2025-12-09 20:32:02 +00:00
|
|
|
const { registerInjectables } = scrmgr;
|
2024-03-21 17:57:50 +00:00
|
|
|
|
2025-02-21 13:23:27 +00:00
|
|
|
let pendingPermissionRequest;
|
|
|
|
|
|
2022-09-13 21:44:24 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
function getCurrentVersion() {
|
|
|
|
|
return runtime.getManifest().version;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 14:15:36 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2025-11-12 17:16:56 +00:00
|
|
|
async function reloadTab(tabId, url = '') {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
self.setTimeout(( ) => {
|
|
|
|
|
if ( url !== '' ) {
|
|
|
|
|
browser.tabs.update(tabId, { url });
|
|
|
|
|
} else {
|
|
|
|
|
browser.tabs.reload(tabId);
|
|
|
|
|
}
|
|
|
|
|
resolve();
|
|
|
|
|
}, 437);
|
|
|
|
|
});
|
2022-09-27 11:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-12 17:16:56 +00:00
|
|
|
// When a new host permission is granted through the popup panel
|
|
|
|
|
async function onPermissionGrantedThruExtension(details, origins) {
|
2025-11-29 14:41:46 +00:00
|
|
|
await persistHostPermissions();
|
2025-02-21 13:23:27 +00:00
|
|
|
const defaultMode = await getDefaultFilteringMode();
|
|
|
|
|
if ( defaultMode >= MODE_OPTIMAL ) { return; }
|
2025-11-12 17:16:56 +00:00
|
|
|
if ( Array.isArray(origins) === false ) { return; }
|
|
|
|
|
const hostnames = hostnamesFromMatches(origins);
|
2025-02-21 13:23:27 +00:00
|
|
|
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();
|
2025-11-12 17:16:56 +00:00
|
|
|
if ( rulesetConfig.autoReload !== true ) { return; }
|
|
|
|
|
await reloadTab(details.tabId, details.url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// When a new host permission is granted through the browser
|
|
|
|
|
async function onPermissionGrantedThruBrowser(origins) {
|
|
|
|
|
const modified = await syncWithBrowserPermissions();
|
|
|
|
|
if ( modified === false ) { return; }
|
2025-11-14 15:01:09 +00:00
|
|
|
await registerInjectables();
|
|
|
|
|
if ( rulesetConfig.autoReload !== true ) { return; }
|
2025-11-12 17:16:56 +00:00
|
|
|
if ( origins.length !== 1 ) { return; }
|
|
|
|
|
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
|
|
|
|
const tabId = tabs?.[0]?.id;
|
|
|
|
|
if ( typeof tabId !== 'number' || tabId === -1 ) { return; }
|
|
|
|
|
const results = await browser.scripting.executeScript({
|
|
|
|
|
target: { tabId, frameIds: [ 0 ] },
|
|
|
|
|
func: ( ) => document.location.hostname,
|
2025-11-29 14:41:46 +00:00
|
|
|
}).catch(( ) => {
|
2025-11-12 17:16:56 +00:00
|
|
|
});
|
|
|
|
|
const tabHostname = results?.[0]?.result;
|
|
|
|
|
if ( typeof tabHostname !== 'string' ) { return; }
|
|
|
|
|
const hostname = hostnameFromMatch(origins[0]);
|
|
|
|
|
if ( tabHostname.endsWith(hostname) === false ) { return; }
|
|
|
|
|
const pos = tabHostname.length - hostname.length;
|
|
|
|
|
if ( pos !== 0 && tabHostname.charAt(pos-1) !== '.' ) { return; }
|
|
|
|
|
await reloadTab(tabId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBOL-home/issues/280
|
|
|
|
|
async function onPermissionsAdded(permissions) {
|
|
|
|
|
const details = pendingPermissionRequest;
|
|
|
|
|
pendingPermissionRequest = undefined;
|
|
|
|
|
const { origins = [] } = permissions;
|
2025-11-14 15:01:09 +00:00
|
|
|
return details !== undefined
|
|
|
|
|
? onPermissionGrantedThruExtension(details, origins)
|
|
|
|
|
: onPermissionGrantedThruBrowser(origins);
|
2025-11-12 17:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function onPermissionsRemoved() {
|
|
|
|
|
const modified = await syncWithBrowserPermissions();
|
|
|
|
|
if ( modified === false ) { return false; }
|
|
|
|
|
registerInjectables();
|
|
|
|
|
return true;
|
2025-02-21 13:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-18 14:03:10 +00:00
|
|
|
async function onPermissionsChanged(op, permissions) {
|
|
|
|
|
await isFullyInitialized;
|
|
|
|
|
const { pending } = onPermissionsChanged;
|
|
|
|
|
await Promise.all(pending);
|
|
|
|
|
const promise = op === 'removed'
|
|
|
|
|
? onPermissionsRemoved()
|
|
|
|
|
: onPermissionsAdded(permissions);
|
|
|
|
|
pending.push(promise);
|
|
|
|
|
}
|
|
|
|
|
onPermissionsChanged.pending = [];
|
|
|
|
|
|
2022-09-13 21:44:24 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
[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
|
|
|
function setDeveloperMode(state) {
|
|
|
|
|
rulesetConfig.developerMode = state === true;
|
|
|
|
|
toggleDeveloperMode(rulesetConfig.developerMode);
|
2025-06-06 12:45:19 +00:00
|
|
|
broadcastMessage({ developerMode: rulesetConfig.developerMode });
|
[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
|
|
|
return Promise.all([
|
|
|
|
|
updateUserRules(),
|
|
|
|
|
saveRulesetConfig(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2022-09-15 17:14:08 +00:00
|
|
|
function onMessage(request, sender, callback) {
|
2022-10-15 17:05:20 +00:00
|
|
|
|
2025-07-06 20:43:22 +00:00
|
|
|
const tabId = sender?.tab?.id ?? false;
|
|
|
|
|
const frameId = tabId && (sender?.frameId ?? false);
|
|
|
|
|
|
2023-06-04 15:32:55 +00:00
|
|
|
// Does not require trusted origin.
|
|
|
|
|
|
|
|
|
|
switch ( request.what ) {
|
|
|
|
|
|
2025-12-09 20:32:02 +00:00
|
|
|
case 'insertCSS':
|
2025-07-06 20:43:22 +00:00
|
|
|
if ( frameId === false ) { return false; }
|
2025-08-16 16:51:17 +00:00
|
|
|
// https://bugs.webkit.org/show_bug.cgi?id=262491
|
|
|
|
|
if ( frameId !== 0 && webextFlavor === 'safari' ) { return false; }
|
2023-06-04 15:32:55 +00:00
|
|
|
browser.scripting.insertCSS({
|
|
|
|
|
css: request.css,
|
|
|
|
|
origin: 'USER',
|
|
|
|
|
target: { tabId, frameIds: [ frameId ] },
|
2023-06-05 13:15:59 +00:00
|
|
|
}).catch(reason => {
|
2025-08-15 12:39:35 +00:00
|
|
|
ubolErr(`insertCSS/${reason}`);
|
2023-06-04 15:32:55 +00:00
|
|
|
});
|
2023-11-29 02:13:44 +00:00
|
|
|
return false;
|
2023-06-04 15:32:55 +00:00
|
|
|
|
2025-12-09 20:32:02 +00:00
|
|
|
case 'removeCSS':
|
2025-07-06 20:43:22 +00:00
|
|
|
if ( frameId === false ) { return false; }
|
2025-12-09 20:32:02 +00:00
|
|
|
// https://bugs.webkit.org/show_bug.cgi?id=262491
|
|
|
|
|
if ( frameId !== 0 && webextFlavor === 'safari' ) { return false; }
|
2025-03-21 17:23:54 +00:00
|
|
|
browser.scripting.removeCSS({
|
|
|
|
|
css: request.css,
|
|
|
|
|
origin: 'USER',
|
|
|
|
|
target: { tabId, frameIds: [ frameId ] },
|
|
|
|
|
}).catch(reason => {
|
2025-08-15 12:39:35 +00:00
|
|
|
ubolErr(`removeCSS/${reason}`);
|
2025-03-21 17:23:54 +00:00
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
|
2025-05-07 12:53:59 +00:00
|
|
|
case 'toggleToolbarIcon': {
|
|
|
|
|
if ( tabId ) {
|
|
|
|
|
toggleToolbarIcon(tabId);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 19:50:22 +00:00
|
|
|
case 'startCustomFilters':
|
2025-07-06 20:43:22 +00:00
|
|
|
if ( frameId === false ) { return false; }
|
2025-08-03 19:50:22 +00:00
|
|
|
startCustomFilters(tabId, frameId).then(( ) => {
|
|
|
|
|
callback();
|
2025-07-06 20:43:22 +00:00
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-08-03 19:50:22 +00:00
|
|
|
case 'terminateCustomFilters':
|
2025-07-06 20:43:22 +00:00
|
|
|
if ( frameId === false ) { return false; }
|
2025-08-03 19:50:22 +00:00
|
|
|
terminateCustomFilters(tabId, frameId).then(( ) => {
|
2025-07-06 20:43:22 +00:00
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-08-03 19:50:22 +00:00
|
|
|
case 'injectCustomFilters':
|
|
|
|
|
if ( frameId === false ) { return false; }
|
|
|
|
|
injectCustomFilters(tabId, frameId, request.hostname).then(selectors => {
|
|
|
|
|
callback(selectors);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-07-13 21:16:04 +00:00
|
|
|
case 'injectCSSProceduralAPI':
|
|
|
|
|
browser.scripting.executeScript({
|
|
|
|
|
files: [ '/js/scripting/css-procedural-api.js' ],
|
|
|
|
|
target: { tabId, frameIds: [ frameId ] },
|
2025-08-03 19:50:22 +00:00
|
|
|
injectImmediately: true,
|
2025-07-13 21:16:04 +00:00
|
|
|
}).catch(reason => {
|
2025-08-15 12:39:35 +00:00
|
|
|
ubolErr(`executeScript/${reason}`);
|
2025-07-13 21:16:04 +00:00
|
|
|
}).then(( ) => {
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2023-06-04 15:32:55 +00:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-13 17:28:02 +00:00
|
|
|
// Does require trusted origin.
|
2023-06-04 15:32:55 +00:00
|
|
|
|
2023-04-07 14:19:43 +00:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/MessageSender
|
|
|
|
|
// Firefox API does not set `sender.origin`
|
2025-04-19 17:08:59 +00:00
|
|
|
if ( sender.origin !== undefined ) {
|
|
|
|
|
if ( sender.origin.toLowerCase() !== UBOL_ORIGIN ) { return; }
|
|
|
|
|
}
|
2022-10-15 17:05:20 +00:00
|
|
|
|
2022-09-13 21:44:24 +00:00
|
|
|
switch ( request.what ) {
|
|
|
|
|
|
2022-09-15 17:14:08 +00:00
|
|
|
case 'applyRulesets': {
|
2025-08-08 14:30:32 +00:00
|
|
|
enableRulesets(request.enabledRulesets).then(result => {
|
|
|
|
|
if ( result === undefined || result.error ) {
|
|
|
|
|
callback(result);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
rulesetConfig.enabledRulesets = result.enabledRulesets;
|
|
|
|
|
return saveRulesetConfig().then(( ) => {
|
|
|
|
|
return registerInjectables();
|
|
|
|
|
}).then(( ) => {
|
|
|
|
|
callback(result);
|
|
|
|
|
});
|
2025-08-11 21:53:59 +00:00
|
|
|
}).finally(( ) => {
|
|
|
|
|
broadcastMessage({ enabledRulesets: rulesetConfig.enabledRulesets });
|
2022-09-15 17:14:08 +00:00
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-27 16:53:58 +00:00
|
|
|
case 'getDefaultConfig':
|
|
|
|
|
getDefaultRulesetsFromEnv().then(rulesets => {
|
|
|
|
|
callback({
|
|
|
|
|
autoReload: defaultConfig.autoReload,
|
|
|
|
|
developerMode: defaultConfig.developerMode,
|
|
|
|
|
showBlockedCount: defaultConfig.showBlockedCount,
|
|
|
|
|
strictBlockMode: defaultConfig.strictBlockMode,
|
|
|
|
|
rulesets,
|
|
|
|
|
filteringModes: Object.assign(defaultFilteringModes),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-06-13 16:46:05 +00:00
|
|
|
case 'getOptionsPageData':
|
2022-09-16 19:56:35 +00:00
|
|
|
Promise.all([
|
2025-04-09 19:11:17 +00:00
|
|
|
hasBroadHostPermissions(),
|
2022-10-10 16:28:24 +00:00
|
|
|
getDefaultFilteringMode(),
|
2022-09-16 19:56:35 +00:00
|
|
|
getRulesetDetails(),
|
|
|
|
|
dnr.getEnabledRulesets(),
|
2024-11-11 18:20:54 +00:00
|
|
|
getAdminRulesets(),
|
2024-11-23 18:17:13 +00:00
|
|
|
adminReadEx('disabledFeatures'),
|
2022-09-16 19:56:35 +00:00
|
|
|
]).then(results => {
|
2022-10-10 16:28:24 +00:00
|
|
|
const [
|
2025-04-09 19:11:17 +00:00
|
|
|
hasOmnipotence,
|
2022-10-10 16:28:24 +00:00
|
|
|
defaultFilteringMode,
|
|
|
|
|
rulesetDetails,
|
|
|
|
|
enabledRulesets,
|
2024-11-11 18:20:54 +00:00
|
|
|
adminRulesets,
|
2024-11-23 18:17:13 +00:00
|
|
|
disabledFeatures,
|
2022-10-10 16:28:24 +00:00
|
|
|
] = results;
|
2022-09-13 21:44:24 +00:00
|
|
|
callback({
|
2025-04-09 19:11:17 +00:00
|
|
|
hasOmnipotence,
|
2022-10-10 16:28:24 +00:00
|
|
|
defaultFilteringMode,
|
2022-09-13 21:44:24 +00:00
|
|
|
enabledRulesets,
|
2024-11-11 18:20:54 +00:00
|
|
|
adminRulesets,
|
2023-07-09 13:25:38 +00:00
|
|
|
maxNumberOfEnabledRulesets: dnr.MAX_NUMBER_OF_ENABLED_STATIC_RULESETS,
|
2022-09-13 21:44:24 +00:00
|
|
|
rulesetDetails: Array.from(rulesetDetails.values()),
|
2023-11-29 21:11:22 +00:00
|
|
|
autoReload: rulesetConfig.autoReload,
|
2024-03-21 17:57:50 +00:00
|
|
|
showBlockedCount: rulesetConfig.showBlockedCount,
|
|
|
|
|
canShowBlockedCount,
|
2024-12-03 21:41:34 +00:00
|
|
|
strictBlockMode: rulesetConfig.strictBlockMode,
|
2024-11-11 18:20:54 +00:00
|
|
|
firstRun: process.firstRun,
|
2024-11-21 17:54:28 +00:00
|
|
|
isSideloaded,
|
|
|
|
|
developerMode: rulesetConfig.developerMode,
|
2024-11-23 18:17:13 +00:00
|
|
|
disabledFeatures,
|
2022-09-07 14:15:36 +00:00
|
|
|
});
|
2024-11-11 18:20:54 +00:00
|
|
|
process.firstRun = false;
|
2022-09-13 21:44:24 +00:00
|
|
|
});
|
|
|
|
|
return true;
|
2025-06-13 16:46:05 +00:00
|
|
|
|
2025-09-29 17:07:59 +00:00
|
|
|
case 'getEnabledRulesets':
|
|
|
|
|
dnr.getEnabledRulesets().then(rulesets => {
|
|
|
|
|
callback(rulesets);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-06-13 16:46:05 +00:00
|
|
|
case 'getRulesetDetails':
|
|
|
|
|
getRulesetDetails().then(rulesetDetails => {
|
|
|
|
|
callback(Array.from(rulesetDetails.values()));
|
|
|
|
|
});
|
|
|
|
|
return true;
|
2022-09-13 21:44:24 +00:00
|
|
|
|
2025-10-08 17:50:26 +00:00
|
|
|
case 'hasBroadHostPermissions':
|
|
|
|
|
hasBroadHostPermissions().then(result => {
|
|
|
|
|
callback(result);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2022-10-10 16:28:24 +00:00
|
|
|
case 'setAutoReload':
|
2023-11-29 21:11:22 +00:00
|
|
|
rulesetConfig.autoReload = request.state && true || false;
|
2022-10-10 16:28:24 +00:00
|
|
|
saveRulesetConfig().then(( ) => {
|
|
|
|
|
callback();
|
2023-11-29 21:11:22 +00:00
|
|
|
broadcastMessage({ autoReload: rulesetConfig.autoReload });
|
2022-10-10 16:28:24 +00:00
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2026-01-21 20:40:01 +00:00
|
|
|
case 'getShowBlockedCount':
|
|
|
|
|
callback(rulesetConfig.showBlockedCount);
|
|
|
|
|
break;
|
|
|
|
|
|
2024-03-21 17:57:50 +00:00
|
|
|
case 'setShowBlockedCount':
|
|
|
|
|
rulesetConfig.showBlockedCount = request.state && true || false;
|
|
|
|
|
if ( canShowBlockedCount ) {
|
|
|
|
|
dnr.setExtensionActionOptions({
|
|
|
|
|
displayActionCountAsBadgeText: rulesetConfig.showBlockedCount,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
saveRulesetConfig().then(( ) => {
|
|
|
|
|
callback();
|
|
|
|
|
broadcastMessage({ showBlockedCount: rulesetConfig.showBlockedCount });
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2024-12-03 21:41:34 +00:00
|
|
|
case 'setStrictBlockMode':
|
|
|
|
|
setStrictBlockMode(request.state).then(( ) => {
|
|
|
|
|
callback();
|
|
|
|
|
broadcastMessage({ strictBlockMode: rulesetConfig.strictBlockMode });
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2024-11-21 17:54:28 +00:00
|
|
|
case 'setDeveloperMode':
|
[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
|
|
|
setDeveloperMode(request.state).then(( ) => {
|
2024-11-21 17:54:28 +00:00
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2022-09-13 21:44:24 +00:00
|
|
|
case 'popupPanelData': {
|
|
|
|
|
Promise.all([
|
2025-04-09 19:11:17 +00:00
|
|
|
hasBroadHostPermissions(),
|
2025-07-18 17:53:01 +00:00
|
|
|
getFilteringMode(request.hostname),
|
2024-11-23 18:17:13 +00:00
|
|
|
adminReadEx('disabledFeatures'),
|
2025-07-06 20:43:22 +00:00
|
|
|
hasCustomFilters(request.hostname),
|
2022-09-13 21:44:24 +00:00
|
|
|
]).then(results => {
|
|
|
|
|
callback({
|
2025-07-06 20:43:22 +00:00
|
|
|
hasOmnipotence: results[0],
|
|
|
|
|
level: results[1],
|
2023-11-29 21:11:22 +00:00
|
|
|
autoReload: rulesetConfig.autoReload,
|
2024-07-29 18:54:46 +00:00
|
|
|
isSideloaded,
|
2024-11-21 17:54:28 +00:00
|
|
|
developerMode: rulesetConfig.developerMode,
|
2025-07-06 20:43:22 +00:00
|
|
|
disabledFeatures: results[2],
|
|
|
|
|
hasCustomFilters: results[3],
|
2022-09-07 14:15:36 +00:00
|
|
|
});
|
2022-09-13 21:44:24 +00:00
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 16:28:24 +00:00
|
|
|
case 'getFilteringMode': {
|
|
|
|
|
getFilteringMode(request.hostname).then(actualLevel => {
|
|
|
|
|
callback(actualLevel);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 18:08:42 +00:00
|
|
|
case 'gotoURL':
|
|
|
|
|
gotoURL(request.url, request.type);
|
|
|
|
|
break;
|
|
|
|
|
|
2022-10-10 16:28:24 +00:00
|
|
|
case 'setFilteringMode': {
|
2025-04-24 23:32:40 +00:00
|
|
|
getFilteringMode(request.hostname).then(beforeLevel => {
|
|
|
|
|
if ( request.level === beforeLevel ) { return beforeLevel; }
|
2022-10-10 16:28:24 +00:00
|
|
|
return setFilteringMode(request.hostname, request.level);
|
2025-04-24 23:32:40 +00:00
|
|
|
}).then(afterLevel => {
|
2022-09-28 14:20:57 +00:00
|
|
|
registerInjectables();
|
2025-04-24 23:32:40 +00:00
|
|
|
callback(afterLevel);
|
2022-10-10 16:28:24 +00:00
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 13:23:27 +00:00
|
|
|
case 'setPendingFilteringMode':
|
|
|
|
|
pendingPermissionRequest = request;
|
|
|
|
|
break;
|
|
|
|
|
|
2024-09-28 18:08:42 +00:00
|
|
|
case 'getDefaultFilteringMode': {
|
|
|
|
|
getDefaultFilteringMode().then(level => {
|
|
|
|
|
callback(level);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 16:46:05 +00:00
|
|
|
case 'setDefaultFilteringMode':
|
2023-06-13 15:40:41 +00:00
|
|
|
getDefaultFilteringMode().then(beforeLevel =>
|
2022-10-10 16:28:24 +00:00
|
|
|
setDefaultFilteringMode(request.level).then(afterLevel =>
|
|
|
|
|
({ beforeLevel, afterLevel })
|
|
|
|
|
)
|
|
|
|
|
).then(({ beforeLevel, afterLevel }) => {
|
|
|
|
|
if ( afterLevel !== beforeLevel ) {
|
|
|
|
|
registerInjectables();
|
|
|
|
|
}
|
|
|
|
|
callback(afterLevel);
|
2022-09-13 21:44:24 +00:00
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-06-13 16:46:05 +00:00
|
|
|
case 'getFilteringModeDetails':
|
|
|
|
|
getFilteringModeDetails(true).then(details => {
|
|
|
|
|
callback(details);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case 'setFilteringModeDetails':
|
|
|
|
|
setFilteringModeDetails(request.modes).then(( ) => {
|
2023-11-29 02:13:44 +00:00
|
|
|
registerInjectables();
|
2025-06-13 16:46:05 +00:00
|
|
|
getDefaultFilteringMode().then(defaultFilteringMode => {
|
|
|
|
|
broadcastMessage({ defaultFilteringMode });
|
|
|
|
|
});
|
|
|
|
|
getFilteringModeDetails(true).then(details => {
|
|
|
|
|
callback(details);
|
2023-11-29 02:13:44 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2024-12-03 21:41:34 +00:00
|
|
|
case 'excludeFromStrictBlock': {
|
|
|
|
|
excludeFromStrictBlock(request.hostname, request.permanent).then(( ) => {
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 18:54:46 +00:00
|
|
|
case 'getMatchedRules':
|
|
|
|
|
getMatchedRules(request.tabId).then(entries => {
|
|
|
|
|
callback(entries);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case 'showMatchedRules':
|
2025-04-24 23:32:40 +00:00
|
|
|
browser.windows.create({
|
2024-07-29 18:54:46 +00:00
|
|
|
type: 'popup',
|
|
|
|
|
url: `/matched-rules.html?tab=${request.tabId}`,
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
|
2025-06-13 16:46:05 +00:00
|
|
|
case 'getEffectiveDynamicRules':
|
|
|
|
|
getEffectiveDynamicRules().then(result => {
|
|
|
|
|
callback(result);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case 'getEffectiveSessionRules':
|
|
|
|
|
getEffectiveSessionRules().then(result => {
|
|
|
|
|
callback(result);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-06-03 10:05:56 +00:00
|
|
|
case 'getEffectiveUserRules':
|
|
|
|
|
getEffectiveUserRules().then(result => {
|
|
|
|
|
callback(result);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
[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
|
|
|
case 'updateUserDnrRules':
|
|
|
|
|
updateUserRules().then(result => {
|
|
|
|
|
callback(result);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-09-24 14:42:18 +00:00
|
|
|
case 'addCustomFilters':
|
|
|
|
|
addCustomFilters(request.hostname, request.selectors).then(modified => {
|
2025-07-06 20:43:22 +00:00
|
|
|
if ( modified !== true ) { return; }
|
|
|
|
|
return registerInjectables();
|
|
|
|
|
}).then(( ) => {
|
|
|
|
|
callback();
|
|
|
|
|
})
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-09-24 14:42:18 +00:00
|
|
|
case 'removeCustomFilters':
|
|
|
|
|
removeCustomFilters(request.hostname, request.selectors).then(modified => {
|
2025-07-06 20:43:22 +00:00
|
|
|
if ( modified !== true ) { return; }
|
|
|
|
|
return registerInjectables();
|
|
|
|
|
}).then(( ) => {
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-09-24 14:42:18 +00:00
|
|
|
case 'removeAllCustomFilters':
|
|
|
|
|
removeAllCustomFilters(request.hostname).then(modified => {
|
|
|
|
|
if ( modified !== true ) { return; }
|
|
|
|
|
return registerInjectables();
|
|
|
|
|
}).then(( ) => {
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case 'customFiltersFromHostname':
|
|
|
|
|
customFiltersFromHostname(request.hostname).then(selectors => {
|
2025-07-06 20:43:22 +00:00
|
|
|
callback(selectors);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-09-24 14:42:18 +00:00
|
|
|
case 'getAllCustomFilters':
|
|
|
|
|
getAllCustomFilters().then(data => {
|
|
|
|
|
callback(data);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2026-01-21 20:40:01 +00:00
|
|
|
case 'getRegisteredContentScripts':
|
|
|
|
|
scrmgr.getRegisteredContentScripts().then(ids => {
|
|
|
|
|
callback(ids);
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
|
2025-09-29 17:07:59 +00:00
|
|
|
case 'getConsoleOutput':
|
|
|
|
|
callback(getConsoleOutput());
|
|
|
|
|
break;
|
2025-08-11 21:53:59 +00:00
|
|
|
|
2022-09-13 21:44:24 +00:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-11-11 18:20:54 +00:00
|
|
|
|
|
|
|
|
return false;
|
2022-09-13 21:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2025-03-21 17:23:54 +00:00
|
|
|
function onCommand(command, tab) {
|
|
|
|
|
switch ( command ) {
|
|
|
|
|
case 'enter-zapper-mode': {
|
|
|
|
|
if ( browser.scripting === undefined ) { return; }
|
|
|
|
|
browser.scripting.executeScript({
|
2025-07-06 20:43:22 +00:00
|
|
|
files: [ '/js/scripting/tool-overlay.js', '/js/scripting/zapper.js' ],
|
2025-03-21 17:23:54 +00:00
|
|
|
target: { tabId: tab.id },
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-07-06 23:06:19 +00:00
|
|
|
case 'enter-picker-mode': {
|
|
|
|
|
if ( browser.scripting === undefined ) { return; }
|
|
|
|
|
browser.scripting.executeScript({
|
2025-08-03 19:50:22 +00:00
|
|
|
files: [
|
|
|
|
|
'/js/scripting/css-procedural-api.js',
|
|
|
|
|
'/js/scripting/tool-overlay.js',
|
|
|
|
|
'/js/scripting/picker.js',
|
|
|
|
|
],
|
2025-07-06 23:06:19 +00:00
|
|
|
target: { tabId: tab.id },
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-03-21 17:23:54 +00:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2025-04-24 23:32:40 +00:00
|
|
|
async function startSession() {
|
2024-12-05 17:56:25 +00:00
|
|
|
const currentVersion = getCurrentVersion();
|
|
|
|
|
const isNewVersion = currentVersion !== rulesetConfig.version;
|
|
|
|
|
|
2025-04-25 15:25:50 +00:00
|
|
|
// Admin settings override user settings
|
|
|
|
|
await loadAdminConfig();
|
|
|
|
|
|
2024-12-05 17:56:25 +00:00
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-20 16:17:57 +00:00
|
|
|
const rulesetsUpdated = await enableRulesets(rulesetConfig.enabledRulesets);
|
2022-09-15 17:14:08 +00:00
|
|
|
|
|
|
|
|
// We need to update the regex rules only when ruleset version changes.
|
2025-08-08 14:30:32 +00:00
|
|
|
if ( rulesetsUpdated === undefined ) {
|
2024-12-29 21:38:56 +00:00
|
|
|
if ( isNewVersion ) {
|
|
|
|
|
updateDynamicRules();
|
2025-04-20 16:17:57 +00:00
|
|
|
} else {
|
2024-12-29 21:38:56 +00:00
|
|
|
updateSessionRules();
|
|
|
|
|
}
|
2022-09-15 17:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 12:48:31 +00:00
|
|
|
// Permissions may have been removed while the extension was disabled
|
2026-01-17 18:19:51 +00:00
|
|
|
const permissionsUpdated = await syncWithBrowserPermissions();
|
2022-10-18 12:48:31 +00:00
|
|
|
|
2026-01-23 17:31:06 +00:00
|
|
|
const shouldInject = isNewVersion || permissionsUpdated ||
|
|
|
|
|
isSideloaded && rulesetConfig.developerMode;
|
|
|
|
|
if ( shouldInject ) {
|
2026-02-09 14:25:57 +00:00
|
|
|
await registerInjectables();
|
2026-01-17 18:19:51 +00:00
|
|
|
}
|
2022-09-15 17:14:08 +00:00
|
|
|
|
2025-12-09 20:32:02 +00:00
|
|
|
// Cosmetic filtering-related content scripts cache fitlering data in
|
|
|
|
|
// session storage.
|
|
|
|
|
sessionAccessLevel({ accessLevel: 'TRUSTED_AND_UNTRUSTED_CONTEXTS' });
|
|
|
|
|
|
2023-04-07 14:19:43 +00:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/declarativeNetRequest
|
|
|
|
|
// Firefox API does not support `dnr.setExtensionActionOptions`
|
2025-04-20 16:17:57 +00:00
|
|
|
if ( canShowBlockedCount ) {
|
2024-03-21 17:57:50 +00:00
|
|
|
dnr.setExtensionActionOptions({
|
|
|
|
|
displayActionCountAsBadgeText: rulesetConfig.showBlockedCount,
|
|
|
|
|
});
|
2023-04-07 14:19:43 +00:00
|
|
|
}
|
2022-09-13 21:44:24 +00:00
|
|
|
|
2025-04-09 19:11:17 +00:00
|
|
|
// Switch to basic filtering if uBOL doesn't have broad permissions at
|
|
|
|
|
// install time.
|
2024-11-11 18:20:54 +00:00
|
|
|
if ( process.firstRun ) {
|
2025-04-09 19:11:17 +00:00
|
|
|
const enableOptimal = await hasBroadHostPermissions();
|
|
|
|
|
if ( enableOptimal === false ) {
|
|
|
|
|
const afterLevel = await setDefaultFilteringMode(MODE_BASIC);
|
|
|
|
|
if ( afterLevel === MODE_BASIC ) {
|
2024-10-17 16:32:28 +00:00
|
|
|
registerInjectables();
|
2024-12-10 15:16:35 +00:00
|
|
|
process.firstRun = false;
|
2024-10-17 16:32:28 +00:00
|
|
|
}
|
2023-07-29 13:33:24 +00:00
|
|
|
}
|
2022-09-30 13:18:52 +00:00
|
|
|
}
|
2024-11-21 17:54:28 +00:00
|
|
|
|
[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
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-04-20 16:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
async function start() {
|
|
|
|
|
await loadRulesetConfig();
|
|
|
|
|
|
2024-11-23 18:17:13 +00:00
|
|
|
if ( process.wakeupRun === false ) {
|
2025-04-24 23:32:40 +00:00
|
|
|
await startSession();
|
2025-12-09 20:32:02 +00:00
|
|
|
} else {
|
|
|
|
|
scrmgr.onWakeupRun();
|
2024-11-23 18:17:13 +00:00
|
|
|
}
|
2025-04-20 16:17:57 +00:00
|
|
|
|
2026-02-09 14:25:57 +00:00
|
|
|
const scripts = await scrmgr.getRegisteredContentScripts();
|
|
|
|
|
if ( scripts.length === 0 ) {
|
|
|
|
|
registerInjectables();
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-20 16:17:57 +00:00
|
|
|
toggleDeveloperMode(rulesetConfig.developerMode);
|
2023-06-06 17:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
2025-03-21 17:23:54 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2025-03-28 02:47:25 +00:00
|
|
|
// 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 => {
|
2025-08-11 21:53:59 +00:00
|
|
|
ubolErr(reason);
|
2025-03-28 02:47:25 +00:00
|
|
|
if ( process.wakeupRun ) { return; }
|
|
|
|
|
return localRead('goodStart').then(goodStart => {
|
|
|
|
|
if ( goodStart === false ) {
|
|
|
|
|
localRemove('goodStart');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return localWrite('goodStart', false).then(( ) => true);
|
2025-03-21 17:23:54 +00:00
|
|
|
});
|
2025-03-28 02:47:25 +00:00
|
|
|
}).then(restart => {
|
|
|
|
|
if ( restart !== true ) { return; }
|
|
|
|
|
runtime.reload();
|
2025-03-21 17:23:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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(( ) => {
|
2025-10-18 14:03:10 +00:00
|
|
|
onPermissionsChanged('removed', ...args);
|
2025-03-21 17:23:54 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
browser.permissions.onAdded.addListener((...args) => {
|
|
|
|
|
isFullyInitialized.then(( ) => {
|
2025-10-18 14:03:10 +00:00
|
|
|
onPermissionsChanged('added', ...args);
|
2025-03-21 17:23:54 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
browser.commands.onCommand.addListener((...args) => {
|
|
|
|
|
isFullyInitialized.then(( ) => {
|
|
|
|
|
onCommand(...args);
|
2024-09-06 13:31:17 +00:00
|
|
|
});
|
2024-09-22 16:14:52 +00:00
|
|
|
});
|