-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground_script.js
36 lines (30 loc) · 925 Bytes
/
background_script.js
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
const pinnedTabs = new Set();
const getPinnedTabs = () => browser.tabs.query({ pinned: true });
getPinnedTabs().then((tabs) =>
tabs.forEach((tab) =>
pinnedTabs.add(tab.id)
)
);
const onPinned = (tabId, changeInfo) => {
// the tab has been unpinned
if (pinnedTabs.has(tabId) && !changeInfo.pinned) {
pinnedTabs.delete(tabId);
} else {
pinnedTabs.add(tabId);
}
};
browser.tabs.onUpdated.addListener(onPinned, { properties: ["pinned"] });
const onAttached = (tabId) => {
// we have moved to a new window, so re-apply the pin
if (pinnedTabs.has(tabId)) {
browser.tabs.update(tabId, { pinned: true }).then(
(tab) => console.log(`Updated tab: ${tab.id}`),
(error) => console.log(`Error: ${error}`)
);
}
};
browser.tabs.onAttached.addListener(onAttached);
// memory cleanup
browser.tabs.onRemoved.addListener(
(tabId) => pinnedTabs.has(tabId) && pinnedTabs.delete(tabId)
);