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