From 87f2833a3a8fac2a84d4c5512953e6325e135a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Thu, 28 Dec 2023 15:37:38 +0000 Subject: [PATCH] get_emotes impl --- .vscode/tasks.json | 16 -------------- src/main.ts | 52 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 24 deletions(-) delete mode 100644 .vscode/tasks.json diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 58eb4fc..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "type": "npm", - "script": "build", - "group": { - "kind": "build", - "isDefault": true - }, - "problemMatcher": [], - "label": "npm: build", - "detail": "webpack --config webpack.config.js" - } - ] -} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index a1c026d..76ea31a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,49 @@ const emote_regex = /bttv:([^\s]+)/g; -async function bttv_get_url(code: string): Promise { - return "changed from ext"; +type Emote = { + code: string; + url: string; +}; + +async function bttv_get_url(code: string): Promise { + return { + code: code, + url: `result_${code}`, + }; +} + +type EmoteMap = { [code: string]: string }; + +async function get_emotes(codes: Iterable): Promise { + const codes_set = new Set(codes); + + // queue emote jobs + const queue = [...codes_set].map((code) => bttv_get_url(code)); + + // run + const result: EmoteMap = {}; + for (const job of await Promise.all(queue)) { + result[job.code] = job.url; + } + + return result; } async function process_text(text: string): Promise { - const emotes = text.matchAll(emote_regex); + const matches = [...text.matchAll(emote_regex)]; + const emotes = await get_emotes(matches.map((elem) => elem[1])); + for (const emote of matches.reverse()) { + const [match, code] = emote; + const index = emote.index ?? 0; + + text = + text.substring(0, index) + + emotes[code] + + text.substring(index + match.length); + } + + console.log(text); return text; } @@ -21,17 +58,16 @@ async function process_text(text: string): Promise { const img = document.createElement("img"); img.setAttribute("src", chrome.runtime.getURL("bttv.png")); - img.style.setProperty("max-height", "25px"); - img.style.setProperty("max-width", "25px"); img.style.setProperty("vertical-align", "middle"); btn.addEventListener("click", async (event) => { const cb_input = document.querySelector("#mgc_cb_evo_input"); if (!(cb_input instanceof HTMLInputElement)) return; - cb_input.focus(); - cb_input.value += await bttv_get_url("foo bar"); - cb_input.focus(); + await process_text("bttv:foo bttv:bar bttv:foo"); + + // await process_text(cb_input.value); + // cb_input.focus(); }); btn.appendChild(img);