From 8f4e08950745448fb76a45478512f93e56179e29 Mon Sep 17 00:00:00 2001 From: coomdev Date: Sun, 2 Jan 2022 06:15:23 +0100 Subject: [PATCH] Add Download button for non-inlinable types --- main.user.js | 22 ++++++++++++++++++---- src/main.ts | 23 ++++++++++++++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/main.user.js b/main.user.js index a9569db..bdca355 100644 --- a/main.user.js +++ b/main.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.37 +// @version 0.38 // @description uhh // @author You // @match https://boards.4channel.org/*/thread/* @@ -12637,13 +12637,25 @@ } else if (type?.mime.startsWith("audio")) { cont = document.createElement("audio"); cont.autoplay = true; - } else + } else if (type) { + cont = document.createElement("a"); + let fn = res.filename; + if (!fn.includes(".")) + fn += "." + type.ext; + cont.download = fn; + a.textContent = "Download " + cont.download; + } else { + debugger; return; + } let src; src = post.getAttribute("data-processed"); if (!src) src = URL.createObjectURL(new Blob([res.data], { type: type.mime })); - cont.src = src; + if (!(cont instanceof HTMLAnchorElement)) + cont.src = src; + else + cont.href = src; await new Promise((res2) => { if (cont instanceof HTMLImageElement) cont.onload = res2; @@ -12651,6 +12663,8 @@ cont.onloadedmetadata = res2; else if (cont instanceof HTMLAudioElement) cont.onloadedmetadata = res2; + else + res2(void 0); }); if (cont instanceof HTMLImageElement) { w = cont.naturalWidth; @@ -12706,7 +12720,7 @@ }; if (!inlining) fi.children[1].insertAdjacentElement("afterend", a); - post.setAttribute("data-processed", cont.src); + post.setAttribute("data-processed", src); }; var startup = async () => { const mo = new MutationObserver((reco) => { diff --git a/src/main.ts b/src/main.ts index 7fe93f6..27568f7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -89,7 +89,7 @@ const processPost = async (post: HTMLDivElement) => { inlining = false; a = document.createRange().createContextualFragment(cf).children[0] as HTMLAnchorElement; } const type = await fileTypeFromBuffer(res.data); - let cont: HTMLImageElement | HTMLVideoElement | HTMLAudioElement; + let cont: HTMLImageElement | HTMLVideoElement | HTMLAudioElement | HTMLAnchorElement; let w: number, h: number; if (type?.mime.startsWith("image")) { cont = document.createElement("img"); @@ -101,14 +101,25 @@ const processPost = async (post: HTMLDivElement) => { } else if (type?.mime.startsWith("audio")) { cont = document.createElement("audio"); cont.autoplay = true; - } else - return; // TODO: handle new file types??? Or direct "download"? + } else if (type) { + cont = document.createElement('a'); + let fn = res.filename; + if (!fn.includes('.')) + fn += '.' + type.ext; + cont.download = fn; + cont.textContent = "Download " + cont.download; + } else { + return; // don't know what kind of file: don't touch + } let src: string | null; src = post.getAttribute('data-processed'); if (!src) src = URL.createObjectURL(new Blob([res.data], { type: type.mime })); - cont.src = src; + if (!(cont instanceof HTMLAnchorElement)) + cont.src = src; + else + cont.href = src; await new Promise(res => { if (cont instanceof HTMLImageElement) @@ -117,6 +128,8 @@ const processPost = async (post: HTMLDivElement) => { cont.onloadedmetadata = res; else if (cont instanceof HTMLAudioElement) cont.onloadedmetadata = res; + else + res(void 0); // Don't know what this is: don't wait }); if (cont instanceof HTMLImageElement) { @@ -181,7 +194,7 @@ const processPost = async (post: HTMLDivElement) => { }; if (!inlining) fi.children[1].insertAdjacentElement('afterend', a); - post.setAttribute('data-processed', cont.src); + post.setAttribute('data-processed', src); }; const startup = async () => {