-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
71 lines (59 loc) · 1.57 KB
/
Copy pathbackground.js
File metadata and controls
71 lines (59 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
importScripts('shared.js');
(() => {
'use strict';
const {
DEFAULT_SETTINGS,
MESSAGE_ACTIONS,
COMMANDS,
normalizeSettings,
areSettingsEqual
} = globalThis.PaperLikeShared;
async function readRawSettings() {
try {
return await chrome.storage.sync.get(DEFAULT_SETTINGS);
} catch (_error) {
return { ...DEFAULT_SETTINGS };
}
}
async function normalizePersistedSettings() {
const current = await readRawSettings();
const normalized = normalizeSettings(current);
if (!areSettingsEqual(current, normalized)) {
await chrome.storage.sync.set(normalized);
return normalized;
}
return current;
}
chrome.runtime.onInstalled.addListener((details) => {
void (async () => {
const normalized = await normalizePersistedSettings();
if (details.reason === 'install') {
await chrome.storage.sync.set(normalized);
}
})();
});
chrome.commands.onCommand.addListener((command) => {
if (command !== COMMANDS.TOGGLE) {
return;
}
void (async () => {
let tabId = null;
try {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (tabs[0] && typeof tabs[0].id === 'number') {
tabId = tabs[0].id;
}
} catch (_error) {
return;
}
if (tabId === null) {
return;
}
try {
await chrome.tabs.sendMessage(tabId, { action: MESSAGE_ACTIONS.TOGGLE });
} catch (_error) {
// Content script is not available in this tab.
}
})();
});
})();