From 0afd9162cde67f265c00d59ac9484ffb8ecd5ce8 Mon Sep 17 00:00:00 2001 From: coomdev Date: Wed, 12 Jan 2022 19:27:07 +0100 Subject: [PATCH] Fixed FF catbox embeds and restricted embeds to catbox --- main.meta.js | 2 +- main.user.js | 96 ++++++++++++++++++++++++++----------------------- src/requests.ts | 5 ++- src/utils.ts | 92 ++++++++++++++++++++++++----------------------- 4 files changed, 104 insertions(+), 91 deletions(-) diff --git a/main.meta.js b/main.meta.js index d3e2436..0571460 100644 --- a/main.meta.js +++ b/main.meta.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.126 +// @version 0.127 // @description uhh // @author You // @match https://boards.4channel.org/* diff --git a/main.user.js b/main.user.js index 11581f2..d0241d2 100644 --- a/main.user.js +++ b/main.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.126 +// @version 0.127 // @description uhh // @author You // @match https://boards.4channel.org/* @@ -11303,7 +11303,10 @@ resolve(resp); }, ontimeout: () => reject("fetch timeout"), - onerror: () => reject("fetch error"), + onerror: (...args) => { + debugger; + reject("fetch error"); + }, onabort: () => reject("fetch abort") }; xmlhttprequest(gmopt); @@ -11392,50 +11395,53 @@ return new Blob([ret]); }; var decodeCoom3Payload = async (buff) => { - const pees = buff.toString().split(" ").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 = +headers["content-size"] || 0; - const header = import_buffer2.Buffer.from(await res.arrayBuffer()); - let hptr = 0; - if (header.slice(0, 4).toString() == "PEE\0") - hptr += 4; - const flags = header[hptr]; - const hasFn = !!(flags & 1); - const hasTags = !!(flags & 2); - const hasThumbnail = !!(flags & 4); - let [ptr, ptr2] = [hptr + 1, hptr + 1]; - let fn = "embedded"; - let tags = []; - let thumb = import_buffer2.Buffer.from(hasembed_default); - if (hasFn) { - while (header[ptr2] != 0) - ptr2++; - fn = header.slice(ptr, ptr2).toString(); - ptr = ++ptr2; - } - if (hasTags) { - while (header[ptr2] != 0) - ptr2++; - tags = header.slice(ptr, ptr2).toString().split(/\s+/); - } - let thumbsize = 0; - if (hasThumbnail) { - thumbsize = header.readInt32LE(ptr); - console.log("Thumbnail size of ", thumbsize); - thumb = import_buffer2.Buffer.from(await (await GM_fetch(pee, { headers: { range: `bytes=${ptr + 4}-${ptr + 4 + thumbsize}` } })).arrayBuffer()); + const pees = buff.toString().split(" ").slice(0, 5).filter((e) => e.startsWith("https://files.catbox.moe/")); + return (await Promise.all(pees.map(async (pee) => { + try { + const headers = headerStringToObject(await GM_head(pee)); + const res = await GM_fetch(pee, { + headers: { ranges: "bytes=0-2048", "user-agent": "" }, + mode: "cors", + referrerPolicy: "no-referrer" + }); + const size = +headers["content-length"] || 0; + const header = import_buffer2.Buffer.from(await res.arrayBuffer()); + let hptr = 0; + if (header.slice(0, 4).toString() == "PEE\0") + hptr += 4; + const flags = header[hptr]; + const hasFn = !!(flags & 1); + const hasTags = !!(flags & 2); + const hasThumbnail = !!(flags & 4); + let [ptr, ptr2] = [hptr + 1, hptr + 1]; + let fn = "embedded"; + let tags = []; + let thumb = import_buffer2.Buffer.from(hasembed_default); + if (hasFn) { + while (header[ptr2] != 0) + ptr2++; + fn = header.slice(ptr, ptr2).toString(); + ptr = ++ptr2; + } + if (hasTags) { + while (header[ptr2] != 0) + ptr2++; + tags = header.slice(ptr, ptr2).toString().split(/\s+/); + } + let thumbsize = 0; + if (hasThumbnail) { + thumbsize = header.readInt32LE(ptr); + thumb = import_buffer2.Buffer.from(await (await GM_fetch(pee, { headers: { "user-agent": "", range: `bytes=${ptr + 4}-${ptr + 4 + thumbsize}` } })).arrayBuffer()); + } + return { + filename: fn, + data: async (lsn) => import_buffer2.Buffer.from(await (await GM_fetch(pee, { headers: { "user-agent": "", range: `bytes=${ptr + 4 + thumbsize}-${size - 1}` } }, lsn)).arrayBuffer()), + thumbnail: thumb + }; + } catch (e) { + console.warn(e); } - return { - filename: fn, - data: async (lsn) => import_buffer2.Buffer.from(await (await GM_fetch(pee, { headers: { range: `bytes=${ptr + 4 + thumbsize}-${size - 1}` } }, lsn)).arrayBuffer()), - thumbnail: thumb - }; - })); + }))).map((e) => e); }; var fireNotification = (level, text2, lifetime = 3) => { document.dispatchEvent(new CustomEvent("CreateNotification", { diff --git a/src/requests.ts b/src/requests.ts index 400e25f..ad5a1ab 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -76,7 +76,10 @@ export let GM_fetch = (...[url, opt, lisn]: [...Parameters, EventT resolve(resp as any); }, ontimeout: () => reject("fetch timeout"), - onerror: () => reject("fetch error"), + onerror: (...args) => { + debugger; + reject("fetch error"); + }, onabort: () => reject("fetch abort") }; xmlhttprequest(gmopt); diff --git a/src/utils.ts b/src/utils.ts index e05ed4f..1ef0f88 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -74,51 +74,55 @@ rest: [X bytes of thumbnail data])[file bytes] &4 => has thumbnail */ export const decodeCoom3Payload = async (buff: Buffer) => { - const pees = buff.toString().split(' ').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 = +headers['content-size'] || 0; - const header = Buffer.from(await res.arrayBuffer()); - let hptr = 0; - if (header.slice(0, 4).toString() == "PEE\0") - hptr += 4; - const flags = header[hptr]; - const hasFn = !!(flags & 1); - const hasTags = !!(flags & 2); - const hasThumbnail = !!(flags & 4); - let [ptr, ptr2] = [hptr + 1, hptr + 1]; - let fn = 'embedded'; - let tags = []; - let thumb: EmbeddedFile['thumbnail'] = Buffer.from(thumbnail); - if (hasFn) { - while (header[ptr2] != 0) - ptr2++; - fn = header.slice(ptr, ptr2).toString(); - ptr = ++ptr2; + const pees = buff.toString().split(' ').slice(0, 5).filter(e => e.startsWith("https://files.catbox.moe/")); + return (await Promise.all(pees.map(async pee => { + try { + const headers = headerStringToObject(await GM_head(pee)); + const res = await GM_fetch(pee, { + headers: { ranges: 'bytes=0-2048', 'user-agent': '' }, + mode: 'cors', + referrerPolicy: 'no-referrer', + }); + const size = +headers['content-length'] || 0; + const header = Buffer.from(await res.arrayBuffer()); + let hptr = 0; + if (header.slice(0, 4).toString() == "PEE\0") + hptr += 4; + const flags = header[hptr]; + const hasFn = !!(flags & 1); + const hasTags = !!(flags & 2); + const hasThumbnail = !!(flags & 4); + let [ptr, ptr2] = [hptr + 1, hptr + 1]; + let fn = 'embedded'; + let tags = []; + let thumb: EmbeddedFile['thumbnail'] = Buffer.from(thumbnail); + if (hasFn) { + while (header[ptr2] != 0) + ptr2++; + fn = header.slice(ptr, ptr2).toString(); + ptr = ++ptr2; + } + if (hasTags) { + while (header[ptr2] != 0) + ptr2++; + tags = header.slice(ptr, ptr2).toString().split(/\s+/); + } + let thumbsize = 0; + if (hasThumbnail) { + thumbsize = header.readInt32LE(ptr); + thumb = Buffer.from(await (await GM_fetch(pee, { headers: { 'user-agent': '', range: `bytes=${ptr + 4}-${ptr + 4 + thumbsize}` } })).arrayBuffer()); + } + return { + filename: fn, + data: async (lsn) => + Buffer.from(await (await GM_fetch(pee, { headers: { 'user-agent': '', range: `bytes=${ptr + 4 + thumbsize}-${size - 1}` } }, lsn)).arrayBuffer()), + thumbnail: thumb, + } as EmbeddedFile; + } catch (e) { + // niggers trying to fuck with bad links + console.warn(e); } - if (hasTags) { - while (header[ptr2] != 0) - ptr2++; - tags = header.slice(ptr, ptr2).toString().split(/\s+/); - } - let thumbsize = 0; - if (hasThumbnail) { - thumbsize = header.readInt32LE(ptr); - console.log("Thumbnail size of ", thumbsize); - thumb = Buffer.from(await (await GM_fetch(pee, { headers: { range: `bytes=${ptr + 4}-${ptr + 4 + thumbsize}` } })).arrayBuffer()); - } - return { - filename: fn, - data: async (lsn) => - Buffer.from(await (await GM_fetch(pee, { headers: { range: `bytes=${ptr + 4 + thumbsize}-${size - 1}` } }, lsn)).arrayBuffer()), - thumbnail: thumb, - } as EmbeddedFile; - })); + }))).map(e => e); }; export const fireNotification = (level: 'success' | 'error' | 'info' | 'warning', text: string, lifetime = 3) => {