From 00bc005e14f5e3201da1e14b63bf10e749f2ee68 Mon Sep 17 00:00:00 2001 From: coomdev Date: Wed, 5 Jan 2022 20:37:41 +0100 Subject: [PATCH] Fix boorus parsing --- main.meta.js | 2 +- main.user.js | 26 ++++++++++++++++++++------ src/Embedding.svelte | 3 +++ src/thirdeye.ts | 23 ++++++++++++++++------- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/main.meta.js b/main.meta.js index f0d3a53..f48b992 100644 --- a/main.meta.js +++ b/main.meta.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.69 +// @version 0.70 // @description uhh // @author You // @match https://boards.4channel.org/* diff --git a/main.user.js b/main.user.js index e6c9dda..8c43532 100644 --- a/main.user.js +++ b/main.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.69 +// @version 0.70 // @description uhh // @author You // @match https://boards.4channel.org/* @@ -11294,7 +11294,7 @@ } // src/thirdeye.ts - var gelquirk = (a) => a.post?.map((e) => ({ + var gelquirk = (a) => (a.post || a).map((e) => ({ ext: e.image.substr(e.image.indexOf(".") + 1), full_url: e.file_url, preview_url: e.preview_url, @@ -11361,19 +11361,28 @@ var cache = {}; var findFileFrom = async (b, hex) => { try { - if (hex in cache) - return cache[hex]; + if (b.domain in cache && hex in cache[b.domain]) + return cache[b.domain][hex]; const res = await GM_fetch(`https://${b.domain}${b.endpoint}${hex}`); const pres = await res.json(); const tran = b.quirks(pres).filter((e) => !e.tags.some((e2) => black.has(e2))); - cache[hex] = tran; + if (!(b.domain in cache)) + cache[b.domain] = {}; + cache[b.domain][hex] = tran; return tran; } catch { return []; } }; var extract4 = async (b, fn) => { - const result = await Promise.race(Object.values(boorus).filter((e) => sources.has(e.domain)).map((e) => findFileFrom(e, fn.substring(0, 32)))); + let result; + for (const e of Object.values(boorus)) { + if (!sources.has(e.domain)) + continue; + result = await findFileFrom(e, fn.substring(0, 32)); + if (result.length) + break; + } return { filename: fn.substring(33) + result[0].ext, thumbnail: await (await GM_fetch(result[0].preview_url)).arrayBuffer(), @@ -11386,6 +11395,8 @@ if (!sources.has(e.domain)) continue; result = await findFileFrom(e, fn.substring(0, 32)); + if (result.length) + break; } return result && result.length != 0; }; @@ -14409,12 +14420,15 @@ $$invalidate(5, url = URL.createObjectURL(new Blob([thumb], { type: type?.mime }))); if (!type) { $$invalidate(4, isFile = true); + debugger; return; } $$invalidate(8, ftype = type.mime); $$invalidate(1, isVideo = type.mime.startsWith("video/")); $$invalidate(3, isAudio = type.mime.startsWith("audio/")); $$invalidate(2, isImage = type.mime.startsWith("image/")); + if (type.mime.includes("svg")) + debugger; if (isImage) $$invalidate(6, contracted = !$settings.xpi); if (isVideo) { diff --git a/src/Embedding.svelte b/src/Embedding.svelte index f47c613..8202ce3 100644 --- a/src/Embedding.svelte +++ b/src/Embedding.svelte @@ -34,12 +34,15 @@ url = URL.createObjectURL(new Blob([thumb], { type: type?.mime })) if (!type) { isFile = true + debugger; return; } ftype = type.mime; isVideo = type.mime.startsWith('video/') isAudio = type.mime.startsWith('audio/') isImage = type.mime.startsWith('image/') + if (type.mime.includes('svg')) + debugger; if (isImage) contracted = !$settings.xpi if (isVideo) { diff --git a/src/thirdeye.ts b/src/thirdeye.ts index 1399baf..e77a849 100644 --- a/src/thirdeye.ts +++ b/src/thirdeye.ts @@ -18,7 +18,7 @@ export type BooruMatch = { type tran = (a: any) => BooruMatch[]; const gelquirk: tran = a => - a.post?.map((e: any) => ({ + (a.post || a).map((e: any) => ({ ext: e.image.substr(e.image.indexOf('.') + 1), full_url: e.file_url, preview_url: e.preview_url, @@ -94,13 +94,15 @@ const cache: any = {}; const findFileFrom = async (b: Booru, hex: string) => { try { - if (hex in cache) - return cache[hex] as BooruMatch[]; + if (b.domain in cache && hex in cache[b.domain]) + return cache[b.domain][hex] as BooruMatch[]; const res = await GM_fetch(`https://${b.domain}${b.endpoint}${hex}`); // might throw because some endpoint respond with invalid json when an error occurs const pres = await res.json(); const tran = b.quirks(pres).filter(e => !e.tags.some(e => black.has(e))); - cache[hex] = tran; + if (!(b.domain in cache)) + cache[b.domain] = {}; + cache[b.domain][hex] = tran; return tran; } catch { return []; @@ -108,9 +110,14 @@ const findFileFrom = async (b: Booru, hex: string) => { }; const extract = async (b: Buffer, fn?: string) => { - const result = await Promise.race(Object.values(boorus) - .filter(e => sources.has(e.domain)) - .map(e => findFileFrom(e, fn!.substring(0, 32)))); + let result!: BooruMatch[]; + for (const e of Object.values(boorus)) { + if (!sources.has(e.domain)) + continue; + result = await findFileFrom(e, fn!.substring(0, 32)); + if (result.length) + break; + } return { filename: fn!.substring(33) + result[0].ext, thumbnail: (await (await GM_fetch(result[0].preview_url)).arrayBuffer()), @@ -126,6 +133,8 @@ const has_embed = async (b: Buffer, fn?: string) => { if (!sources.has(e.domain)) continue; result = await findFileFrom(e, fn!.substring(0, 32)); + if (result.length) + break; } return result && result.length != 0; };