-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
46 lines (39 loc) · 1.12 KB
/
background.js
File metadata and controls
46 lines (39 loc) · 1.12 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
// background.js
// Service worker (Manifest V3)
//
// Keeps track of which tabs have the feature enabled.
// State is stored in memory only (resets when extension reloads).
const tabs = new Map();
// key: tabId
// value: { active: boolean, fieldCount: number }
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
const { type, tabId, active, fieldCount } = message;
if (type === "GET_STATE") {
// If we don't have anything stored for this tab,
// return default values.
const state = tabs.get(tabId);
sendResponse(state || { active: false, fieldCount: 0 });
return true;
}
if (type === "SET_STATE") {
tabs.set(tabId, {
active,
fieldCount
});
sendResponse({ ok: true });
return true;
}
});
// Remove stored data when the tab is closed
chrome.tabs.onRemoved.addListener((tabId) => {
if (tabs.has(tabId)) {
tabs.delete(tabId);
}
});
// Clear state when a new page loads in the tab
chrome.webNavigation.onCommitted.addListener((details) => {
// Only reset on top-level navigation
if (details.frameId === 0) {
tabs.delete(details.tabId);
}
});