-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
42 lines (34 loc) · 1.3 KB
/
background.js
File metadata and controls
42 lines (34 loc) · 1.3 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
import { openOrFocusFloatingWindow } from './lib/window-launcher.js';
const DEFAULT_CLICK_MODE_KEY = 'defaultActionClickMode';
const DEFAULT_CLICK_MODE = 'sidepanel';
let cachedClickMode = DEFAULT_CLICK_MODE;
async function getDefaultClickMode() {
const result = await chrome.storage.local.get([DEFAULT_CLICK_MODE_KEY]);
const value = result[DEFAULT_CLICK_MODE_KEY];
if (value === 'floating' || value === 'sidepanel') {
return value;
}
return DEFAULT_CLICK_MODE;
}
async function refreshCachedClickMode() {
const mode = await getDefaultClickMode();
cachedClickMode = mode;
}
refreshCachedClickMode().catch(() => {});
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName !== 'local' || !changes[DEFAULT_CLICK_MODE_KEY]) return;
const nextValue = changes[DEFAULT_CLICK_MODE_KEY].newValue;
cachedClickMode = nextValue === 'floating' ? 'floating' : DEFAULT_CLICK_MODE;
});
chrome.action.onClicked.addListener((tab) => {
const clickMode = cachedClickMode;
if (clickMode === 'floating') {
openOrFocusFloatingWindow().catch(() => {});
return;
}
if (chrome.sidePanel?.open && Number.isInteger(tab?.windowId)) {
chrome.sidePanel.open({ windowId: tab.windowId })
.catch(() => {});
return;
}
});