-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.js
More file actions
94 lines (80 loc) · 2.14 KB
/
Copy pathsettings.js
File metadata and controls
94 lines (80 loc) · 2.14 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
// Domain-based settings for content scripts.
function normalizeDomain(value) {
if (typeof value !== "string") return null;
const domain = value.trim().toLowerCase().replace(/\.$/, "");
if (!domain) return null;
if (!/^[a-z0-9.-]+$/.test(domain)) return null;
if (domain.startsWith(".") || domain.endsWith(".") || domain.includes("..")) {
return null;
}
return domain;
}
function normalizeDomainList(values) {
const unique = new Set();
const normalized = [];
if (Array.isArray(values)) {
for (const value of values) {
const domain = normalizeDomain(value);
if (!domain || unique.has(domain)) continue;
unique.add(domain);
normalized.push(domain);
}
}
if (normalized.length > 0) return normalized;
if (!Array.isArray(values)) return [];
return [];
}
function hostMatchesDomain(hostname, domain) {
return hostname === domain || hostname.endsWith(`.${domain}`);
}
/**
* Get active settings for this page based on configured AI chat domains.
*/
async function getSiteSettings() {
const hostname = normalizeDomain(window.location.hostname);
if (!hostname) {
return {
enabled: false,
host: null,
source: "unsupported",
};
}
const { allowedDomains, temporaryEnabledDomains } = await chrome.storage.local.get([
"allowedDomains",
"temporaryEnabledDomains",
]);
const allowed = normalizeDomainList(allowedDomains);
const temporary = normalizeDomainList(temporaryEnabledDomains);
if (allowed.length === 0) {
return {
enabled: false,
host: hostname,
source: "no-domains",
matchedDomain: null,
};
}
const matchedDomain = allowed.find((domain) => hostMatchesDomain(hostname, domain)) || null;
if (matchedDomain) {
return {
enabled: true,
host: hostname,
source: "allowlist",
matchedDomain,
};
}
const temporaryEnabled = temporary.includes(hostname);
if (temporaryEnabled) {
return {
enabled: true,
host: hostname,
source: "temporary",
matchedDomain: null,
};
}
return {
enabled: false,
host: hostname,
source: "blocked",
matchedDomain: null,
};
}