Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 1 | // #!if browser_target == 'chromium_mv3' |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 2 | import XMLHttpRequest from 'sw-xhr'; |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 3 | // #!endif |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 4 | |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 5 | import actionApi from './common/actionApi.js'; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 6 | import {cleanUpOptPermissions} from './common/optionsPermissions.js'; |
| 7 | import {cleanUpOptions, disableItemsWithMissingPermissions} from './common/optionsUtils.js'; |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 8 | import KillSwitchMechanism from './killSwitch/index.js'; |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 9 | import {handleBgOptionChange, handleBgOptionsOnStart} from './options/bgHandler.js'; |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 10 | |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 11 | // #!if browser_target == 'chromium_mv3' |
| 12 | // XMLHttpRequest is not present in service workers (MV3) and is required by the |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 13 | // grpc-web package. Importing a shim to work around this. |
| 14 | // https://github.com/grpc/grpc-web/issues/1134 |
| 15 | self.XMLHttpRequest = XMLHttpRequest; |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 16 | // #!endif |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 17 | |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 18 | actionApi.onClicked.addListener(() => { |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 19 | chrome.runtime.openOptionsPage(); |
| 20 | }); |
| 21 | |
| 22 | const killSwitchMechanism = new KillSwitchMechanism(); |
| 23 | |
| 24 | chrome.alarms.create('updateKillSwitchStatus', { |
| 25 | periodInMinutes: PRODUCTION ? 30 : 1, |
| 26 | }); |
| 27 | |
| 28 | chrome.alarms.onAlarm.addListener(alarm => { |
| 29 | if (alarm.name === 'updateKillSwitchStatus') |
| 30 | killSwitchMechanism.updateKillSwitchStatus(); |
| 31 | }); |
| 32 | |
| 33 | // When the profile is first started, update the kill switch status. |
| 34 | chrome.runtime.onStartup.addListener(() => { |
| 35 | killSwitchMechanism.updateKillSwitchStatus(); |
| 36 | }); |
| 37 | |
| 38 | // When the extension is first installed or gets updated, set new options to |
| 39 | // their default value and update the kill switch status. |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 40 | chrome.runtime.onInstalled.addListener(details => { |
| 41 | if (details.reason == 'install' || details.reason == 'update') { |
| 42 | chrome.storage.sync.get(null, options => { |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 43 | cleanUpOptions(options, false); |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 44 | }); |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 45 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 46 | killSwitchMechanism.updateKillSwitchStatus(); |
| 47 | } |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 48 | }); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 49 | |
| 50 | // Clean up optional permissions and check that none are missing for enabled |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 51 | // features, and also handle background option changes as soon as the extension |
| 52 | // starts and when the options change. |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 53 | cleanUpOptPermissions(); |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 54 | handleBgOptionsOnStart(); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 55 | |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 56 | chrome.storage.sync.onChanged.addListener(changes => { |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 57 | cleanUpOptPermissions(); |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 58 | |
| 59 | for (let [key, {oldValue, newValue}] of Object.entries(changes)) { |
| 60 | handleBgOptionChange(key); |
| 61 | } |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 62 | }); |
| 63 | |
| 64 | chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { |
| 65 | if (sender.id !== chrome.runtime.id) |
| 66 | return console.warn( |
| 67 | 'An unknown sender (' + sender.id + |
| 68 | ') sent a message to the extension: ', |
| 69 | msg); |
| 70 | |
| 71 | console.assert(msg.message); |
| 72 | switch (msg.message) { |
| 73 | case 'runDisableItemsWithMissingPermissions': |
| 74 | console.assert( |
| 75 | msg.options?.items && msg.options?.permissionChecksFeatures); |
| 76 | disableItemsWithMissingPermissions( |
| 77 | msg.options?.items, msg.options?.permissionChecksFeatures) |
| 78 | .then(items => sendResponse({status: 'resolved', items})) |
| 79 | .catch(error => sendResponse({status: 'rejected', error})); |
| 80 | break; |
| 81 | |
| 82 | default: |
| 83 | console.warn('Unknown message "' + msg.message + '".'); |
| 84 | } |
| 85 | }); |