-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoverlay.js
More file actions
411 lines (359 loc) · 12.8 KB
/
overlay.js
File metadata and controls
411 lines (359 loc) · 12.8 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// ==== KONFIGURATION ==== (aus config.js laden)
const TWITCH_CHANNEL = CONFIG.TWITCH_CHANNEL;
const TWITCH_OAUTH = CONFIG.TWITCH_OAUTH;
const KICK_APP_KEY = CONFIG.KICK_APP_KEY;
const KICK_CLUSTER = CONFIG.KICK_CLUSTER;
const KICK_CHATROOM_ID = CONFIG.KICK_CHATROOM_ID;
const SEVENTV_USER_ID = CONFIG.SEVENTV_USER_ID;
const BTTV_TWITCH_USER_ID = CONFIG.BTTV_TWITCH_USER_ID;
// ====== Anzeige ======
const MAX_MESSAGES = 20;
// ==== State ====
const chatBox = document.getElementById("chat");
let emoteMap = {};
let twitchEmoteCache = {};
// ===== 7TV laden =====
async function load7TVUser() {
try {
const res = await fetch(`https://7tv.io/v3/users/${SEVENTV_USER_ID}`);
const data = await res.json();
if (data?.connections?.[0]?.emote_set?.emotes) {
data.connections[0].emote_set.emotes.forEach(e => {
emoteMap[e.name] = `https:${e.data.host.url}/4x.webp`;
});
console.log("[7TV] User-Set geladen:", data.connections[0].emote_set.emotes.length);
}
} catch (err) {
console.warn("[7TV] Fehler User-Set:", err);
}
}
async function load7TVGlobal() {
try {
const res = await fetch("https://7tv.io/v3/emote-sets/global");
const data = await res.json();
if (data?.emotes) {
data.emotes.forEach(e => {
emoteMap[e.name] = `https:${e.data.host.url}/4x.webp`;
});
console.log("[7TV] Globale Emotes geladen:", data.emotes.length);
}
} catch (err) {
console.warn("[7TV] Fehler Global:", err);
}
}
// ===== BTTV laden =====
async function loadBTTVGlobal() {
try {
const res = await fetch("https://api.betterttv.net/3/cached/emotes/global");
const data = await res.json();
data.forEach(e => {
emoteMap[e.code] = `https://cdn.betterttv.net/emote/${e.id}/3x`;
});
console.log("[BTTV] Globale Emotes geladen:", data.length);
} catch (err) {
console.warn("[BTTV] Fehler Global:", err);
}
}
async function loadBTTVChannel(twitchUserId) {
try {
const res = await fetch(`https://api.betterttv.net/3/cached/users/twitch/${twitchUserId}`);
const data = await res.json();
(data.channelEmotes || []).forEach(e => {
emoteMap[e.code] = `https://cdn.betterttv.net/emote/${e.id}/3x`;
});
(data.sharedEmotes || []).forEach(e => {
emoteMap[e.code] = `https://cdn.betterttv.net/emote/${e.id}/3x`;
});
console.log("[BTTV] Channel-Emotes geladen");
} catch (err) {
console.warn("[BTTV] Fehler Channel:", err);
}
}
// ===== FFZ laden =====
function normalizeFfzUrl(url) {
return url.startsWith("//") ? `https:${url}` : url;
}
async function loadFFZChannel(channelName) {
try {
const res = await fetch(`https://api.frankerfacez.com/v1/room/${channelName}`);
const data = await res.json();
if (data.sets) {
let count = 0;
for (const set of Object.values(data.sets)) {
set.emoticons.forEach(e => {
const urls = e.urls || {};
const url = urls["4"] || urls["2"] || urls["1"];
if (url) {
emoteMap[e.name] = normalizeFfzUrl(url);
count++;
}
});
}
console.log(`[FFZ] Channel-Emotes geladen: ${count}`);
}
} catch (err) {
console.warn("[FFZ] Fehler Channel:", err);
}
}
async function loadFFZGlobal() {
try {
const res = await fetch("https://api.frankerfacez.com/v1/set/global");
const data = await res.json();
if (data.sets) {
let count = 0;
for (const set of Object.values(data.sets)) {
set.emoticons.forEach(e => {
const urls = e.urls || {};
const url = urls["4"] || urls["2"] || urls["1"];
if (url) {
emoteMap[e.name] = normalizeFfzUrl(url);
count++;
}
});
}
console.log(`[FFZ] Globale Emotes geladen: ${count}`);
}
} catch (err) {
console.warn("[FFZ] Fehler Global:", err);
}
}
// ===== Text/Emotes =====
function renderTextWithTwitch(text, twitchEmotes) {
if (!text) return "";
let output = "";
let lastIndex = 0;
if (twitchEmotes && Object.keys(twitchEmotes).length > 0) {
const positions = [];
for (const emoteId in twitchEmotes) {
twitchEmotes[emoteId].forEach(pos => {
const [start, end] = pos.split("-").map(Number);
positions.push({ emoteId, start, end });
});
}
positions.sort((a, b) => a.start - b.start);
positions.forEach(({ emoteId, start, end }) => {
if (lastIndex < start) {
output += renderText7TV_BTTV(text.substring(lastIndex, start));
}
const emoteUrl = getTwitchEmoteUrl(emoteId);
output += `<img class="emote" src="${emoteUrl}" alt="emote">`;
lastIndex = end + 1;
});
if (lastIndex < text.length) {
output += renderText7TV_BTTV(text.substring(lastIndex));
}
return output;
}
return renderText7TV_BTTV(text);
}
function renderText7TV_BTTV(text) {
if (!text) return "";
const parts = text.split(/\s+/);
return parts.map(part => {
if (emoteMap.hasOwnProperty(part)) {
return `<img class="emote" src="${emoteMap[part]}" alt="${part}">`;
}
return escapeHtml(part);
}).join(" ");
}
function getTwitchEmoteUrl(id) {
if (!twitchEmoteCache[id]) {
twitchEmoteCache[id] = `https://static-cdn.jtvnw.net/emoticons/v2/${id}/default/dark/3.0`;
}
return twitchEmoteCache[id];
}
function escapeHtml(str) {
return str.replace(/[&<>"']/g, s =>
({'&':'&','<':'<','>':'>','"':'"',"'":'''}[s])
);
}
function addMessage(user, text, platform, twitchEmotes = null, nameColor = "#fff", badgesHtml = "", msgId = null) {
// --- Filter: blockierte User ---
if (CONFIG.BLOCKED_USERS && CONFIG.BLOCKED_USERS.map(u => u.toLowerCase()).includes(user.toLowerCase())) {
return;
}
// --- Filter: Commands blockieren ---
// 1) Alle Nachrichten blocken, die mit "!" beginnen
if (CONFIG.BLOCK_ALL_PREFIX_COMMANDS && text && text.trim().startsWith("!")) {
return;
}
// 2) Einzelne Commands blockieren
if (CONFIG.BLOCKED_COMMANDS && text) {
const lowered = text.toLowerCase().trim();
for (const cmd of CONFIG.BLOCKED_COMMANDS) {
if (lowered.startsWith(cmd.toLowerCase())) {
return;
}
}
}
// ✅ --- Filter: Links blockieren ---
if (CONFIG.BLOCK_LINKS && text && /(https?:\/\/|www\.)/i.test(text)) {
return;
}
// ---------------------------------------
const el = document.createElement("div");
el.className = "message";
let icon = platform === "Twitch" ? "twitch_icon.png" : "kick_icon.png";
if (msgId) {
el.dataset.msgId = msgId;
el.dataset.user = user.toLowerCase();
}
let renderedText;
if (platform === "Twitch") {
renderedText = renderTextWithTwitch(text, twitchEmotes);
} else if (platform === "Kick") {
renderedText = text;
}
el.innerHTML =
`<img class="platform-icon" src="img/${icon}">
${CONFIG.SHOW_BADGES ? badgesHtml : ""}
<span class="username" style="color:${nameColor}">${escapeHtml(user)}:</span>
${renderedText}`;
chatBox.appendChild(el);
chatBox.scrollTop = chatBox.scrollHeight;
while (chatBox.childNodes.length > MAX_MESSAGES) {
chatBox.removeChild(chatBox.firstChild);
}
}
// ===== Twitch verbinden =====
function connectTwitch() {
const ws = new WebSocket("wss://irc-ws.chat.twitch.tv:443");
ws.onopen = () => {
ws.send("CAP REQ :twitch.tv/tags twitch.tv/commands twitch.tv/membership");
ws.send(`PASS ${TWITCH_OAUTH}`);
ws.send(`NICK ${TWITCH_CHANNEL}`);
ws.send(`JOIN #${TWITCH_CHANNEL}`);
console.log("[Twitch] Verbunden");
};
ws.onmessage = (event) => {
const lines = event.data.trim().split("\r\n");
for (const msg of lines) {
// PING
if (msg.startsWith("PING")) {
ws.send("PONG :tmi.twitch.tv");
continue;
}
// CLEARMSG (einzelne Nachricht gelöscht)
if (msg.includes("CLEARMSG")) {
const tagsRaw = msg.startsWith("@") ? msg.split(" ")[0] : "";
const tags = {};
if (tagsRaw) {
tagsRaw.substring(1).split(";").forEach(t => {
const [k, v] = t.split("=");
tags[k] = v ?? "";
});
}
const targetMsgId = tags["target-msg-id"];
if (targetMsgId) {
const el = document.querySelector(`[data-msg-id="${targetMsgId}"]`);
if (el) el.remove();
}
continue;
}
// CLEARCHAT (Ban / Timeout / /clear)
if (msg.includes("CLEARCHAT")) {
const parts = msg.split("CLEARCHAT")[1];
const username = parts?.split(" :")[1]?.trim();
if (username) {
document.querySelectorAll(`[data-user="${username.toLowerCase()}"]`)
.forEach(el => el.remove());
} else {
chatBox.innerHTML = "";
}
continue;
}
// PRIVMSG (normale Nachricht)
if (msg.includes("PRIVMSG")) {
const tagsRaw = msg.startsWith("@") ? msg.split(" ")[0] : "";
const tags = {};
if (tagsRaw) {
tagsRaw.substring(1).split(";").forEach(t => {
const [k, v] = t.split("=");
tags[k] = v ?? "";
});
}
const user = tags["display-name"] || msg.split("!")[0].substring(1);
const text = msg.split("PRIVMSG")[1].split(" :")[1] ?? "";
const msgId = tags["id"] || null;
let twitchEmotes = null;
if (tags["emotes"]) {
twitchEmotes = {};
tags["emotes"].split("/").forEach(e => {
const [id, positions] = e.split(":");
twitchEmotes[id] = positions.split(",");
});
}
let badgesHtml = "";
if (CONFIG.SHOW_BADGES && tags["badges"]) {
tags["badges"].split(",").forEach(b => {
const [type] = b.split("/");
if (type === "lead_moderator") {
badgesHtml += `<img class="badge" src="img/twitch_lead_mod.png" style="height:18px;margin-right:4px">`;
} else if (type === "moderator") {
badgesHtml += `<img class="badge" src="img/twitch_mod.png" style="height:18px;margin-right:4px">`;
} else if (type === "vip") {
badgesHtml += `<img class="badge" src="img/twitch_vip.png" style="height:18px;margin-right:4px">`;
}
});
}
addMessage(user, text, "Twitch", twitchEmotes, tags["color"] || "#fff", badgesHtml, msgId);
}
}
};
}
// ===== Kick verbinden =====
function connectKick() {
const wsUrl = `wss://ws-${KICK_CLUSTER}.pusher.com/app/${KICK_APP_KEY}?protocol=7&client=js&version=8.4.0&flash=false`;
const ws = new WebSocket(wsUrl);
ws.onopen = () => {
console.log("[Kick] Verbunden (Pusher)");
const subMsg = {
event: "pusher:subscribe",
data: { channel: `chatrooms.${KICK_CHATROOM_ID}.v2` }
};
ws.send(JSON.stringify(subMsg));
};
ws.onmessage = (evt) => {
try {
const payload = JSON.parse(evt.data);
if (payload.event === "App\\Events\\ChatMessageEvent") {
const inner = JSON.parse(payload.data);
const user = inner.sender?.username || "unknown";
let text = inner.content || "";
const color = inner.sender?.identity?.color || "#fff";
// Kick-Emotes
text = text.replace(/\[emote:(\d+):([^\]]+)\]/g, (m, id, name) => {
const url = `https://files.kick.com/emotes/${id}/fullsize`;
return `<img class="emote" src="${url}" alt="${name}">`;
});
// Kick-Badges
let badgesHtml = "";
if (CONFIG.SHOW_BADGES) {
(inner.sender.identity?.badges || []).forEach(b => {
if (b.type === "moderator") {
badgesHtml += `<img class="badge" src="img/kick_mod.svg" alt="mod" style="height:18px;vertical-align:middle;margin-right:4px">`;
} else if (b.type === "vip") {
badgesHtml += `<img class="badge" src="img/kick_vip.png" alt="vip" style="height:18px;vertical-align:middle;margin-right:4px">`;
}
});
}
addMessage(user, text, "Kick", null, color, badgesHtml);
}
} catch (e) {
console.error("[Kick] Parse-Fehler:", e, evt.data);
}
};
ws.onerror = (err) => console.error("[Kick] Fehler:", err);
ws.onclose = () => console.warn("[Kick] Verbindung geschlossen");
}
// ===== Start =====
(async () => {
await load7TVUser();
await load7TVGlobal();
await loadBTTVGlobal();
await loadBTTVChannel(BTTV_TWITCH_USER_ID);
await loadFFZGlobal();
await loadFFZChannel(CONFIG.FFZ_CHANNEL);
connectTwitch();
connectKick();
})();