avm99963 | 3a4946d | 2021-02-05 21:27:12 +0100 | [diff] [blame] | 1 | // IMPORTANT: keep this file in sync with background.js |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 2 | import XMLHttpRequest from 'sw-xhr'; |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 3 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 4 | import {cleanUpOptions} from './common/optionsUtils.js'; |
| 5 | import KillSwitchMechanism from './killSwitch/index.js'; |
| 6 | |
| 7 | // XMLHttpRequest is not present in service workers and is required by the |
| 8 | // grpc-web package. Importing a shim to work around this. |
| 9 | // https://github.com/grpc/grpc-web/issues/1134 |
| 10 | self.XMLHttpRequest = XMLHttpRequest; |
| 11 | |
| 12 | chrome.action.onClicked.addListener(_ => { |
| 13 | chrome.runtime.openOptionsPage(); |
| 14 | }); |
| 15 | |
| 16 | const killSwitchMechanism = new KillSwitchMechanism(); |
| 17 | |
| 18 | chrome.alarms.create('updateKillSwitchStatus', { |
| 19 | periodInMinutes: PRODUCTION ? 30 : 1, |
| 20 | }); |
| 21 | |
| 22 | chrome.alarms.onAlarm.addListener(alarm => { |
| 23 | if (alarm.name === 'updateKillSwitchStatus') |
| 24 | killSwitchMechanism.updateKillSwitchStatus(); |
| 25 | }); |
| 26 | |
| 27 | // When the profile is first started, update the kill switch status. |
| 28 | chrome.runtime.onStartup.addListener(() => { |
| 29 | killSwitchMechanism.updateKillSwitchStatus(); |
| 30 | }); |
| 31 | |
| 32 | // When the extension is first installed or gets updated, set new options to |
| 33 | // their default value and update the kill switch status. |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 34 | chrome.runtime.onInstalled.addListener(details => { |
| 35 | if (details.reason == 'install' || details.reason == 'update') { |
| 36 | chrome.storage.sync.get(null, options => { |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 37 | cleanUpOptions(options, false); |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 38 | }); |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 39 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 40 | killSwitchMechanism.updateKillSwitchStatus(); |
| 41 | } |
avm99963 | bbc88c6 | 2020-12-25 03:44:41 +0100 | [diff] [blame] | 42 | }); |