From f5afd85cfc55f725ae2ab9ad8af81918d0df4bec Mon Sep 17 00:00:00 2001 From: coomdev Date: Fri, 10 Jun 2022 05:44:58 +0200 Subject: [PATCH] Still report embeds when fetching for central cache --- src/main.ts | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main.ts b/src/main.ts index 49395ee..ea0d61b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -87,14 +87,13 @@ type EmbeddedFileWithoutPreview = { export type EmbeddedFile = EmbeddedFileWithPreview | EmbeddedFileWithoutPreview; -const processImage = async (srcs: AsyncGenerator, fn: string, hex: string, prevurl: string, onfound: () => void) => { +const processImage = async (srcs: AsyncGenerator, fn: string, hex: string, prevurl: string) => { const ret = await Promise.all(processors.filter(e => e.match(fn)).map(async proc => { if (proc.skip) { // skip file downloading, file is referenced from the filename // basically does things like filtering out blacklisted tags const md5 = Buffer.from(hex, 'base64'); if (await proc.has_embed(md5, fn, prevurl) === true) { - onfound(); return [await proc.extract(md5, fn), true] as [EmbeddedFile[], boolean]; } return; @@ -128,7 +127,6 @@ const processImage = async (srcs: AsyncGenerator, fn: string //console.log(`Gave up on ${src} after downloading ${cumul.byteLength} bytes...`); return; } - onfound(); return [await proc.extract(cumul), false] as [EmbeddedFile[], boolean]; } catch { // ignore error and retry with another link @@ -199,29 +197,31 @@ const processPost = async (post: HTMLDivElement) => { return; let res2: [EmbeddedFile[], boolean][] | undefined = undefined; + const reportEmbed = () => { + if (csettings.tm) { + // dont report results from archive, only live threads + if (['boards.4chan.org', 'boards.4channel.org'].includes(location.host)) { + if (!cappState.isCatalog) { // only save from within threads + // we must be in a thread, thus the following is valid + const op = +location.pathname.match(/\/thread\/(.*)/)![1]; + pendingPosts.push({ id: +(post.id.match(/([0-9]+)/)![1]), op }); + signalNewEmbeds(); // let it run async + } + } + } + }; + if (shouldUseCache()) { res2 = await getEmbedsFromCache(qp.getCurrentBoard(), +qp.getCurrentThread()!, post.id); } if (!res2) { - res2 = await processImage(origlink, qp.getFilename(post), qp.getMD5(post), thumbLink, - () => { - if (csettings.tm) { - // dont report results from archive, only live threads - if (['boards.4chan.org', 'boards.4channel.org'].includes(location.host)) { - if (!cappState.isCatalog) { // only save from within threads - // we must be in a thread, thus the following is valid - const op = +location.pathname.match(/\/thread\/(.*)/)![1]; - pendingPosts.push({ id: +(post.id.match(/([0-9]+)/)![1]), op }); - signalNewEmbeds(); // let it run async - } - } - } - post.querySelector('.post')?.classList.add("embedfound"); - }); + res2 = await processImage(origlink, qp.getFilename(post), qp.getMD5(post), thumbLink); res2 = res2?.filter(e => e); } if (!res2 || res2.length == 0) return; + reportEmbed(); + post.querySelector('.post')?.classList.add("embedfound"); processAttachments(post, res2?.flatMap(e => e![0].map(k => [k, e![1]] as [EmbeddedFile, boolean]))); }; @@ -694,7 +694,7 @@ function processAttachments(post: HTMLDivElement, ress: [EmbeddedFile, boolean][ target: viewcounthost, props: { board: qp.getCurrentBoard(), - op: cappState.isCatalog ? pid : qp.getCurrentThread(), + op: cappState.isCatalog ? pid : qp.getCurrentThread()!, pid } });