Repository navigation
Expand file tree
/
Copy pathpopup.js
More file actions
119 lines (101 loc) · 3.83 KB
/
Copy pathpopup.js
File metadata and controls
119 lines (101 loc) · 3.83 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"use strict";
/**
* Configuration mapping for the popup toggles.
* Defaults match those defined in background.js.
*/
const SETTINGS_CONFIG = [
{ id: "openNextToCurrent", key: "openNextToCurrent", default: true },
{ id: "inheritTabGroup", key: "inheritTabGroup", default: true },
];
document.addEventListener("DOMContentLoaded", async () => {
// 1. Verify browser capability (Tab Groups API is available in Firefox 138+)
checkTabGroupSupport();
// 2. Load stored preferences and attach change listeners
await initSettings();
// 3. Display current tab container status badge
await updateCurrentContainerStatus();
});
/**
* Checks if browser.tabs.group exists. If not, disables the setting row gracefully.
*/
function checkTabGroupSupport() {
const supportsTabGroups = typeof browser.tabs?.group === "function";
if (!supportsTabGroups) {
const checkbox = document.getElementById("inheritTabGroup");
if (!checkbox) return;
checkbox.disabled = true;
const row = checkbox.closest(".setting-row");
if (row) {
row.classList.add("disabled");
row.title = "Requires Firefox 138 or later with tab groups enabled";
}
}
}
/**
* Reads preferences from browser.storage.local, sets toggle states,
* and handles writing updates back to storage on change.
*/
async function initSettings() {
// Construct lookup defaults object
const defaults = Object.fromEntries(
SETTINGS_CONFIG.map(({ key, default: defVal }) => [key, defVal])
);
try {
const settings = await browser.storage.local.get(defaults);
for (const { id, key } of SETTINGS_CONFIG) {
const checkbox = document.getElementById(id);
if (!checkbox) continue;
// Sync checkbox state with storage
const value = Boolean(settings[key]);
checkbox.checked = value;
checkbox.setAttribute("aria-checked", String(value));
// Persist changes immediately on user interaction
checkbox.addEventListener("change", async () => {
const isChecked = checkbox.checked;
checkbox.setAttribute("aria-checked", String(isChecked));
try {
await browser.storage.local.set({ [key]: isChecked });
} catch (error) {
console.error(`Persistent Containers: Failed to save '${key}':`, error);
}
});
}
} catch (err) {
console.error("Persistent Containers: Error retrieving settings:", err);
}
}
/**
* Inspects the current active tab and shows container information in the status badge.
*/
async function updateCurrentContainerStatus() {
const statusText = document.getElementById("containerText");
const statusDot = document.getElementById("containerDot");
if (!statusText || !statusDot) return;
try {
const [activeTab] = await browser.tabs.query({
active: true,
currentWindow: true,
});
// Handle non-container tabs or missing contextual identity
if (!activeTab || !activeTab.cookieStoreId || activeTab.cookieStoreId === "firefox-default") {
statusText.textContent = "Default Container (no container)";
statusDot.style.backgroundColor = "#999999";
return;
}
if (activeTab.incognito || activeTab.cookieStoreId === "firefox-private") {
statusText.textContent = "Private Window (inactive)";
statusDot.style.backgroundColor = "#800080";
return;
}
// Query container metadata (name and color) from Firefox Contextual Identities API
if (browser.contextualIdentities) {
const identity = await browser.contextualIdentities.get(activeTab.cookieStoreId);
statusText.textContent = `Active: ${identity.name}`;
statusDot.style.backgroundColor = identity.colorCode || "#37adff";
}
} catch {
// If permissions are restricted or tab cannot be read, display general status
statusText.textContent = "Monitoring tab inheritance";
statusDot.style.backgroundColor = "#37adff";
}
}