avm99963 | 3a4946d | 2021-02-05 21:27:12 +0100 | [diff] [blame] | 1 | // IMPORTANT: keep this file in sync with sw.js |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame^] | 2 | import {cleanUpOptPermissions} from './common/optionsPermissions.js'; |
| 3 | import {cleanUpOptions, disableItemsWithMissingPermissions} from './common/optionsUtils.js'; |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 4 | import KillSwitchMechanism from './killSwitch/index.js'; |
avm99963 | 1a1d2b7 | 2020-08-20 03:15:20 +0200 | [diff] [blame] | 5 | |
| 6 | chrome.browserAction.onClicked.addListener(function() { |
| 7 | chrome.runtime.openOptionsPage(); |
| 8 | }); |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 9 | |
| 10 | const killSwitchMechanism = new KillSwitchMechanism(); |
| 11 | |
| 12 | chrome.alarms.create('updateKillSwitchStatus', { |
| 13 | periodInMinutes: PRODUCTION ? 30 : 1, |
| 14 | }); |
| 15 | |
| 16 | chrome.alarms.onAlarm.addListener(alarm => { |
| 17 | if (alarm.name === 'updateKillSwitchStatus') |
| 18 | killSwitchMechanism.updateKillSwitchStatus(); |
| 19 | }); |
| 20 | |
| 21 | // When the profile is first started, update the kill switch status. |
| 22 | chrome.runtime.onStartup.addListener(() => { |
| 23 | killSwitchMechanism.updateKillSwitchStatus(); |
| 24 | }); |
| 25 | |
| 26 | // When the extension is first installed or gets updated, set new options to |
| 27 | // their default value and update the kill switch status. |
| 28 | chrome.runtime.onInstalled.addListener(details => { |
| 29 | if (details.reason == 'install' || details.reason == 'update') { |
| 30 | chrome.storage.sync.get(null, options => { |
| 31 | cleanUpOptions(options, false); |
| 32 | }); |
| 33 | |
| 34 | killSwitchMechanism.updateKillSwitchStatus(); |
| 35 | } |
| 36 | }); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame^] | 37 | |
| 38 | // Clean up optional permissions and check that none are missing for enabled |
| 39 | // features as soon as the extension starts and when the options change. |
| 40 | cleanUpOptPermissions(); |
| 41 | |
| 42 | chrome.storage.sync.onChanged.addListener(() => { |
| 43 | cleanUpOptPermissions(); |
| 44 | }); |
| 45 | |
| 46 | chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { |
| 47 | if (sender.id !== chrome.runtime.id) |
| 48 | return console.warn( |
| 49 | 'An unknown sender (' + sender.id + |
| 50 | ') sent a message to the extension: ', |
| 51 | msg); |
| 52 | |
| 53 | console.assert(msg.message); |
| 54 | switch (msg.message) { |
| 55 | case 'runDisableItemsWithMissingPermissions': |
| 56 | console.assert( |
| 57 | msg.options?.items && msg.options?.permissionChecksFeatures); |
| 58 | disableItemsWithMissingPermissions( |
| 59 | msg.options?.items, msg.options?.permissionChecksFeatures) |
| 60 | .then(items => sendResponse({status: 'resolved', items})) |
| 61 | .catch(error => sendResponse({status: 'rejected', error})); |
| 62 | break; |
| 63 | |
| 64 | default: |
| 65 | console.warn('Unknown message "' + msg.message + '".'); |
| 66 | } |
| 67 | }); |