Created
March 30, 2023 17:49
-
-
Save anadius/ba856a1c41f20a55f6e4039834bb3a31 to your computer and use it in GitHub Desktop.
Kemono Party attachment download fixer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Kemono Party attachment download fixer | |
// @author anadius | |
// @namespace anadius.hermietkreeft.site | |
// @match https://kemono.party/* | |
// @version 1.0.0 | |
// @grant GM.xmlHttpRequest | |
// @run-at document-end | |
// ==/UserScript== | |
const downloadBlob = (blob, name) => { | |
const link = document.createElement("a"); | |
const url = window.URL.createObjectURL(blob); | |
link.href = url; | |
link.download = name; | |
document.body.appendChild(link); | |
link.click(); | |
window.URL.revokeObjectURL(url); | |
link.remove(); | |
}; | |
const downloadAttachment = async event => { | |
event.preventDefault(); | |
const attachment = event.target; | |
GM.xmlHttpRequest({ | |
method: "GET", | |
url: attachment.href, | |
responseType: "blob", | |
onload: response => { | |
downloadBlob(response.response, decodeURIComponent(attachment.download)); | |
attachment.removeAttribute("data-progress"); | |
}, | |
onprogress: response => { | |
let progress; | |
if(response.lengthComputable) { | |
progress = `${(response.loaded / response.total * 100).toFixed(2)}%`; | |
} | |
else { | |
progress = `${response.loaded} bytes loaded`; | |
} | |
attachment.setAttribute("data-progress", progress); | |
}, | |
}); | |
}; | |
document.styleSheets[0].addRule(".post__attachment-link:after","color: red; content: attr(data-progress)"); | |
for(const attachment of document.querySelectorAll(".post__attachment-link")) { | |
attachment.addEventListener("click", downloadAttachment); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment