Browse Source

Add Download button for non-inlinable types

pull/7/head
coomdev 2 years ago
parent
commit
8f4e089507
  1. 22
      main.user.js
  2. 23
      src/main.ts

22
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) => {

23
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 () => {

Loading…
Cancel
Save