-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCensorCore.js
More file actions
170 lines (142 loc) · 4.07 KB
/
CensorCore.js
File metadata and controls
170 lines (142 loc) · 4.07 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
// CensorCore.js
// Copyright (c) 2025 Derrick Richard
// Lightweight, zero-setup message filtering library.
// Source: https://github.com/DerrickRichard/CensorCore-Library
// Licensed under the MIT License
// Author: Derrick Richard (https://derrickrichard.github.io/profile/)
// Weekly programming articles: https://dev.to/derrickrichard
// v2.0 — Adds severity levels, phrase detection, custom rules, async events, and rich analyze() API.
(function () {
let rules = [];
let ready = false;
let loadFailed = false;
const readyCallbacks = [];
const errorCallbacks = [];
// Default severity mapping for categories
const CATEGORY_SEVERITY = {
profanity: "medium",
hate_speech: "high",
harassment: "medium",
sexual_content: "high",
violence: "high",
self_harm: "high",
drugs: "medium",
weapons: "high",
extremism: "high",
terrorism: "high",
disallowed_phrases: "medium",
custom: "low",
default: "low"
};
// Public API
const censor = {
isBlocked(text) {
return this.analyze(text).blocked;
},
analyze(text) {
if (!ready || !text) {
return { blocked: false, matches: [] };
}
const normalized = normalizeText(text);
const matches = [];
for (const rule of rules) {
if (rule.pattern.test(normalized)) {
matches.push({
text: rule.text,
category: rule.category,
severity: rule.severity
});
}
}
if (matches.length === 0) {
return { blocked: false, matches: [] };
}
const severityOrder = { low: 1, medium: 2, high: 3 };
const highest = matches.reduce((a, b) =>
(severityOrder[b.severity] || 1) > (severityOrder[a.severity] || 1)
? b
: a
);
return {
blocked: true,
severity: highest.severity,
category: highest.category,
matches
};
},
extend(customRules) {
if (!Array.isArray(customRules)) return;
for (const r of customRules) {
if (!r || !r.text) continue;
const text = String(r.text).toLowerCase();
const category = r.category || "custom";
const severity = r.severity || CATEGORY_SEVERITY[category] || "low";
rules.push({
text,
category,
severity,
pattern: buildPattern(text)
});
}
},
isReady() {
return ready;
},
isFailed() {
return loadFailed;
},
onReady(callback) {
if (typeof callback !== "function") return;
if (ready) callback();
else readyCallbacks.push(callback);
},
onError(callback) {
if (typeof callback !== "function") return;
if (loadFailed) callback();
else errorCallbacks.push(callback);
}
};
window.censor = Object.freeze(censor);
// Load JSON wordlist
fetch("https://cdn.jsdelivr.net/gh/DerrickRichard/CensorCore-Library@main/wordlist.json")
.then(res => res.json())
.then(data => {
const newRules = [];
for (const [category, list] of Object.entries(data || {})) {
const severity =
CATEGORY_SEVERITY[category] || CATEGORY_SEVERITY.default;
if (!Array.isArray(list)) continue;
for (const entry of list) {
if (!entry) continue;
const text = String(entry).toLowerCase();
newRules.push({
text,
category,
severity,
pattern: buildPattern(text)
});
}
}
rules = newRules;
ready = true;
readyCallbacks.forEach(cb => cb());
})
.catch(err => {
loadFailed = true;
errorCallbacks.forEach(cb => cb(err));
console.error("CensorCore: Could not load wordlist.json", err);
});
function normalizeText(text) {
return String(text)
.trim()
.toLowerCase()
.normalize("NFKC");
}
function buildPattern(text) {
const escaped = escapeRegex(text);
return new RegExp("\\b" + escaped + "\\b", "i");
}
function escapeRegex(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
})();