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