From 03bbf1327756d9bed5909882766c812f2dce7985 Mon Sep 17 00:00:00 2001 From: coomdev Date: Thu, 6 Jan 2022 01:10:12 +0100 Subject: [PATCH] Fix accidentally broken GIF embeds --- main.meta.js | 2 +- main.user.js | 6 +++--- src/gif.ts | 2 +- src/main.ts | 51 ++++++++++++++++++++++++++++--------------------- src/requests.ts | 6 +++++- 5 files changed, 39 insertions(+), 28 deletions(-) diff --git a/main.meta.js b/main.meta.js index 71eb2b7..8c54813 100644 --- a/main.meta.js +++ b/main.meta.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.75 +// @version 0.76 // @description uhh // @author You // @match https://boards.4channel.org/* diff --git a/main.user.js b/main.user.js index 65f1805..bf03e7d 100644 --- a/main.user.js +++ b/main.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.75 +// @version 0.76 // @description uhh // @author You // @match https://boards.4channel.org/* @@ -11176,7 +11176,7 @@ if (gcte) endo += 3 * (1 << (field & 7) + 1); if (netscape.compare(contbuff, endo, endo + netscape.byteLength) == 0) - endo += netscape.byteLength; + endo += 19; await writer.write(contbuff.slice(0, endo)); await write_embedding(writer, import_buffer3.Buffer.from(await inj.arrayBuffer())); await writer.write(contbuff.slice(endo)); @@ -11218,7 +11218,7 @@ // src/requests.ts init_esbuild_inject(); - var xmlhttprequest = typeof GM_xmlhttpRequest != "undefined" ? GM_xmlhttpRequest : typeof GM != "undefined" ? GM.xmlHttpRequest : GM_xmlhttpRequest; + var xmlhttprequest = typeof GM_xmlhttpRequest != "undefined" ? GM_xmlhttpRequest : typeof GM != "undefined" ? GM.xmlHttpRequest : window["GM_xmlhttpRequest"]; var headerStringToObject = (s) => Object.fromEntries(s.split("\n").map((e) => { const [name, ...rest] = e.split(":"); return [name.toLowerCase(), rest.join(":").trim()]; diff --git a/src/gif.ts b/src/gif.ts index 629c6a0..6028319 100644 --- a/src/gif.ts +++ b/src/gif.ts @@ -78,7 +78,7 @@ const inject = async (container: File, inj: File) => { endo += 3 * (1 << ((field & 7) + 1)); if (netscape.compare(contbuff, endo, endo + netscape.byteLength) == 0) - endo += netscape.byteLength; + endo += 19; await writer.write(contbuff.slice(0, endo)); await write_embedding(writer, Buffer.from(await inj.arrayBuffer())); await writer.write(contbuff.slice(endo)); diff --git a/src/main.ts b/src/main.ts index 7342955..7eea1c8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -173,7 +173,7 @@ const processPost = async (post: HTMLDivElement) => { } }); if (!ahem) - opFile?.append(imgcont); + opFile?.append(imgcont); } post.setAttribute('data-processed', "true"); @@ -340,24 +340,31 @@ customStyles.appendChild(document.createTextNode( document.documentElement.insertBefore(customStyles, null); -//import * as gif2 from './png'; -// -//onload = () => { -// const container = document.getElementById("container") as HTMLInputElement; -// const injection = document.getElementById("injection") as HTMLInputElement; -// container.onchange = injection.onchange = async () => { -// if (container.files?.length && injection.files?.length) { -// const res = await gif2.inject(container.files[0], injection.files[0]); -// const result = document.getElementById("result") as HTMLImageElement; -// const extracted = document.getElementById("extracted") as HTMLImageElement; -// const res2 = new Blob([res], { type: 'image/gif' }); -// result.src = URL.createObjectURL(res2); -// const embedded = await gif2.extract(res); -// extracted.src = URL.createObjectURL(new Blob([embedded?.data!])); -// const dlr = document.getElementById("dlr") as HTMLAnchorElement; -// const dle = document.getElementById("dle") as HTMLAnchorElement; -// dlr.href = result.src; -// dle.href = extracted.src; -// } -// }; -//}; +// if ((window as any)['pagemode']) { +// onload = () => { +// const resbuf = async (s: EmbeddedFile['data']) => Buffer.isBuffer(s) ? s : await s(); +// const container = document.getElementById("container") as HTMLInputElement; +// const injection = document.getElementById("injection") as HTMLInputElement; +// 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 gif.inject!(container.files[0], injection.files[0]); +// console.log('inj done'); +// const result = document.getElementById("result") as HTMLImageElement; +// const extracted = document.getElementById("extracted") as HTMLImageElement; +// const res2 = new Blob([res], { type: 'image/gif' }); +// result.src = URL.createObjectURL(res2); +// dlr.href = result.src; +// console.log('url created'); +// const embedded = await gif.extract(res); +// if (!embedded) +// debugger; +// extracted.src = URL.createObjectURL(new Blob([await resbuf(embedded?.data!)])); +// dle.href = extracted.src; +// } +// }; +// }; +// } \ No newline at end of file diff --git a/src/requests.ts b/src/requests.ts index 4a6d918..002f29c 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -1,4 +1,8 @@ -const xmlhttprequest = typeof GM_xmlhttpRequest != 'undefined' ? GM_xmlhttpRequest : (typeof GM != "undefined" ? GM.xmlHttpRequest : GM_xmlhttpRequest); +const xmlhttprequest = typeof GM_xmlhttpRequest != 'undefined' ? + GM_xmlhttpRequest : + (typeof GM != "undefined" ? + GM.xmlHttpRequest : + (window as any)['GM_xmlhttpRequest']); export const headerStringToObject = (s: string) => Object.fromEntries(s.split('\n').map(e => {