Skip to content

Instantly share code, notes, and snippets.

@anadius
Created March 30, 2023 17:49
Show Gist options
  • Save anadius/ba856a1c41f20a55f6e4039834bb3a31 to your computer and use it in GitHub Desktop.
Save anadius/ba856a1c41f20a55f6e4039834bb3a31 to your computer and use it in GitHub Desktop.
Kemono Party attachment download fixer
// ==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