From db3d1c42a1886d162843fe57a0067f4b5f976132 Mon Sep 17 00:00:00 2001 From: coomdev Date: Sun, 2 Jan 2022 06:19:44 +0100 Subject: [PATCH] Enable download button for unhandled types --- main.user.js | 13 ++++++------- src/main.ts | 10 ++++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/main.user.js b/main.user.js index bdca355..9c0021c 100644 --- a/main.user.js +++ b/main.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name PNGExtraEmbed // @namespace https://coom.tech/ -// @version 0.38 +// @version 0.39 // @description uhh // @author You // @match https://boards.4channel.org/*/thread/* @@ -12624,7 +12624,7 @@ inlining = false; a = document.createRange().createContextualFragment(cf).children[0]; } - const type = await fileTypeFromBuffer(res.data); + let type = await fileTypeFromBuffer(res.data); let cont; let w, h; if (type?.mime.startsWith("image")) { @@ -12637,16 +12637,15 @@ } else if (type?.mime.startsWith("audio")) { cont = document.createElement("audio"); cont.autoplay = true; - } else if (type) { + } else { + if (!type) + type = { mime: "application/unknown", "ext": "data" }; 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; + cont.textContent = "Download " + cont.download; } let src; src = post.getAttribute("data-processed"); diff --git a/src/main.ts b/src/main.ts index 27568f7..700f351 100644 --- a/src/main.ts +++ b/src/main.ts @@ -88,7 +88,8 @@ const processPost = async (post: HTMLDivElement) => { if (!a) { inlining = false; a = document.createRange().createContextualFragment(cf).children[0] as HTMLAnchorElement; - } const type = await fileTypeFromBuffer(res.data); + } + let type = await fileTypeFromBuffer(res.data); let cont: HTMLImageElement | HTMLVideoElement | HTMLAudioElement | HTMLAnchorElement; let w: number, h: number; if (type?.mime.startsWith("image")) { @@ -101,15 +102,16 @@ const processPost = async (post: HTMLDivElement) => { } else if (type?.mime.startsWith("audio")) { cont = document.createElement("audio"); cont.autoplay = true; - } else if (type) { + } else { + // If type detection fails, you'd better have an extension + if (!type) + type = { mime: "application/unknown" as any, 'ext': "data" as any }; 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;