Compare commits

..

2 commits

Author SHA1 Message Date
9eb0976b35 skip undefined emotes 2023-12-28 16:31:04 +00:00
5824f03253 working 2023-12-28 16:16:15 +00:00

View file

@ -2,13 +2,33 @@ const emote_regex = /bttv:([^\s]+)/g;
type Emote = { type Emote = {
code: string; 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<Emote> { async function bttv_get_url(code: string): Promise<Emote> {
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 { return {
code: code, 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<string>): Promise<EmoteMap> {
// queue emote jobs // queue emote jobs
const queue = [...codes_set].map((code) => bttv_get_url(code)); const queue = [...codes_set].map((code) => bttv_get_url(code));
console.log("[lmlfc-bttv] queueing", queue.length, "jobs");
// run // run
const result: EmoteMap = {}; const result: EmoteMap = {};
for (const job of await Promise.all(queue)) { for (const job of await Promise.all(queue)) {
result[job.code] = job.url; if (job.url) result[job.code] = job.url;
} }
return result; return result;
@ -36,14 +57,16 @@ async function process_text(text: string): Promise<string> {
for (const emote of matches.reverse()) { for (const emote of matches.reverse()) {
const [match, code] = emote; const [match, code] = emote;
const index = emote.index ?? 0; const index = emote.index ?? 0;
if (!(code in emotes)) continue;
text = text =
text.substring(0, index) + text.substring(0, index) +
emotes[code] + `[not]${emotes[code]}[/not]` +
text.substring(index + match.length); text.substring(index + match.length);
} }
console.log(text); text = text.replace(/\[\/not\]\s+\[not\]/g, "[/not][not]");
return text; return text;
} }
@ -64,10 +87,8 @@ async function process_text(text: string): Promise<string> {
const cb_input = document.querySelector("#mgc_cb_evo_input"); const cb_input = document.querySelector("#mgc_cb_evo_input");
if (!(cb_input instanceof HTMLInputElement)) return; if (!(cb_input instanceof HTMLInputElement)) return;
await process_text("bttv:foo bttv:bar bttv:foo"); cb_input.value = await process_text(cb_input.value);
cb_input.focus();
// await process_text(cb_input.value);
// cb_input.focus();
}); });
btn.appendChild(img); btn.appendChild(img);