From 5824f03253bb0368e27ce8d39fa3698303fb35a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Thu, 28 Dec 2023 16:16:15 +0000 Subject: [PATCH] working --- src/main.ts | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/main.ts b/src/main.ts index 76ea31a..4545563 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,13 +2,33 @@ const emote_regex = /bttv:([^\s]+)/g; type Emote = { code: string; - url: string; + url?: string; +}; + +type BTTV_Emote = { + id: string; + code: string; + imageType: string; + animated: boolean; + user: any; }; async function bttv_get_url(code: string): Promise { + const res = await fetch( + `https://api.betterttv.net/3/emotes/shared/search?query=${code}&offset=0&limit=50`, + ); + const content = await res.json(); + + if (!Array.isArray(content)) return { code: code }; + const matches = (content as BTTV_Emote[]).filter( + (be) => be.code.toLowerCase() == code.toLowerCase(), + ); + + if (matches.length <= 0) return { code: code }; + return { code: code, - url: `result_${code}`, + url: `https://cdn.betterttv.net/emote/${matches[0].id}/1x.${matches[0].imageType}`, }; } @@ -19,11 +39,12 @@ async function get_emotes(codes: Iterable): Promise { // queue emote jobs const queue = [...codes_set].map((code) => bttv_get_url(code)); + console.log("[lmlfc-bttv] queueing", queue.length, "jobs"); // run const result: EmoteMap = {}; for (const job of await Promise.all(queue)) { - result[job.code] = job.url; + if (job.url) result[job.code] = job.url; } return result; @@ -39,11 +60,12 @@ async function process_text(text: string): Promise { text = text.substring(0, index) + - emotes[code] + + `[not]${emotes[code]}[/not]` + text.substring(index + match.length); } - console.log(text); + text = text.replace(/\[\/not\]\s+\[not\]/g, "[/not][not]"); + return text; } @@ -64,10 +86,8 @@ async function process_text(text: string): Promise { const cb_input = document.querySelector("#mgc_cb_evo_input"); if (!(cb_input instanceof HTMLInputElement)) return; - await process_text("bttv:foo bttv:bar bttv:foo"); - - // await process_text(cb_input.value); - // cb_input.focus(); + cb_input.value = await process_text(cb_input.value); + cb_input.focus(); }); btn.appendChild(img);