Browse Source

Insert multiple catbox files in post

pull/46/head
coomdev 2 years ago
parent
commit
62834774e9
  1. 2
      main.meta.js
  2. 80
      main.user.js
  3. 139
      src/main.ts
  4. 4
      src/pngv3.ts
  5. 4
      src/requests.ts
  6. 9
      src/utils.ts

2
main.meta.js

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name PNGExtraEmbed // @name PNGExtraEmbed
// @namespace https://coom.tech/ // @namespace https://coom.tech/
// @version 0.122 // @version 0.123
// @description uhh // @description uhh
// @author You // @author You
// @match https://boards.4channel.org/* // @match https://boards.4channel.org/*

80
main.user.js

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name PNGExtraEmbed // @name PNGExtraEmbed
// @namespace https://coom.tech/ // @namespace https://coom.tech/
// @version 0.122 // @version 0.123
// @description uhh // @description uhh
// @author You // @author You
// @match https://boards.4channel.org/* // @match https://boards.4channel.org/*
@ -11284,10 +11284,10 @@
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const gmopt = { const gmopt = {
url: url.toString(), url: url.toString(),
data: opt?.body?.toString(), data: opt?.body,
responseType: "blob", responseType: "blob",
headers: opt?.headers, headers: opt?.headers,
method: "GET", method: opt?.method || "GET",
...lisn ? { ...lisn ? {
onprogress: (prog) => { onprogress: (prog) => {
if (prog.loaded != prog.total && prog.total != 0) if (prog.loaded != prog.total && prog.total != 0)
@ -11393,14 +11393,15 @@
return new Blob([ret]); return new Blob([ret]);
}; };
var decodeCoom3Payload = async (buff) => { 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) => { return Promise.all(pees.map(async (pee) => {
const headers = headerStringToObject(await GM_head(pee));
const res = await GM_fetch(pee, { const res = await GM_fetch(pee, {
headers: { ranges: "bytes=0-2048" }, headers: { ranges: "bytes=0-2048" },
mode: "cors", mode: "cors",
referrerPolicy: "no-referrer" 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()); const header = import_buffer2.Buffer.from(await res.arrayBuffer());
let hptr = 0; let hptr = 0;
if (header.slice(0, 4).toString() == "PEE\0") if (header.slice(0, 4).toString() == "PEE\0")
@ -11522,14 +11523,14 @@
let total = 0; let total = 0;
fireNotification("info", `Uploading ${injs.length} files...`); fireNotification("info", `Uploading ${injs.length} files...`);
const links = await Promise.all(injs.map(async (inj) => { 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", method: "POST",
body: parseForm({ body: parseForm({
reqtype: "fileupload", reqtype: "fileupload",
fileToUpload: await buildPeeFile(inj) fileToUpload: await buildPeeFile(inj)
}) })
})).text(); })).text();
fireNotification("info", `Uploaded files [${++total}/${injs.length}]`); fireNotification("info", `Uploaded files [${++total}/${injs.length}] ${ret}`);
return ret; return ret;
})); }));
let magic2 = false; let magic2 = false;
@ -15405,7 +15406,7 @@
// dist/requests.js // dist/requests.js
init_esbuild_inject(); init_esbuild_inject();
var xmlhttprequest2 = typeof GM_xmlhttpRequest != "undefined" ? GM_xmlhttpRequest : typeof GM != "undefined" ? GM.xmlHttpRequest : window["GM_xmlhttpRequest"]; 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(":"); const [name, ...rest] = e.split(":");
return [name.toLowerCase(), rest.join(":").trim()]; return [name.toLowerCase(), rest.join(":").trim()];
})); }));
@ -15917,7 +15918,7 @@
if (!type) if (!type)
return; return;
} else { } else {
let head = headerStringToObject3(await GM_head2(thumb, void 0)); let head = headerStringToObject2(await GM_head2(thumb, void 0));
type = { type = {
ext: "", ext: "",
mime: head["content-type"].split(";")[0].trim() mime: head["content-type"].split(";")[0].trim()
@ -15975,7 +15976,7 @@
} else { } else {
$$invalidate(5, url = file.data); $$invalidate(5, url = file.data);
$$invalidate(14, furl = 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 = { type = {
ext: "", ext: "",
mime: head["content-type"].split(";")[0].trim() mime: head["content-type"].split(";")[0].trim()
@ -16930,7 +16931,6 @@
isCatalog: !!document.querySelector(".catalog-small") || !!location.pathname.match(/\/catalog$/) isCatalog: !!document.querySelector(".catalog-small") || !!location.pathname.match(/\/catalog$/)
}); });
const n = 7; const n = 7;
console.log(posts);
const range = ~~(posts.length / n) + 1; const range = ~~(posts.length / n) + 1;
await Promise.all([...new Array(n + 1)].map(async (e, i) => { await Promise.all([...new Array(n + 1)].map(async (e, i) => {
const postsslice = posts.slice(i * range, (i + 1) * range); const postsslice = posts.slice(i * range, (i + 1) * range);
@ -17010,6 +17010,8 @@
customStyles.appendChild(document.createTextNode(global_default)); customStyles.appendChild(document.createTextNode(global_default));
document.documentElement.insertBefore(customStyles, null); document.documentElement.insertBefore(customStyles, null);
function processAttachments(post, ress) { function processAttachments(post, ress) {
if (ress.length == 0)
return;
const replyBox = post.querySelector(".post"); const replyBox = post.querySelector(".post");
const external = ress[0][1]; const external = ress[0][1];
if (external) if (external)
@ -17077,62 +17079,6 @@
} }
post.setAttribute("data-processed", "true"); 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. * The buffer module from node.js, for the browser.

139
src/main.ts

@ -192,7 +192,7 @@ const startup = async () => {
// await processPost(posts[i] as any); // await processPost(posts[i] as any);
const n = 7; const n = 7;
console.log(posts); //console.log(posts);
const range = ~~(posts.length / n) + 1; const range = ~~(posts.length / n) + 1;
await Promise.all([...new Array(n + 1)].map(async (e, i) => { await Promise.all([...new Array(n + 1)].map(async (e, i) => {
const postsslice = posts.slice(i * range, (i + 1) * range); const postsslice = posts.slice(i * range, (i + 1) * range);
@ -295,6 +295,8 @@ customStyles.appendChild(document.createTextNode(globalCss));
document.documentElement.insertBefore(customStyles, null); document.documentElement.insertBefore(customStyles, null);
function processAttachments(post: HTMLDivElement, ress: [EmbeddedFile, boolean][]) { function processAttachments(post: HTMLDivElement, ress: [EmbeddedFile, boolean][]) {
if (ress.length == 0)
return;
const replyBox = post.querySelector('.post'); const replyBox = post.querySelector('.post');
const external = ress[0][1]; const external = ress[0][1];
if (external) if (external)
@ -379,71 +381,70 @@ function parseForm(data: object) {
return form; return form;
} }
if ((window as any)['pagemode']) { // if ((window as any)['pagemode']) {
onload = () => { // onload = () => {
const resbuf = async (s: EmbeddedFile['data']) => typeof s != "string" && (Buffer.isBuffer(s) ? s : await s()); // const resbuf = async (s: EmbeddedFile['data']) => typeof s != "string" && (Buffer.isBuffer(s) ? s : await s());
const container = document.getElementById("container") as HTMLInputElement; // const container = document.getElementById("container") as HTMLInputElement;
const injection = document.getElementById("injection") as HTMLInputElement; // const injection = document.getElementById("injection") as HTMLInputElement;
container.onchange = async () => { // container.onchange = async () => {
const ret = await fetch("https://catbox.moe/user/api.php", { // const ret = await fetch("https://catbox.moe/user/api.php", {
method: 'POST', // method: 'POST',
body: parseForm({ // body: parseForm({
reqtype: 'fileupload', // reqtype: 'fileupload',
fileToUpload: container.files![0] // fileToUpload: container.files![0]
}) // })
}); // });
console.log(ret); // console.log(ret);
console.log(await ret.text()); // console.log(await ret.text());
}; // };
}; // };
} // }
if ((window as any)['pagemode']) { // if ((window as any)['pagemode']) {
onload = () => { // onload = () => {
const extraction = document.getElementById("extraction") as HTMLInputElement; // const extraction = document.getElementById("extraction") as HTMLInputElement;
/* extraction.onchange = async () => { // /* extraction.onchange = async () => {
const pee = await buildPeeFile(extraction.files![0]); // const pee = await buildPeeFile(extraction.files![0]);
const dlr = document.getElementById("dlr") as HTMLAnchorElement; // const dlr = document.getElementById("dlr") as HTMLAnchorElement;
dlr.href = URL.createObjectURL(pee); // dlr.href = URL.createObjectURL(pee);
};*/ // };*/
document.addEventListener("CreateNotification", (e: any) => console.log(e.detail)); // document.addEventListener("CreateNotification", (e: any) => console.log(e.detail));
console.log("loaded"); // console.log("loaded");
//const resbuf = async (s: any) => ((Buffer.isBuffer(s) ? s : await s())); // //const resbuf = async (s: any) => ((Buffer.isBuffer(s) ? s : await s()));
const container = document.getElementById("container") as HTMLInputElement; // const container = document.getElementById("container") as HTMLInputElement;
const injection = document.getElementById("injection") as HTMLInputElement; // const injection = document.getElementById("injection") as HTMLInputElement;
injection.multiple = true; // injection.multiple = true;
extraction.onchange = async () => { // extraction.onchange = async () => {
const embedded = await pngv3.extract(Buffer.from(await extraction.files![0].arrayBuffer())); // const embedded = await pngv3.extract(Buffer.from(await extraction.files![0].arrayBuffer()));
const d = document.createElement('div'); // const d = document.createElement('div');
new Embeddings({ // new Embeddings({
target: d, // target: d,
props: {files: embedded} // props: { files: embedded }
}); // });
document.body.append(d); // document.body.append(d);
console.log(embedded); // };
};
// container.onchange = injection.onchange = async () => {
container.onchange = injection.onchange = async () => { // console.log('eval changed');
console.log('eval changed'); // if (container.files?.length && injection.files?.length) {
if (container.files?.length && injection.files?.length) { // const dlr = document.getElementById("dlr") as HTMLAnchorElement;
const dlr = document.getElementById("dlr") as HTMLAnchorElement; // //const dle = document.getElementById("dle") as HTMLAnchorElement;
//const dle = document.getElementById("dle") as HTMLAnchorElement; // const res = await pngv3.inject!(container.files[0], [...injection.files]);
const res = await pngv3.inject!(container.files[0], [...injection.files]); // const result = document.getElementById("result") as HTMLImageElement;
const result = document.getElementById("result") as HTMLImageElement; // //const extracted = document.getElementById("extracted") as HTMLImageElement;
//const extracted = document.getElementById("extracted") as HTMLImageElement; // const res2 = new Blob([res], { type: (await fileTypeFromBuffer(res))?.mime });
const res2 = new Blob([res], { type: (await fileTypeFromBuffer(res))?.mime }); // result.src = URL.createObjectURL(res2);
result.src = URL.createObjectURL(res2); // dlr.href = result.src;
dlr.href = result.src; // console.log('url created');
console.log('url created'); // //const embedded = await pngv3.extract(res);
//const embedded = await pngv3.extract(res); // //if (!embedded) {
//if (!embedded) { // // debugger;
// debugger; // return;
return; // //}
//} // //extracted.src = URL.createObjectURL(new Blob([await resbuf(embedded.data!)]));
//extracted.src = URL.createObjectURL(new Blob([await resbuf(embedded.data!)])); // //dle.href = extracted.src;
//dle.href = extracted.src; // }
} // };
}; // };
}; // }
}

4
src/pngv3.ts

@ -93,14 +93,14 @@ const inject = async (container: File, injs: File[]) => {
let total = 0; let total = 0;
fireNotification('info', `Uploading ${injs.length} files...`); fireNotification('info', `Uploading ${injs.length} files...`);
const links = await Promise.all(injs.map(async inj => { 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', method: 'POST',
body: parseForm({ body: parseForm({
reqtype: 'fileupload', reqtype: 'fileupload',
fileToUpload: await buildPeeFile(inj) fileToUpload: await buildPeeFile(inj)
}) })
})).text(); })).text();
fireNotification('info', `Uploaded files [${++total}/${injs.length}]`); fireNotification('info', `Uploaded files [${++total}/${injs.length}] ${ret}`);
return ret; return ret;
})); }));

4
src/requests.ts

@ -56,10 +56,10 @@ export let GM_fetch = (...[url, opt, lisn]: [...Parameters<typeof fetch>, EventT
// https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest // https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest
const gmopt: Tampermonkey.Request<any> = { const gmopt: Tampermonkey.Request<any> = {
url: url.toString(), url: url.toString(),
data: opt?.body?.toString(), data: opt?.body as any,
responseType: "blob", responseType: "blob",
headers: opt?.headers as any, headers: opt?.headers as any,
method: "GET", method: opt?.method as any || "GET",
...(lisn ? { ...(lisn ? {
onprogress: (prog) => { onprogress: (prog) => {
if (prog.loaded != prog.total && prog.total != 0) if (prog.loaded != prog.total && prog.total != 0)

9
src/utils.ts

@ -1,5 +1,5 @@
import { Buffer } from "buffer"; 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 thumbnail from "./assets/hasembed.png";
import type { EmbeddedFile } from './main'; import type { EmbeddedFile } from './main';
@ -44,7 +44,7 @@ export const buildPeeFile = async (f: File) => {
let thumbnail = Buffer.alloc(0); let thumbnail = Buffer.alloc(0);
thumbnail = await generateThumbnail(f); thumbnail = await generateThumbnail(f);
const namebuf = Buffer.from(f.name); const namebuf = Buffer.from(f.name);
const ret = Buffer.alloc(4 /* Magic */ + const ret = Buffer.alloc(4 /* Magic */ +
1 /* Flags */ + namebuf.byteLength + 1 + 1 /* Flags */ + namebuf.byteLength + 1 +
(4 + thumbnail.byteLength) /* TSize + Thumbnail */ + (4 + thumbnail.byteLength) /* TSize + Thumbnail */ +
f.size /*Teh file*/); f.size /*Teh file*/);
@ -74,14 +74,15 @@ rest: [X bytes of thumbnail data])[file bytes]
&4 => has thumbnail &4 => has thumbnail
*/ */
export const decodeCoom3Payload = async (buff: Buffer) => { 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 => { return Promise.all(pees.map(async pee => {
const headers = headerStringToObject(await GM_head(pee));
const res = await GM_fetch(pee, { const res = await GM_fetch(pee, {
headers: { ranges: 'bytes=0-2048' }, headers: { ranges: 'bytes=0-2048' },
mode: 'cors', mode: 'cors',
referrerPolicy: 'no-referrer', referrerPolicy: 'no-referrer',
}); });
const size = +(res.headers.get('content-size') || 0); const size = +headers['content-size'] || 0;
const header = Buffer.from(await res.arrayBuffer()); const header = Buffer.from(await res.arrayBuffer());
let hptr = 0; let hptr = 0;
if (header.slice(0, 4).toString() == "PEE\0") if (header.slice(0, 4).toString() == "PEE\0")

Loading…
Cancel
Save