blob: a9d80ab10462724e9b1d82548a339329f3ba972c [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//
6// An example is the "blockdrafts" feature, which when enabled should enable the
7// static ruleset blocking *DraftMessages requests.
8
9import {isOptionEnabled} from '../common/optionsUtils.js';
10
11// List of features controled in the background:
12export var bgFeatures = [
13 'blockdrafts',
14];
15
16const blockDraftsRuleset = 'blockDrafts';
17
18export function handleBgOptionChange(feature) {
19 isOptionEnabled(feature)
20 .then(enabled => {
21 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 }
42 })
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}