Compare commits

..

No commits in common. "49adf851b1358573d3ceb80114f055c826692cff" and "ffb5a3b4972da89844ab171631464c0084154a13" have entirely different histories.

5 changed files with 29 additions and 19 deletions

View file

@ -1,6 +1,5 @@
# lmlfc-bttv
- install deps: `yarn`
- more verbose but same: `yarn install --production=false`
- compile extension (into "dist" dir): `yarn build`
- pack "dist" dir (runs web-ext): `yarn build-ext`
- install deps `yarn install --production=false`
- make "dist" dir: `yarn build`
- pack "dist" dir: `yarn build-ext`

View file

@ -4,8 +4,8 @@
"author": "Jörn-Michael Miehe <joern-michael.miehe@lenaisten.de>",
"license": "MIT",
"scripts": {
"build-debug": "webpack --mode=development",
"build": "webpack --mode=production",
"build": "webpack --mode=development",
"build-release": "webpack --mode=production",
"lint-ext": "web-ext lint --source-dir=dist",
"build-ext": "web-ext build --source-dir=dist --artifacts-dir=dist --overwrite-dest"
},

View file

@ -13,9 +13,12 @@ type BTTV_Emote = {
* Query BTTV API for an emote graphic
*
* @param code string associated with the emote graphic (often called "emote" itself)
* @returns `null` iff no emote found or error, else URL to a BTTV emote graphic
* @returns `null` iff no emote found or error,
* else `[code, url]` tuple where `url` is the URL to a BTTV emote graphic
*/
export async function bttv_get_url(code: string): Promise<string | null> {
export async function bttv_get_url(
code: string,
): Promise<[string, string] | null> {
// search for emotes
const res = await fetch(
`https://api.betterttv.net/3/emotes/shared/search?query=${code}&offset=0&limit=50`,
@ -34,5 +37,8 @@ export async function bttv_get_url(code: string): Promise<string | null> {
if (matches.length <= 0) return null;
// return first emote's URL
return `//cdn.betterttv.net/emote/${matches[0].id}/1x.${matches[0].imageType}`;
return [
code,
`//cdn.betterttv.net/emote/${matches[0].id}/1x.${matches[0].imageType}`,
];
}

View file

@ -1,3 +1,8 @@
/**
* Async function mapping some `input` to an `[input, output]` tuple or a `null` if no output exists
*/
type MapFunction<In, Out> = (input: In) => Promise<[In, Out] | null>;
/**
* Concurrently create some mapping of input to output elements.
*
@ -7,13 +12,13 @@
*/
export async function map_concurrent<In, Out>(
inputs: Iterable<In>,
map_fn: (input: In) => Promise<Out | null>,
): Promise<Map<In, Out | null>> {
map_fn: MapFunction<In, Out>,
): Promise<Map<In, Out>> {
// apply map_fn to inputs
const outputs = await Promise.all([...inputs].map((input) => map_fn(input)));
const queue = [...inputs].map((input) => map_fn(input));
// create map object
return new Map<In, Out | null>(
[...inputs].map((input, index) => [input, outputs[index]]),
// wait for calls to finish
return new Map<In, Out>(
(await Promise.all(queue)).filter((job): job is [In, Out] => job !== null),
);
}

View file

@ -24,13 +24,13 @@ async function process_text(text: string): Promise<string> {
for (const emote of matches.reverse()) {
const [match, code] = emote;
// ensure emote exists
if (!emotes.has(code)) continue;
// emote exists
const url = emotes.get(code);
if (url == null) continue;
// ensure position in input string is known
if (emote.index == undefined) continue;
// position in input string is known
const index = emote.index;
if (index == undefined) continue;
text =
text.substring(0, index) +