From 62834774e98f86b7b6453e7d33eb262b1a5ba1a8 Mon Sep 17 00:00:00 2001 From: coomdev Date: Wed, 12 Jan 2022 14:55:57 +0100 Subject: [PATCH] Insert multiple catbox files in post --- main.meta.js | 2 +- main.user.js | 80 +++++----------------------- src/main.ts | 139 ++++++++++++++++++++++++------------------------ src/pngv3.ts | 4 +- src/requests.ts | 4 +- src/utils.ts | 9 ++-- 6 files changed, 93 insertions(+), 145 deletions(-) diff --git a/main.meta.js b/main.meta.js index d524db1..1a76d29 100644 --- a/main.meta.js +++ b/main.meta.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.122 +// @version 0.123 // @description uhh // @author You // @match https://boards.4channel.org/* diff --git a/main.user.js b/main.user.js index 732705f..c47fb3e 100644 --- a/main.user.js +++ b/main.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.122 +// @version 0.123 // @description uhh // @author You // @match https://boards.4channel.org/* @@ -11284,10 +11284,10 @@ return new Promise((resolve, reject) => { const gmopt = { url: url.toString(), - data: opt?.body?.toString(), + data: opt?.body, responseType: "blob", headers: opt?.headers, - method: "GET", + method: opt?.method || "GET", ...lisn ? { onprogress: (prog) => { if (prog.loaded != prog.total && prog.total != 0) @@ -11393,14 +11393,15 @@ return new Blob([ret]); }; var decodeCoom3Payload = async (buff) => { - const pees = buff.toString().split("\0").slice(0, 5); + const pees = buff.toString().split("\0").slice(0, 5).filter((e) => e.startsWith("http")); return Promise.all(pees.map(async (pee) => { + const headers = headerStringToObject(await GM_head(pee)); const res = await GM_fetch(pee, { headers: { ranges: "bytes=0-2048" }, mode: "cors", referrerPolicy: "no-referrer" }); - const size = +(res.headers.get("content-size") || 0); + const size = +headers["content-size"] || 0; const header = import_buffer2.Buffer.from(await res.arrayBuffer()); let hptr = 0; if (header.slice(0, 4).toString() == "PEE\0") @@ -11522,14 +11523,14 @@ let total = 0; fireNotification("info", `Uploading ${injs.length} files...`); const links = await Promise.all(injs.map(async (inj) => { - const ret = (await GM_fetch("https://catbox.moe/user/api.php", { + const ret = await (await GM_fetch("https://catbox.moe/user/api.php", { method: "POST", body: parseForm({ reqtype: "fileupload", fileToUpload: await buildPeeFile(inj) }) })).text(); - fireNotification("info", `Uploaded files [${++total}/${injs.length}]`); + fireNotification("info", `Uploaded files [${++total}/${injs.length}] ${ret}`); return ret; })); let magic2 = false; @@ -15405,7 +15406,7 @@ // dist/requests.js init_esbuild_inject(); var xmlhttprequest2 = typeof GM_xmlhttpRequest != "undefined" ? GM_xmlhttpRequest : typeof GM != "undefined" ? GM.xmlHttpRequest : window["GM_xmlhttpRequest"]; - var headerStringToObject3 = (s) => Object.fromEntries(s.split("\n").map((e) => { + var headerStringToObject2 = (s) => Object.fromEntries(s.split("\n").map((e) => { const [name, ...rest] = e.split(":"); return [name.toLowerCase(), rest.join(":").trim()]; })); @@ -15917,7 +15918,7 @@ if (!type) return; } else { - let head = headerStringToObject3(await GM_head2(thumb, void 0)); + let head = headerStringToObject2(await GM_head2(thumb, void 0)); type = { ext: "", mime: head["content-type"].split(";")[0].trim() @@ -15975,7 +15976,7 @@ } else { $$invalidate(5, url = file.data); $$invalidate(14, furl = file.data); - let head = headerStringToObject3(await GM_head2(file.data, void 0)); + let head = headerStringToObject2(await GM_head2(file.data, void 0)); type = { ext: "", mime: head["content-type"].split(";")[0].trim() @@ -16930,7 +16931,6 @@ isCatalog: !!document.querySelector(".catalog-small") || !!location.pathname.match(/\/catalog$/) }); const n = 7; - console.log(posts); const range = ~~(posts.length / n) + 1; await Promise.all([...new Array(n + 1)].map(async (e, i) => { const postsslice = posts.slice(i * range, (i + 1) * range); @@ -17010,6 +17010,8 @@ customStyles.appendChild(document.createTextNode(global_default)); document.documentElement.insertBefore(customStyles, null); function processAttachments(post, ress) { + if (ress.length == 0) + return; const replyBox = post.querySelector(".post"); const external = ress[0][1]; if (external) @@ -17077,62 +17079,6 @@ } post.setAttribute("data-processed", "true"); } - function parseForm2(data) { - const form = new FormData(); - Object.entries(data).filter(([key, value]) => value !== null).map(([key, value]) => form.append(key, value)); - return form; - } - if (window["pagemode"]) { - onload = () => { - const resbuf = async (s) => typeof s != "string" && (import_buffer7.Buffer.isBuffer(s) ? s : await s()); - const container = document.getElementById("container"); - const injection = document.getElementById("injection"); - container.onchange = async () => { - const ret = await fetch("https://catbox.moe/user/api.php", { - method: "POST", - body: parseForm2({ - reqtype: "fileupload", - fileToUpload: container.files[0] - }) - }); - console.log(ret); - console.log(await ret.text()); - }; - }; - } - if (window["pagemode"]) { - onload = () => { - const extraction = document.getElementById("extraction"); - document.addEventListener("CreateNotification", (e) => console.log(e.detail)); - console.log("loaded"); - const container = document.getElementById("container"); - const injection = document.getElementById("injection"); - injection.multiple = true; - extraction.onchange = async () => { - const embedded = await pngv3_default.extract(import_buffer7.Buffer.from(await extraction.files[0].arrayBuffer())); - const d = document.createElement("div"); - new Embeddings_default({ - target: d, - props: { files: embedded } - }); - document.body.append(d); - console.log(embedded); - }; - container.onchange = injection.onchange = async () => { - console.log("eval changed"); - if (container.files?.length && injection.files?.length) { - const dlr = document.getElementById("dlr"); - const res = await pngv3_default.inject(container.files[0], [...injection.files]); - const result = document.getElementById("result"); - const res2 = new Blob([res], { type: (await fileTypeFromBuffer(res))?.mime }); - result.src = URL.createObjectURL(res2); - dlr.href = result.src; - console.log("url created"); - return; - } - }; - }; - } })(); /*! * The buffer module from node.js, for the browser. diff --git a/src/main.ts b/src/main.ts index b666880..007c75f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -192,7 +192,7 @@ const startup = async () => { // await processPost(posts[i] as any); const n = 7; - console.log(posts); + //console.log(posts); const range = ~~(posts.length / n) + 1; await Promise.all([...new Array(n + 1)].map(async (e, i) => { const postsslice = posts.slice(i * range, (i + 1) * range); @@ -295,6 +295,8 @@ customStyles.appendChild(document.createTextNode(globalCss)); document.documentElement.insertBefore(customStyles, null); function processAttachments(post: HTMLDivElement, ress: [EmbeddedFile, boolean][]) { + if (ress.length == 0) + return; const replyBox = post.querySelector('.post'); const external = ress[0][1]; if (external) @@ -379,71 +381,70 @@ function parseForm(data: object) { return form; } -if ((window as any)['pagemode']) { - onload = () => { - const resbuf = async (s: EmbeddedFile['data']) => typeof s != "string" && (Buffer.isBuffer(s) ? s : await s()); - const container = document.getElementById("container") as HTMLInputElement; - const injection = document.getElementById("injection") as HTMLInputElement; - container.onchange = async () => { - const ret = await fetch("https://catbox.moe/user/api.php", { - method: 'POST', - body: parseForm({ - reqtype: 'fileupload', - fileToUpload: container.files![0] - }) - }); - console.log(ret); - console.log(await ret.text()); - }; - }; -} - -if ((window as any)['pagemode']) { - onload = () => { - const extraction = document.getElementById("extraction") as HTMLInputElement; - /* extraction.onchange = async () => { - const pee = await buildPeeFile(extraction.files![0]); - const dlr = document.getElementById("dlr") as HTMLAnchorElement; - dlr.href = URL.createObjectURL(pee); - };*/ - - document.addEventListener("CreateNotification", (e: any) => console.log(e.detail)); - console.log("loaded"); - //const resbuf = async (s: any) => ((Buffer.isBuffer(s) ? s : await s())); - const container = document.getElementById("container") as HTMLInputElement; - const injection = document.getElementById("injection") as HTMLInputElement; - injection.multiple = true; - extraction.onchange = async () => { - const embedded = await pngv3.extract(Buffer.from(await extraction.files![0].arrayBuffer())); - const d = document.createElement('div'); - new Embeddings({ - target: d, - props: {files: embedded} - }); - document.body.append(d); - console.log(embedded); - }; - - container.onchange = injection.onchange = async () => { - console.log('eval changed'); - if (container.files?.length && injection.files?.length) { - const dlr = document.getElementById("dlr") as HTMLAnchorElement; - //const dle = document.getElementById("dle") as HTMLAnchorElement; - const res = await pngv3.inject!(container.files[0], [...injection.files]); - const result = document.getElementById("result") as HTMLImageElement; - //const extracted = document.getElementById("extracted") as HTMLImageElement; - const res2 = new Blob([res], { type: (await fileTypeFromBuffer(res))?.mime }); - result.src = URL.createObjectURL(res2); - dlr.href = result.src; - console.log('url created'); - //const embedded = await pngv3.extract(res); - //if (!embedded) { - // debugger; - return; - //} - //extracted.src = URL.createObjectURL(new Blob([await resbuf(embedded.data!)])); - //dle.href = extracted.src; - } - }; - }; -} +// if ((window as any)['pagemode']) { +// onload = () => { +// const resbuf = async (s: EmbeddedFile['data']) => typeof s != "string" && (Buffer.isBuffer(s) ? s : await s()); +// const container = document.getElementById("container") as HTMLInputElement; +// const injection = document.getElementById("injection") as HTMLInputElement; +// container.onchange = async () => { +// const ret = await fetch("https://catbox.moe/user/api.php", { +// method: 'POST', +// body: parseForm({ +// reqtype: 'fileupload', +// fileToUpload: container.files![0] +// }) +// }); +// console.log(ret); +// console.log(await ret.text()); +// }; +// }; +// } + +// if ((window as any)['pagemode']) { +// onload = () => { +// const extraction = document.getElementById("extraction") as HTMLInputElement; +// /* extraction.onchange = async () => { +// const pee = await buildPeeFile(extraction.files![0]); +// const dlr = document.getElementById("dlr") as HTMLAnchorElement; +// dlr.href = URL.createObjectURL(pee); +// };*/ + +// document.addEventListener("CreateNotification", (e: any) => console.log(e.detail)); +// console.log("loaded"); +// //const resbuf = async (s: any) => ((Buffer.isBuffer(s) ? s : await s())); +// const container = document.getElementById("container") as HTMLInputElement; +// const injection = document.getElementById("injection") as HTMLInputElement; +// injection.multiple = true; +// extraction.onchange = async () => { +// const embedded = await pngv3.extract(Buffer.from(await extraction.files![0].arrayBuffer())); +// const d = document.createElement('div'); +// new Embeddings({ +// target: d, +// props: { files: embedded } +// }); +// document.body.append(d); +// }; + +// container.onchange = injection.onchange = async () => { +// console.log('eval changed'); +// if (container.files?.length && injection.files?.length) { +// const dlr = document.getElementById("dlr") as HTMLAnchorElement; +// //const dle = document.getElementById("dle") as HTMLAnchorElement; +// const res = await pngv3.inject!(container.files[0], [...injection.files]); +// const result = document.getElementById("result") as HTMLImageElement; +// //const extracted = document.getElementById("extracted") as HTMLImageElement; +// const res2 = new Blob([res], { type: (await fileTypeFromBuffer(res))?.mime }); +// result.src = URL.createObjectURL(res2); +// dlr.href = result.src; +// console.log('url created'); +// //const embedded = await pngv3.extract(res); +// //if (!embedded) { +// // debugger; +// return; +// //} +// //extracted.src = URL.createObjectURL(new Blob([await resbuf(embedded.data!)])); +// //dle.href = extracted.src; +// } +// }; +// }; +// } diff --git a/src/pngv3.ts b/src/pngv3.ts index df04b44..300e773 100644 --- a/src/pngv3.ts +++ b/src/pngv3.ts @@ -93,14 +93,14 @@ const inject = async (container: File, injs: File[]) => { let total = 0; fireNotification('info', `Uploading ${injs.length} files...`); const links = await Promise.all(injs.map(async inj => { - const ret = (await GM_fetch("https://catbox.moe/user/api.php", { + const ret = await (await GM_fetch("https://catbox.moe/user/api.php", { method: 'POST', body: parseForm({ reqtype: 'fileupload', fileToUpload: await buildPeeFile(inj) }) })).text(); - fireNotification('info', `Uploaded files [${++total}/${injs.length}]`); + fireNotification('info', `Uploaded files [${++total}/${injs.length}] ${ret}`); return ret; })); diff --git a/src/requests.ts b/src/requests.ts index 5900c0b..400e25f 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -56,10 +56,10 @@ export let GM_fetch = (...[url, opt, lisn]: [...Parameters, EventT // https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest const gmopt: Tampermonkey.Request = { url: url.toString(), - data: opt?.body?.toString(), + data: opt?.body as any, responseType: "blob", headers: opt?.headers as any, - method: "GET", + method: opt?.method as any || "GET", ...(lisn ? { onprogress: (prog) => { if (prog.loaded != prog.total && prog.total != 0) diff --git a/src/utils.ts b/src/utils.ts index a01f602..275c699 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ import { Buffer } from "buffer"; -import { GM_fetch, headerStringToObject } from "./requests"; +import { GM_fetch, GM_head, headerStringToObject } from "./requests"; import thumbnail from "./assets/hasembed.png"; import type { EmbeddedFile } from './main'; @@ -44,7 +44,7 @@ export const buildPeeFile = async (f: File) => { let thumbnail = Buffer.alloc(0); thumbnail = await generateThumbnail(f); const namebuf = Buffer.from(f.name); - const ret = Buffer.alloc(4 /* Magic */ + + const ret = Buffer.alloc(4 /* Magic */ + 1 /* Flags */ + namebuf.byteLength + 1 + (4 + thumbnail.byteLength) /* TSize + Thumbnail */ + f.size /*Teh file*/); @@ -74,14 +74,15 @@ rest: [X bytes of thumbnail data])[file bytes] &4 => has thumbnail */ export const decodeCoom3Payload = async (buff: Buffer) => { - const pees = buff.toString().split('\0').slice(0, 5); + const pees = buff.toString().split('\0').slice(0, 5).filter(e => e.startsWith("http")); return Promise.all(pees.map(async pee => { + const headers = headerStringToObject(await GM_head(pee)); const res = await GM_fetch(pee, { headers: { ranges: 'bytes=0-2048' }, mode: 'cors', referrerPolicy: 'no-referrer', }); - const size = +(res.headers.get('content-size') || 0); + const size = +headers['content-size'] || 0; const header = Buffer.from(await res.arrayBuffer()); let hptr = 0; if (header.slice(0, 4).toString() == "PEE\0")