blob: c97c93591387f1c11913b057f4740478bf8d7e38 [file] [log] [blame]
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +01001// Most options are dynamic, which means whenever they are enabled or disabled,
2// the effect is immediate. However, some features aren't controlled directly in
3// content scripts or injected scripts but instead in the background
4// script/service worker.
5//
Adrià Vilanova61f782f2024-05-31 22:57:24 +00006// An example is the "blockdrafts" feature, which when enabled should enable the
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +01007// static ruleset blocking *DraftMessages requests.
8
Adrià Vilanova Martínezb523be92024-05-25 19:14:19 +02009import {isOptionEnabled} from '../common/options/optionsUtils.js';
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010010
11// List of features controled in the background:
Adrià Vilanova61f782f2024-05-31 22:57:24 +000012export var bgFeatures = [
13 'blockdrafts',
14];
15
16const blockDraftsRuleset = 'blockDrafts';
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010017
18export function handleBgOptionChange(feature) {
19 isOptionEnabled(feature)
20 .then(enabled => {
Adrià Vilanova61f782f2024-05-31 22:57:24 +000021 switch (feature) {
22 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
23 case 'blockdrafts':
24 chrome.declarativeNetRequest.getEnabledRulesets(rulesets => {
25 if (rulesets === undefined) {
26 throw new Error(
27 chrome.runtime.lastError.message ??
28 'Unknown error in chrome.declarativeNetRequest.getEnabledRulesets()');
29 }
30
31 let isRulesetEnabled = rulesets.includes(blockDraftsRuleset);
32 if (!isRulesetEnabled && enabled)
33 chrome.declarativeNetRequest.updateEnabledRulesets(
34 {enableRulesetIds: [blockDraftsRuleset]});
35 if (isRulesetEnabled && !enabled)
36 chrome.declarativeNetRequest.updateEnabledRulesets(
37 {disableRulesetIds: [blockDraftsRuleset]});
38 });
39 break;
40 // #!endif
41 }
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010042 })
43 .catch(err => {
44 console.error(
45 'handleBgOptionChange: error while handling feature "' + feature +
46 '": ',
47 err);
48 });
49}
50
51export function handleBgOptionsOnStart() {
52 for (let feature of bgFeatures) handleBgOptionChange(feature);
53}