Compare commits
No commits in common. "ba0fa94eff5fee2b550a2a52848158bb8fdb4631" and "fd04f4024f98dc57224d0bf7ce59375b92351fbc" have entirely different histories.
ba0fa94eff
...
fd04f4024f
10 changed files with 84 additions and 1288 deletions
9
LICENSE
9
LICENSE
|
@ -1,9 +0,0 @@
|
||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2023 Lenaisten.de
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
@ -1,6 +1,5 @@
|
||||||
# lmlfc-bttv
|
# lmlfc-bttv
|
||||||
|
|
||||||
- install deps: `yarn`
|
- install deps `yarn install --production=false`
|
||||||
- more verbose but same: `yarn install --production=false`
|
- make "dist" dir: `yarn build`
|
||||||
- compile extension (into "dist" dir): `yarn build`
|
- pack "dist" dir: `yarn build-ext`
|
||||||
- pack "dist" dir (runs web-ext): `yarn build-ext`
|
|
||||||
|
|
|
@ -1,19 +1,15 @@
|
||||||
{
|
{
|
||||||
"name": "lmlfc-bttv",
|
"name": "lmlfc-bttv",
|
||||||
"version": "0.2.0",
|
"version": "0.1",
|
||||||
"author": "Jörn-Michael Miehe <joern-michael.miehe@lenaisten.de>",
|
"author": "Jörn-Michael Miehe <joern-michael.miehe@lenaisten.de>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build-debug": "webpack --mode=development",
|
"build": "webpack --config webpack.config.js",
|
||||||
"build": "webpack --mode=production",
|
|
||||||
"lint-ext": "web-ext lint --source-dir=dist",
|
"lint-ext": "web-ext lint --source-dir=dist",
|
||||||
"build-ext": "web-ext build --source-dir=dist --artifacts-dir=dist --overwrite-dest"
|
"build-ext": "web-ext build --source-dir=dist --artifacts-dir=dist --overwrite-dest"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/core": "^7.23.7",
|
|
||||||
"@babel/preset-env": "^7.23.7",
|
|
||||||
"@types/chrome": "^0.0.254",
|
"@types/chrome": "^0.0.254",
|
||||||
"babel-loader": "^9.1.3",
|
|
||||||
"copy-webpack-plugin": "^11.0.0",
|
"copy-webpack-plugin": "^11.0.0",
|
||||||
"prettier": "^3.1.1",
|
"prettier": "^3.1.1",
|
||||||
"ts-loader": "^9.5.1",
|
"ts-loader": "^9.5.1",
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
"manifest_version": 3,
|
"manifest_version": 3,
|
||||||
"name": "BTTV für Schnatzbox",
|
"name": "BTTV für Schnatzbox",
|
||||||
"description": "Macht \"bttv:kekw\" und co. in der \"Schnatzbox\" verfügbar.",
|
"description": "Macht \"bttv:kekw\" und co. in der \"Schnatzbox\" verfügbar.",
|
||||||
"version": "0.2.0",
|
"version": "0.1",
|
||||||
"icons": {
|
"icons": {
|
||||||
"16": "img/icon-16.png",
|
"16": "img/icon-16.png",
|
||||||
"32": "img/icon-32.png",
|
"32": "img/icon-32.png",
|
||||||
|
@ -17,7 +17,7 @@
|
||||||
],
|
],
|
||||||
"web_accessible_resources": [
|
"web_accessible_resources": [
|
||||||
{
|
{
|
||||||
"resources": ["main.js.map", "img/sb_button.png"],
|
"resources": ["img/sb_button.png"],
|
||||||
"matches": ["*://*.lenameyerlandrut-fanclub.de/*"]
|
"matches": ["*://*.lenameyerlandrut-fanclub.de/*"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,38 +0,0 @@
|
||||||
/**
|
|
||||||
* An "Emote" as returned by the BTTV API
|
|
||||||
*/
|
|
||||||
type BTTV_Emote = {
|
|
||||||
id: string;
|
|
||||||
code: string;
|
|
||||||
imageType: string;
|
|
||||||
animated: boolean;
|
|
||||||
user: any;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
export async function bttv_get_url(code: string): Promise<string | null> {
|
|
||||||
// search for emotes
|
|
||||||
const res = await fetch(
|
|
||||||
`https://api.betterttv.net/3/emotes/shared/search?query=${code}&offset=0&limit=50`,
|
|
||||||
);
|
|
||||||
const content = await res.json();
|
|
||||||
|
|
||||||
// sanity check
|
|
||||||
if (!Array.isArray(content)) return null;
|
|
||||||
|
|
||||||
// find exact matches (disregard case)
|
|
||||||
const matches = (content as BTTV_Emote[]).filter(
|
|
||||||
(be) => be.code.toLowerCase() == code.toLowerCase(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// ensure there's emotes left
|
|
||||||
if (matches.length <= 0) return null;
|
|
||||||
|
|
||||||
// return first emote's URL
|
|
||||||
return `//cdn.betterttv.net/emote/${matches[0].id}/1x.${matches[0].imageType}`;
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
/**
|
|
||||||
* Concurrently create some mapping of input to output elements.
|
|
||||||
*
|
|
||||||
* @param inputs collection of input elements
|
|
||||||
* @param map_fn function mapping inputs to outputs
|
|
||||||
* @returns ES6 Map object with inputs mapped to outputs where possible
|
|
||||||
*/
|
|
||||||
export async function map_concurrent<In, Out>(
|
|
||||||
inputs: Iterable<In>,
|
|
||||||
map_fn: (input: In) => Promise<Out | null>,
|
|
||||||
): Promise<Map<In, Out | null>> {
|
|
||||||
// apply map_fn to inputs
|
|
||||||
const outputs = await Promise.all([...inputs].map((input) => map_fn(input)));
|
|
||||||
|
|
||||||
// create map object
|
|
||||||
return new Map<In, Out | null>(
|
|
||||||
[...inputs].map((input, index) => [input, outputs[index]]),
|
|
||||||
);
|
|
||||||
}
|
|
104
src/main.ts
104
src/main.ts
|
@ -1,62 +1,84 @@
|
||||||
import { bttv_get_url } from "./bttv_api";
|
|
||||||
import { map_concurrent } from "./helpers";
|
|
||||||
|
|
||||||
const emote_regex = /bttv:([^\s]+)/gi;
|
const emote_regex = /bttv:([^\s]+)/gi;
|
||||||
|
|
||||||
/**
|
type Emote = {
|
||||||
* Process "bttv:*" emotes in a string
|
code: string;
|
||||||
*
|
url?: string;
|
||||||
* @param text the input string
|
};
|
||||||
* @returns new string with existing "bttv:*" emotes replaced by "[not]" BBCodes
|
|
||||||
*/
|
type BTTV_Emote = {
|
||||||
|
id: string;
|
||||||
|
code: string;
|
||||||
|
imageType: string;
|
||||||
|
animated: boolean;
|
||||||
|
user: any;
|
||||||
|
};
|
||||||
|
|
||||||
|
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 {
|
||||||
|
code: code,
|
||||||
|
url: `//cdn.betterttv.net/emote/${matches[0].id}/1x.${matches[0].imageType}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
console.log("[lmlfc-bttv] queued", queue.length, "jobs");
|
||||||
|
|
||||||
|
// run
|
||||||
|
const result: EmoteMap = {};
|
||||||
|
for (const job of await Promise.all(queue)) {
|
||||||
|
if (job.url) result[job.code] = job.url;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
async function process_text(text: string): Promise<string> {
|
async function process_text(text: string): Promise<string> {
|
||||||
// find "bttv:*" emotes
|
|
||||||
const matches = [...text.matchAll(emote_regex)];
|
const matches = [...text.matchAll(emote_regex)];
|
||||||
|
const emotes = await get_emotes(matches.map(([_, code]) => code));
|
||||||
|
|
||||||
// unique emote codes (the "*" parts)
|
|
||||||
const codes = new Set(matches.map(([_, code]) => code));
|
|
||||||
|
|
||||||
// find emote pics using BTTV API
|
|
||||||
console.log("[lmlfc-bttv]", `searching ${[...codes].length} emotes`);
|
|
||||||
const emotes = await map_concurrent(codes, bttv_get_url);
|
|
||||||
|
|
||||||
// replace emotes with BBCodes
|
|
||||||
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;
|
||||||
// ensure emote exists
|
if (!(code in emotes)) continue;
|
||||||
const url = emotes.get(code);
|
|
||||||
if (url == null) continue;
|
|
||||||
|
|
||||||
// ensure position in input string is known
|
|
||||||
const index = emote.index;
|
|
||||||
if (index == undefined) continue;
|
|
||||||
|
|
||||||
text =
|
text =
|
||||||
text.substring(0, index) +
|
text.substring(0, index) +
|
||||||
`[not]${url}[/not]` +
|
`[not]${emotes[code]}[/not]` +
|
||||||
text.substring(index + match.length);
|
text.substring(index + match.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove space between adjacent emotes
|
text = text.replace(/\[\/not\]\s+\[not\]/g, "[/not][not]");
|
||||||
// (maybe do this in emote_regex instead?)
|
|
||||||
return text.replace(/\[\/not\]\s+\[not\]/g, "[/not][not]");
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
(() => {
|
(() => {
|
||||||
// find chatbox elements
|
|
||||||
const cb_form = document.querySelector("#mgc_cb_evo_form");
|
const cb_form = document.querySelector("#mgc_cb_evo_form");
|
||||||
|
if (!(cb_form instanceof HTMLFormElement)) return;
|
||||||
|
|
||||||
const cb_send = document.querySelector(
|
const cb_send = document.querySelector(
|
||||||
"#mgc_cb_evo_form > input[type=image]:nth-child(2)",
|
"#mgc_cb_evo_form > input[type=image]:nth-child(2)",
|
||||||
);
|
);
|
||||||
const cb_input = document.querySelector("#mgc_cb_evo_input");
|
|
||||||
|
|
||||||
// check chatbox elements
|
|
||||||
if (!(cb_form instanceof HTMLFormElement)) return;
|
|
||||||
if (!(cb_send instanceof HTMLInputElement)) return;
|
if (!(cb_send instanceof HTMLInputElement)) return;
|
||||||
if (!(cb_input instanceof HTMLInputElement)) return;
|
|
||||||
|
|
||||||
// create "bttv" button
|
|
||||||
const bttv_btn = (() => {
|
const bttv_btn = (() => {
|
||||||
const btn = document.createElement("a");
|
const btn = document.createElement("a");
|
||||||
btn.style.setProperty("cursor", "pointer");
|
btn.style.setProperty("cursor", "pointer");
|
||||||
|
@ -68,10 +90,10 @@ async function process_text(text: string): Promise<string> {
|
||||||
img.setAttribute("title", "bttv:* emotes ersetzen und absenden");
|
img.setAttribute("title", "bttv:* emotes ersetzen und absenden");
|
||||||
img.style.setProperty("vertical-align", "middle");
|
img.style.setProperty("vertical-align", "middle");
|
||||||
|
|
||||||
// ******************
|
|
||||||
// * business logic *
|
|
||||||
// ******************
|
|
||||||
btn.addEventListener("click", async () => {
|
btn.addEventListener("click", async () => {
|
||||||
|
const cb_input = document.querySelector("#mgc_cb_evo_input");
|
||||||
|
if (!(cb_input instanceof HTMLInputElement)) return;
|
||||||
|
|
||||||
cb_input.value = await process_text(cb_input.value);
|
cb_input.value = await process_text(cb_input.value);
|
||||||
cb_input.focus();
|
cb_input.focus();
|
||||||
cb_send.click();
|
cb_send.click();
|
||||||
|
@ -83,5 +105,5 @@ async function process_text(text: string): Promise<string> {
|
||||||
|
|
||||||
cb_form.insertBefore(bttv_btn, cb_send);
|
cb_form.insertBefore(bttv_btn, cb_send);
|
||||||
|
|
||||||
console.log("[lmlfc-bttv]", `loaded`);
|
console.log("done.");
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -1,18 +1,13 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"module": "CommonJS",
|
"module": "commonjs",
|
||||||
"target": "es6",
|
"target": "es6",
|
||||||
|
"esModuleInterop": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
// flags
|
"rootDir": "src",
|
||||||
"alwaysStrict": true,
|
"outDir": "dist/js",
|
||||||
"strictNullChecks": true,
|
|
||||||
"noEmitOnError": true,
|
"noEmitOnError": true,
|
||||||
"noFallthroughCasesInSwitch": true,
|
"typeRoots": ["node_modules/@types"]
|
||||||
"noImplicitAny": true,
|
|
||||||
"noImplicitThis": true,
|
|
||||||
"noImplicitReturns": true,
|
|
||||||
"noUnusedLocals": true,
|
|
||||||
"forceConsistentCasingInFileNames": true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ const path = require("path");
|
||||||
const CopyPlugin = require("copy-webpack-plugin");
|
const CopyPlugin = require("copy-webpack-plugin");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
mode: "production",
|
||||||
entry: {
|
entry: {
|
||||||
main: path.join(__dirname, "src", "main.ts"),
|
main: path.join(__dirname, "src", "main.ts"),
|
||||||
},
|
},
|
||||||
|
@ -9,26 +10,15 @@ module.exports = {
|
||||||
path: path.join(__dirname, "dist"),
|
path: path.join(__dirname, "dist"),
|
||||||
filename: "[name].js",
|
filename: "[name].js",
|
||||||
},
|
},
|
||||||
devtool: "source-map",
|
|
||||||
resolve: {
|
resolve: {
|
||||||
extensions: [".ts", ".js"],
|
extensions: [".ts", ".js"],
|
||||||
},
|
},
|
||||||
module: {
|
module: {
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
test: /\.[jt]s$/,
|
test: /\.tsx?$/,
|
||||||
exclude: /(node_modules)/,
|
loader: "ts-loader",
|
||||||
use: [
|
exclude: /node_modules/,
|
||||||
{
|
|
||||||
loader: "babel-loader",
|
|
||||||
options: {
|
|
||||||
presets: ["@babel/preset-env"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
loader: "ts-loader",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue