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 | 258b4aa | 2022-01-15 22:01:01 +0100 | [diff] [blame] | 18 | // Returns whether the script is being ran when the extension starts up. It does |
| 19 | // so on a best-effort-basis. |
| 20 | function isExtensionStartup() { |
| 21 | // If chrome.storage.session isn't implemented in this version of Chrome, we |
| 22 | // don't know whether it's the extension startup. |
| 23 | if (!chrome.storage.session) return Promise.resolve(true); |
| 24 | |
| 25 | return new Promise((resolve, reject) => { |
| 26 | return chrome.storage.session.get('hasAlreadyStarted', v => { |
| 27 | resolve(v.hasAlreadyStarted !== true); |
| 28 | }); |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | // Sets that the extension has already started up |
| 33 | function setHasAlreadyStarted() { |
| 34 | if (!chrome.storage.session) return; |
| 35 | chrome.storage.session.set({ |
| 36 | hasAlreadyStarted: true, |
| 37 | }); |
| 38 | } |
| 39 | |
Adrià Vilanova Martínez | 54fbad1 | 2022-01-04 03:39:04 +0100 | [diff] [blame] | 40 | actionApi.onClicked.addListener(() => { |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 41 | chrome.runtime.openOptionsPage(); |
| 42 | }); |
| 43 | |
| 44 | const killSwitchMechanism = new KillSwitchMechanism(); |
| 45 | |
Adrià Vilanova Martínez | 258b4aa | 2022-01-15 22:01:01 +0100 | [diff] [blame] | 46 | chrome.alarms.get('updateKillSwitchStatus', alarm => { |
| 47 | if (!alarm) |
| 48 | chrome.alarms.create('updateKillSwitchStatus', { |
| 49 | periodInMinutes: PRODUCTION ? 30 : 1, |
| 50 | }); |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 51 | }); |
| 52 | |
| 53 | chrome.alarms.onAlarm.addListener(alarm => { |
| 54 | if (alarm.name === 'updateKillSwitchStatus') |
| 55 | killSwitchMechanism.updateKillSwitchStatus(); |
| 56 | }); |
| 57 | |
| 58 | // When the profile is first started, update the kill switch status. |
| 59 | chrome.runtime.onStartup.addListener(() => { |
| 60 | killSwitchMechanism.updateKillSwitchStatus(); |
| 61 | }); |
| 62 | |
| 63 | // When the extension is first installed or gets updated, set new options to |
| 64 | // their default value and update the kill switch status. |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 65 | chrome.runtime.onInstalled.addListener(details => { |
| 66 | if (details.reason == 'install' || details.reason == 'update') { |
| 67 | chrome.storage.sync.get(null, options => { |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 68 | cleanUpOptions(options, false); |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 69 | }); |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 70 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 71 | killSwitchMechanism.updateKillSwitchStatus(); |
| 72 | } |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 73 | }); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 74 | |
| 75 | // 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] | 76 | // features, and also handle background option changes as soon as the extension |
| 77 | // starts and when the options change. |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 78 | chrome.storage.sync.onChanged.addListener(changes => { |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 79 | cleanUpOptPermissions(); |
Adrià Vilanova Martínez | a4dd5fd | 2022-01-05 04:23:44 +0100 | [diff] [blame] | 80 | |
| 81 | for (let [key, {oldValue, newValue}] of Object.entries(changes)) { |
| 82 | handleBgOptionChange(key); |
| 83 | } |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 84 | }); |
| 85 | |
| 86 | chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { |
| 87 | if (sender.id !== chrome.runtime.id) |
| 88 | return console.warn( |
| 89 | 'An unknown sender (' + sender.id + |
| 90 | ') sent a message to the extension: ', |
| 91 | msg); |
| 92 | |
| 93 | console.assert(msg.message); |
| 94 | switch (msg.message) { |
| 95 | case 'runDisableItemsWithMissingPermissions': |
| 96 | console.assert( |
| 97 | msg.options?.items && msg.options?.permissionChecksFeatures); |
| 98 | disableItemsWithMissingPermissions( |
| 99 | msg.options?.items, msg.options?.permissionChecksFeatures) |
| 100 | .then(items => sendResponse({status: 'resolved', items})) |
| 101 | .catch(error => sendResponse({status: 'rejected', error})); |
| 102 | break; |
| 103 | |
| 104 | default: |
| 105 | console.warn('Unknown message "' + msg.message + '".'); |
| 106 | } |
| 107 | }); |
Adrià Vilanova Martínez | 258b4aa | 2022-01-15 22:01:01 +0100 | [diff] [blame] | 108 | |
| 109 | // This should only run once when the extension starts up. |
| 110 | isExtensionStartup().then(isStartup => { |
| 111 | if (isStartup) { |
| 112 | cleanUpOptPermissions(); |
| 113 | handleBgOptionsOnStart(); |
| 114 | setHasAlreadyStarted(); |
| 115 | } |
| 116 | }); |