Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 1 | // 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à Vilanova | 61f782f | 2024-05-31 22:57:24 +0000 | [diff] [blame] | 6 | // An example is the "blockdrafts" feature, which when enabled should enable the |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 7 | // static ruleset blocking *DraftMessages requests. |
| 8 | |
Adrià Vilanova Martínez | b523be9 | 2024-05-25 19:14:19 +0200 | [diff] [blame] | 9 | import {isOptionEnabled} from '../common/options/optionsUtils.js'; |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 10 | |
| 11 | // List of features controled in the background: |
Adrià Vilanova | 61f782f | 2024-05-31 22:57:24 +0000 | [diff] [blame] | 12 | export var bgFeatures = [ |
| 13 | 'blockdrafts', |
| 14 | ]; |
| 15 | |
| 16 | const blockDraftsRuleset = 'blockDrafts'; |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 17 | |
| 18 | export function handleBgOptionChange(feature) { |
| 19 | isOptionEnabled(feature) |
| 20 | .then(enabled => { |
Adrià Vilanova | 61f782f | 2024-05-31 22:57:24 +0000 | [diff] [blame] | 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 | } |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 42 | }) |
| 43 | .catch(err => { |
| 44 | console.error( |
| 45 | 'handleBgOptionChange: error while handling feature "' + feature + |
| 46 | '": ', |
| 47 | err); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | export function handleBgOptionsOnStart() { |
| 52 | for (let feature of bgFeatures) handleBgOptionChange(feature); |
| 53 | } |