-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup-settings.js
More file actions
128 lines (106 loc) · 3.03 KB
/
Copy pathpopup-settings.js
File metadata and controls
128 lines (106 loc) · 3.03 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
// Popup settings and domain management helpers.
export function normalizeDomain(value) {
if (typeof value !== "string") return null;
let domain = value.trim().toLowerCase();
if (!domain) return null;
if (domain.startsWith("https://")) {
domain = domain.slice("https://".length);
} else if (domain.startsWith("http://")) {
domain = domain.slice("http://".length);
}
if (domain.includes("/")) {
domain = domain.split("/")[0];
}
if (domain.includes(":")) {
domain = domain.split(":")[0];
}
domain = domain.replace(/\.$/, "");
if (!domain) return null;
if (!/^[a-z0-9.-]+$/.test(domain)) return null;
if (domain.startsWith(".") || domain.endsWith(".") || domain.includes("..")) {
return null;
}
return domain;
}
export function normalizeDomainList(values) {
const seen = new Set();
const normalized = [];
if (Array.isArray(values)) {
for (const value of values) {
const domain = normalizeDomain(value);
if (!domain || seen.has(domain)) continue;
seen.add(domain);
normalized.push(domain);
}
}
if (normalized.length > 0) return normalized;
if (!Array.isArray(values)) return [];
return [];
}
/**
* Extract hostname from URL.
*/
export function getHostnameFromUrl(url) {
try {
const hostname = new URL(url).hostname;
return normalizeDomain(hostname);
} catch {
return null;
}
}
/**
* Return true when hostname is exactly a domain or its subdomain.
*/
export function hostMatchesDomain(hostname, domain) {
return hostname === domain || hostname.endsWith(`.${domain}`);
}
/**
* Get the active tab.
*/
export async function getActiveTab() {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
return tab;
}
/**
* Load domain-related settings from extension local storage.
*/
export async function loadDomainSettings() {
const { allowedDomains, temporaryEnabledDomains } =
await chrome.storage.local.get([
"allowedDomains",
"temporaryEnabledDomains",
]);
return {
allowedDomains: normalizeDomainList(allowedDomains),
temporaryEnabledDomains: normalizeDomainList(temporaryEnabledDomains),
};
}
/**
* True if host is in allowed domain list (including subdomains).
*/
export function isHostAllowlisted(hostname, allowedDomains) {
return allowedDomains.some((domain) => hostMatchesDomain(hostname, domain));
}
/**
* True if host has a temporary manual override.
*/
export function isHostTemporarilyEnabled(hostname, temporaryEnabledDomains) {
return temporaryEnabledDomains.includes(hostname);
}
/**
* Update temporary override for a host.
*/
export async function setTemporaryOverride(hostname, enabled) {
const normalizedHost = normalizeDomain(hostname);
if (!normalizedHost) return;
const { temporaryEnabledDomains } = await loadDomainSettings();
const next = new Set(temporaryEnabledDomains);
if (enabled) {
next.add(normalizedHost);
} else {
next.delete(normalizedHost);
}
await chrome.storage.local.set({
temporaryEnabledDomains: Array.from(next).sort(),
});
}