-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
257 lines (224 loc) · 7.45 KB
/
Copy pathpopup.js
File metadata and controls
257 lines (224 loc) · 7.45 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Popup UI logic - global and per-site protection controls.
import {
getActiveTab,
getHostnameFromUrl,
isHostAllowlisted,
isHostTemporarilyEnabled,
loadDomainSettings,
setTemporaryOverride,
} from "./popup-settings.js";
/**
* Initialize popup UI and event handlers.
*/
(async function init() {
const protectionStateText = document.getElementById("protectionStateText");
const statusDot = document.getElementById("statusDot");
const statusText = document.getElementById("statusText");
const installHint = document.getElementById("installHint");
const installHintBaseText =
"Please install Redactosaurus on your computer to enable protection.";
const domainHint = document.getElementById("domainHint");
const siteHost = document.getElementById("siteHost");
const siteStatus = document.getElementById("siteStatus");
const siteToggle = document.getElementById("siteToggle");
const siteToggleSection = document.getElementById("siteToggleSection");
const activeTab = await getActiveTab();
const tabId = activeTab?.id;
const currentHost = getHostnameFromUrl(activeTab?.url || "");
let serviceEnabled = false;
let nativeHostAvailable = true;
let nativeHostError = null;
let hasConfiguredDomains = true;
let allowlisted = false;
let temporarilyEnabled = false;
let matchedAllowlistDomain = null;
let serviceRefreshTimer = null;
async function loadSiteDomainState() {
const domains = await loadDomainSettings();
hasConfiguredDomains = domains.allowedDomains.length > 0;
if (!currentHost) {
allowlisted = false;
temporarilyEnabled = false;
matchedAllowlistDomain = null;
return;
}
if (!hasConfiguredDomains) {
allowlisted = false;
temporarilyEnabled = false;
matchedAllowlistDomain = null;
return;
}
allowlisted = isHostAllowlisted(currentHost, domains.allowedDomains);
temporarilyEnabled = isHostTemporarilyEnabled(
currentHost,
domains.temporaryEnabledDomains,
);
matchedAllowlistDomain =
domains.allowedDomains.find(
(domain) =>
currentHost === domain || currentHost.endsWith(`.${domain}`),
) || null;
if (allowlisted) {
temporarilyEnabled = false;
}
}
function renderSiteState() {
if (!currentHost) {
siteHost.textContent = "Unavailable on this page";
siteStatus.textContent = "Open any regular website tab to use site controls.";
siteToggleSection.style.display = "none";
return;
}
siteHost.textContent = currentHost;
if (!hasConfiguredDomains) {
siteStatus.textContent = "Please add at least one domain to protect.";
siteToggleSection.style.display = "none";
return;
}
if (allowlisted) {
siteStatus.textContent = matchedAllowlistDomain
? `Included by allowlist (${matchedAllowlistDomain})`
: "Included by allowlist";
siteToggleSection.style.display = "none";
return;
}
siteToggleSection.style.display = "flex";
siteToggle.disabled = nativeHostAvailable === false;
siteToggle.checked = temporarilyEnabled;
siteStatus.textContent = temporarilyEnabled
? "Temporarily enabled for this site"
: "Not in default domain list";
}
function render() {
if (nativeHostAvailable === false) {
protectionStateText.textContent = "Unavailable";
statusDot.className = "status-dot inactive";
statusText.textContent = "Protection Unavailable";
installHint.textContent = nativeHostError
? `${installHintBaseText} Details: ${nativeHostError}`
: installHintBaseText;
installHint.style.display = "block";
domainHint.style.display = "none";
} else if (!hasConfiguredDomains) {
protectionStateText.textContent = "Off";
statusDot.className = "status-dot inactive";
statusText.textContent = "Protection Disabled";
installHint.style.display = "none";
domainHint.style.display = "block";
} else {
installHint.style.display = "none";
domainHint.style.display = "none";
protectionStateText.textContent = serviceEnabled ? "On" : "Off";
if (serviceEnabled) {
statusDot.className = "status-dot active";
statusText.textContent = "Protection Active";
} else {
statusDot.className = "status-dot inactive";
statusText.textContent = "Protection Disabled";
}
}
renderSiteState();
}
// Load current service state from storage.
const stored = await chrome.storage.local.get([
"serviceEnabled",
"nativeHostAvailable",
"nativeHostError",
]);
serviceEnabled = stored.serviceEnabled === true;
nativeHostAvailable = stored.nativeHostAvailable === false ? false : true;
nativeHostError =
typeof stored.nativeHostError === "string" && stored.nativeHostError
? stored.nativeHostError
: null;
const refreshServiceState = async () => {
try {
const refreshedStatus = await chrome.runtime.sendMessage({
type: "SERVICE_STATE_REFRESH",
});
if (refreshedStatus?.ok) {
if (typeof refreshedStatus.enabled === "boolean") {
serviceEnabled = refreshedStatus.enabled;
}
if (typeof refreshedStatus.available === "boolean") {
nativeHostAvailable = refreshedStatus.available;
}
nativeHostError =
typeof refreshedStatus.error === "string" && refreshedStatus.error
? refreshedStatus.error
: null;
}
} catch {
// Best effort only.
}
};
// Force a fresh protection-state sync when popup opens.
await refreshServiceState();
// Refresh allowed domains from native config when popup opens.
try {
await chrome.runtime.sendMessage({ type: "DOMAINS_REFRESH" });
} catch {
// Best effort only.
}
await loadSiteDomainState();
render();
serviceRefreshTimer = setInterval(() => {
void (async () => {
await refreshServiceState();
await loadSiteDomainState();
render();
})();
}, 2000);
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== "local") return;
if (changes.serviceEnabled) {
serviceEnabled = changes.serviceEnabled.newValue === true;
}
if (changes.nativeHostAvailable) {
nativeHostAvailable =
changes.nativeHostAvailable.newValue === false ? false : true;
}
if (changes.nativeHostError) {
nativeHostError =
typeof changes.nativeHostError.newValue === "string" &&
changes.nativeHostError.newValue
? changes.nativeHostError.newValue
: null;
}
if (changes.allowedDomains || changes.temporaryEnabledDomains) {
void (async () => {
await loadSiteDomainState();
render();
})();
return;
}
render();
});
// Temporary override for unlisted site.
siteToggle.addEventListener("change", async () => {
if (
!currentHost ||
allowlisted ||
nativeHostAvailable === false ||
!hasConfiguredDomains
) {
siteToggle.checked = temporarilyEnabled;
return;
}
temporarilyEnabled = siteToggle.checked;
await setTemporaryOverride(currentHost, temporarilyEnabled);
if (tabId) {
chrome.tabs
.sendMessage(tabId, { type: "PII_SITE_SETTINGS_UPDATED" })
.catch(() => {});
}
await loadSiteDomainState();
render();
});
window.addEventListener("unload", () => {
if (serviceRefreshTimer) {
clearInterval(serviceRefreshTimer);
serviceRefreshTimer = null;
}
});
})();