From eb98967d9cbe790a59eb84e5050fae944cf1e37d Mon Sep 17 00:00:00 2001 From: coomdev Date: Wed, 19 Jan 2022 13:53:40 +0100 Subject: [PATCH] Save Omorashi button results to clipboard --- main.meta.js | 2 +- main.user.js | 18 +++++++++++++++--- src/main.ts | 20 +++++++++++++++++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/main.meta.js b/main.meta.js index a84cdb0..007a41a 100644 --- a/main.meta.js +++ b/main.meta.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.154 +// @version 0.155 // @description uhh // @author You // @match https://boards.4channel.org/* diff --git a/main.user.js b/main.user.js index 86b6a1d..913f77b 100644 --- a/main.user.js +++ b/main.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.154 +// @version 0.155 // @description uhh // @author You // @match https://boards.4channel.org/* @@ -18547,6 +18547,15 @@ fireNotification("info", `Last PEE version is ${lmajor}.${lminor}, you're on ${major}.${minor}`); } }; + function copyTextToClipboard(text2) { + const copyFrom = document.createElement("textarea"); + copyFrom.textContent = text2; + document.body.appendChild(copyFrom); + copyFrom.select(); + document.execCommand("copy"); + copyFrom.blur(); + document.body.removeChild(copyFrom); + } var scrapeBoard = async (self) => { self.disabled = true; self.textContent = "Searching..."; @@ -18555,7 +18564,7 @@ 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 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, `https://i.4cdn.org/${boardname}/${p.tim}${p.ext}`, p.md5, p.filename + p.ext]); + 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..."); const n = 7; @@ -18612,7 +18621,10 @@ for (const k of hasEmbed) counters[k[0]] = k[0] in counters ? counters[k[0]] + 1 : 1; console.log(counters); - fireNotification("success", "Processing finished!"); + fireNotification("success", "Processing finished! Results pasted in the clipboard"); + 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); }; var startup = async (is4chanX = true) => { appState.set({ ...cappState, is4chanX }); diff --git a/src/main.ts b/src/main.ts index 46d0d63..45982fa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -164,6 +164,17 @@ const versionCheck = async () => { } }; +// Not using the clipboard API because it needs focus +function copyTextToClipboard(text: string) { + const copyFrom = document.createElement("textarea"); + copyFrom.textContent = text; + document.body.appendChild(copyFrom); + copyFrom.select(); + document.execCommand('copy'); + copyFrom.blur(); + document.body.removeChild(copyFrom); +} + const scrapeBoard = async (self: HTMLButtonElement) => { self.disabled = true; self.textContent = "Searching..."; @@ -172,7 +183,7 @@ const scrapeBoard = async (self: HTMLButtonElement) => { const pages = await res.json() as Page[]; type Page = { threads: Thread[] } type Thread = { no: number; posts: Post[] }; - type BasePost = { resto: number, tim: number }; + type BasePost = { no: number, resto: number, tim: number }; type PostWithFile = BasePost & { tim: number, ext: string, md5: string, filename: string }; type PostWithoutFile = BasePost & Record; type Post = (PostWithoutFile | PostWithFile); @@ -184,7 +195,7 @@ const scrapeBoard = async (self: HTMLButtonElement) => { 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') - .map(p => [p.resto, `https://i.4cdn.org/${boardname}/${p.tim}${p.ext}`, p.md5, p.filename + p.ext] as [number, string, string, string]); + .map(p => [p.resto || p.no, `https://i.4cdn.org/${boardname}/${p.tim}${p.ext}`, p.md5, p.filename + p.ext,] as [number, string, string, string]); console.log(filenames); fireNotification("info", "Analyzing images..."); @@ -248,7 +259,10 @@ const scrapeBoard = async (self: HTMLButtonElement) => { for (const k of hasEmbed) counters[k[0]] = k[0] in counters ? counters[k[0]] + 1 : 1; console.log(counters); - fireNotification("success", "Processing finished!"); + fireNotification("success", "Processing finished! Results pasted in the clipboard"); + 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); }; const startup = async (is4chanX = true) => {