From 59f28fc30ed87eeb5a9b194cd0efab1f13b2cbc5 Mon Sep 17 00:00:00 2001 From: coomdev Date: Fri, 21 Jan 2022 09:18:20 +0100 Subject: [PATCH] Use both clipboard writing method in case one doesn't work. Fix omorashi on 404 on load --- main.meta.js | 2 +- main.user.js | 23 ++++++++++++++++++++--- src/main.ts | 21 ++++++++++++++++++--- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/main.meta.js b/main.meta.js index 007a41a..82a12c7 100644 --- a/main.meta.js +++ b/main.meta.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.155 +// @version 0.156 // @description uhh // @author You // @match https://boards.4channel.org/* diff --git a/main.user.js b/main.user.js index 913f77b..4383de0 100644 --- a/main.user.js +++ b/main.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.155 +// @version 0.156 // @description uhh // @author You // @match https://boards.4channel.org/* @@ -18555,6 +18555,7 @@ document.execCommand("copy"); copyFrom.blur(); document.body.removeChild(copyFrom); + navigator.clipboard.writeText(text2); } var scrapeBoard = async (self) => { self.disabled = true; @@ -18563,7 +18564,14 @@ const res = await GM_fetch(`https://a.4cdn.org/${boardname}/threads.json`); const pages = await res.json(); fireNotification("info", "Fetching all threads..."); - const threads = await Promise.all(pages.reduce((a, b) => [...a, ...b.threads], []).map((e) => e.no).map((id) => GM_fetch(`https://a.4cdn.org/${boardname}/thread/${id}.json`).then((e) => e.json()))); + const threads = (await Promise.all(pages.reduce((a, b) => [...a, ...b.threads], []).map((e) => e.no).map(async (id) => { + try { + const res2 = await GM_fetch(`https://a.4cdn.org/${boardname}/thread/${id}.json`); + return await res2.json(); + } catch { + return void 0; + } + }))).filter((e) => e).map((e) => e); const filenames = threads.reduce((a, b) => [...a, ...b.posts.filter((p) => p.ext).map((p) => p)], []).filter((p) => p.ext != ".webm" && p.ext != ".gif").map((p) => [p.resto || p.no, `https://i.4cdn.org/${boardname}/${p.tim}${p.ext}`, p.md5, p.filename + p.ext]); console.log(filenames); fireNotification("info", "Analyzing images..."); @@ -18625,6 +18633,11 @@ const text2 = Object.entries(counters).sort((a, b) => b[1] - a[1]).map((e) => `>>${e[0]} (${e[1]})`).join("\n"); console.log(text2); copyTextToClipboard(text2); + self.textContent = "Copy Results"; + self.disabled = false; + self.onclick = () => { + copyTextToClipboard(text2); + }; }; var startup = async (is4chanX = true) => { appState.set({ ...cappState, is4chanX }); @@ -18709,7 +18722,11 @@ await Promise.all([...new Array(n + 1)].map(async (e, i) => { const postsslice = posts.slice(i * range, (i + 1) * range); for (const post of postsslice) { - await processPost(post); + try { + await processPost(post); + } catch (e2) { + console.log("Processing failed for post", post, e2); + } } })); }; diff --git a/src/main.ts b/src/main.ts index 45982fa..6e859d1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -173,6 +173,7 @@ function copyTextToClipboard(text: string) { document.execCommand('copy'); copyFrom.blur(); document.body.removeChild(copyFrom); + navigator.clipboard.writeText(text); } const scrapeBoard = async (self: HTMLButtonElement) => { @@ -188,10 +189,17 @@ const scrapeBoard = async (self: HTMLButtonElement) => { type PostWithoutFile = BasePost & Record; type Post = (PostWithoutFile | PostWithFile); fireNotification("info", "Fetching all threads..."); - const threads = await Promise.all(pages + const threads = (await Promise.all(pages .reduce((a: Thread[], b: Page) => [...a, ...b.threads], []) .map(e => e.no) - .map(id => GM_fetch(`https://a.4cdn.org/${boardname}/thread/${id}.json`).then(e => e.json() as Promise))); + .map(async id => { + try { + const res = await GM_fetch(`https://a.4cdn.org/${boardname}/thread/${id}.json`); + return await res.json() as Thread; + } catch { + return undefined; + } + }))).filter(e => e).map(e => e as Thread); const filenames = threads .reduce((a, b) => [...a, ...b.posts.filter(p => p.ext) .map(p => p as PostWithFile)], [] as PostWithFile[]).filter(p => p.ext != '.webm' && p.ext != '.gif') @@ -263,6 +271,11 @@ const scrapeBoard = async (self: HTMLButtonElement) => { const text = Object.entries(counters).sort((a, b) => b[1] - a[1]).map(e => `>>${e[0]} (${e[1]})`).join('\n'); console.log(text); copyTextToClipboard(text); + self.textContent = "Copy Results"; + self.disabled = false; + self.onclick = () => { + copyTextToClipboard(text); + }; }; const startup = async (is4chanX = true) => { @@ -365,7 +378,9 @@ const startup = async (is4chanX = true) => { await Promise.all([...new Array(n + 1)].map(async (e, i) => { const postsslice = posts.slice(i * range, (i + 1) * range); for (const post of postsslice) { - await processPost(post as any); + try { + await processPost(post as any); + } catch (e) { console.log('Processing failed for post', post, e); } } })); //await Promise.all(posts.map(e => processPost(e as any)));