forked from zokuzoku/cat-gatekeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.js
More file actions
145 lines (119 loc) · 3.48 KB
/
Copy pathshared.js
File metadata and controls
145 lines (119 loc) · 3.48 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
(function attachShared(root, factory) {
const shared = factory();
if (typeof module !== 'undefined' && module.exports) {
module.exports = shared;
}
root.CatGatekeeperShared = shared;
})(typeof globalThis !== 'undefined' ? globalThis : this, () => {
const DEFAULT_DOMAINS = Object.freeze([
'x.com',
'youtube.com',
'facebook.com',
'reddit.com',
'threads.net',
'bsky.app',
]);
const DEFAULT_SETTINGS = Object.freeze({
usageLimit: 60,
breakTime: 5,
customDomains: DEFAULT_DOMAINS,
});
function clampNumber(value, min, max, fallback) {
const parsedValue = Number.parseInt(value, 10);
if (Number.isNaN(parsedValue)) {
return fallback;
}
return Math.min(Math.max(parsedValue, min), max);
}
function normalizeDomainEntry(entry) {
if (typeof entry !== 'string') {
return '';
}
let value = entry.trim().toLowerCase();
if (!value) {
return '';
}
value = value.replace(/^[*.]+/, '');
try {
const url = new URL(value.includes('://') ? value : `https://${value}`);
value = url.hostname.toLowerCase();
} catch (_error) {
value = value.split(/[/?#]/, 1)[0].trim().toLowerCase();
value = value.replace(/:\d+$/, '');
}
value = value.replace(/^[*.]+/, '');
value = value.replace(/^www\./, '');
value = value.replace(/\.+$/, '');
if (!value || !value.includes('.') || !/^[a-z0-9.-]+$/.test(value)) {
return '';
}
return value;
}
function normalizeDomainList(domains) {
const inputList = Array.isArray(domains)
? domains
: typeof domains === 'string'
? domains.split(/[\n,]+/)
: [];
const normalizedDomains = [];
const seenDomains = new Set();
inputList.forEach((domain) => {
const normalizedDomain = normalizeDomainEntry(domain);
if (!normalizedDomain || seenDomains.has(normalizedDomain)) {
return;
}
seenDomains.add(normalizedDomain);
normalizedDomains.push(normalizedDomain);
});
return normalizedDomains;
}
function hostnameMatchesDomain(hostname, domain) {
const normalizedHostname = normalizeDomainEntry(hostname);
const normalizedDomain = normalizeDomainEntry(domain);
if (!normalizedHostname || !normalizedDomain) {
return false;
}
return normalizedHostname === normalizedDomain ||
normalizedHostname.endsWith(`.${normalizedDomain}`);
}
function isCustomDomain(hostname, customDomains) {
return normalizeDomainList(customDomains).some((domain) =>
hostnameMatchesDomain(hostname, domain)
);
}
function normalizeSettings(settings) {
const safeSettings = settings && typeof settings === 'object' ? settings : {};
const customDomains = normalizeDomainList(safeSettings.customDomains);
const hasCustomDomainsSetting = Object.prototype.hasOwnProperty.call(
safeSettings,
'customDomains'
);
return {
usageLimit: clampNumber(
safeSettings.usageLimit,
1,
480,
DEFAULT_SETTINGS.usageLimit
),
breakTime: clampNumber(
safeSettings.breakTime,
1,
60,
DEFAULT_SETTINGS.breakTime
),
customDomains: hasCustomDomainsSetting
? customDomains
: [...DEFAULT_SETTINGS.customDomains],
};
}
return {
DEFAULT_SETTINGS,
DEFAULT_DOMAINS,
clampNumber,
hostnameMatchesDomain,
isCustomDomain,
normalizeDomainEntry,
normalizeDomainList,
normalizeSettings,
};
});