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);