get_emotes impl

This commit is contained in:
Jörn-Michael Miehe 2023-12-28 15:37:38 +00:00
parent 70ccf22939
commit 87f2833a3a
2 changed files with 44 additions and 24 deletions

16
.vscode/tasks.json vendored
View file

@ -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"
}
]
}

View file

@ -1,12 +1,49 @@
const emote_regex = /bttv:([^\s]+)/g;
async function bttv_get_url(code: string): Promise<string> {
return "changed from ext";
type Emote = {
code: string;
url: string;
};
async function bttv_get_url(code: string): Promise<Emote> {
return {
code: code,
url: `result_${code}`,
};
}
type EmoteMap = { [code: string]: string };
async function get_emotes(codes: Iterable<string>): Promise<EmoteMap> {
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<string> {
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<string> {
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);