blob: fce2328c92c423f4edfd18cbedb478ce7e85175d [file] [log] [blame]
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +01001// #!if browser_target == 'chromium_mv3'
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +02002import XMLHttpRequest from 'sw-xhr';
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +01003// #!endif
avm99963bbc88c62020-12-25 03:44:41 +01004
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +01005import actionApi from './common/actionApi.js';
Adrià Vilanova Martínezb523be92024-05-25 19:14:19 +02006import {cleanUpOptPermissions} from './common/options/optionsPermissions.js';
7import {cleanUpOptions, disableItemsWithMissingPermissions} from './common/options/optionsUtils.js';
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +02008import KillSwitchMechanism from './killSwitch/index.js';
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +01009import {handleBgOptionChange, handleBgOptionsOnStart} from './options/bgHandler.js';
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +020010import UpdateNotifier from './updateNotifier/index.js';
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020011
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010012// #!if browser_target == 'chromium_mv3'
13// XMLHttpRequest is not present in service workers (MV3) and is required by the
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020014// grpc-web package. Importing a shim to work around this.
15// https://github.com/grpc/grpc-web/issues/1134
16self.XMLHttpRequest = XMLHttpRequest;
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010017// #!endif
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020018
Adrià Vilanova Martínez258b4aa2022-01-15 22:01:01 +010019// Returns whether the script is being ran when the extension starts up. It does
20// so on a best-effort-basis.
21function 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ínezfae8a962022-12-29 23:43:13 +010026 return new Promise(resolve => {
Adrià Vilanova Martínez258b4aa2022-01-15 22:01:01 +010027 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
34function setHasAlreadyStarted() {
35 if (!chrome.storage.session) return;
36 chrome.storage.session.set({
37 hasAlreadyStarted: true,
38 });
39}
40
Adrià Vilanova Martínez54fbad12022-01-04 03:39:04 +010041actionApi.onClicked.addListener(() => {
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020042 chrome.runtime.openOptionsPage();
43});
44
45const killSwitchMechanism = new KillSwitchMechanism();
46
Adrià Vilanova Martínez258b4aa2022-01-15 22:01:01 +010047chrome.alarms.get('updateKillSwitchStatus', alarm => {
48 if (!alarm)
49 chrome.alarms.create('updateKillSwitchStatus', {
50 periodInMinutes: PRODUCTION ? 30 : 1,
51 });
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020052});
53
54chrome.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.
60chrome.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ínez5f5b3e02023-07-23 00:08:17 +020065// their default value, update the kill switch status and prompt the user to
66// refresh the Community Console page.
avm99963bbc88c62020-12-25 03:44:41 +010067chrome.runtime.onInstalled.addListener(details => {
68 if (details.reason == 'install' || details.reason == 'update') {
69 chrome.storage.sync.get(null, options => {
avm99963bf8eece2021-04-22 00:27:03 +020070 cleanUpOptions(options, false);
avm99963bbc88c62020-12-25 03:44:41 +010071 });
avm99963bbc88c62020-12-25 03:44:41 +010072
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020073 killSwitchMechanism.updateKillSwitchStatus();
Adrià Vilanova Martínez5f5b3e02023-07-23 00:08:17 +020074
75 const updateNotifier = new UpdateNotifier();
76 updateNotifier.notify(details.reason);
Adrià Vilanova Martínez413cb442021-09-06 00:30:45 +020077 }
avm99963bbc88c62020-12-25 03:44:41 +010078});
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +010079
80// Clean up optional permissions and check that none are missing for enabled
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010081// features, and also handle background option changes as soon as the extension
82// starts and when the options change.
Adrià Vilanova Martínezbc4d6a32022-02-12 16:47:34 +010083chrome.storage.onChanged.addListener((changes, areaName) => {
84 if (areaName !== 'sync') return;
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +010085 cleanUpOptPermissions();
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010086
Adrià Vilanova Martínezfae8a962022-12-29 23:43:13 +010087 for (let [key] of Object.entries(changes)) {
Adrià Vilanova Martíneza4dd5fd2022-01-05 04:23:44 +010088 handleBgOptionChange(key);
89 }
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +010090});
91
92chrome.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ínez96ae96f2022-10-17 23:50:36 +0200110 case 'openWorkflowsManager':
111 chrome.tabs.create({
112 url: chrome.runtime.getURL('options/workflows.html'),
113 });
Adrià Vilanova Martínezfae8a962022-12-29 23:43:13 +0100114 break;
Adrià Vilanova Martínez96ae96f2022-10-17 23:50:36 +0200115
Adrià Vilanova Martínez5120dbb2022-01-04 03:21:17 +0100116 default:
117 console.warn('Unknown message "' + msg.message + '".');
118 }
119});
Adrià Vilanova Martínez258b4aa2022-01-15 22:01:01 +0100120
121// This should only run once when the extension starts up.
122isExtensionStartup().then(isStartup => {
123 if (isStartup) {
124 cleanUpOptPermissions();
125 handleBgOptionsOnStart();
126 setHasAlreadyStarted();
127 }
128});