// ==UserScript==
// @name 8Muses Downloader
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://8muses.xxx/comics*/issue-*/
// @icon https://www.google.com/s2/favicons?sz=64&domain=8muses.xxx
// @grant GM_download
// @run-at document-end
// ==/UserScript==
function waitForElm(selector) {
return new Promise(resolve => {
if (document.querySelectorAll(selector)) {
return resolve(document.querySelectorAll(selector));
}
const observer = new MutationObserver(mutations => {
if (document.querySelectorAll(selector)) {
resolve(document.querySelectorAll(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
function downloadImage(url, filename, comicName, comicChapter) {
GM_download({
url: url,
name: "8muses\\" + comicName.replace(/[\/\\:*?"<>]/g, "") + "\\" + comicChapter + "\\" + filename,
saveAs: false
});
}
function addGenericButton(downloadEvent) {
let btn = document.createElement("button");
btn.style.cssText = "position:fixed;top:0px;left:0px;background-color:red;z-index:9999;";
btn.innerText = "Download All";
btn.onclick = downloadEvent;
document.body.appendChild(btn);
}
function splitLastOccurrence(str, substring) {
const lastIndex = str.lastIndexOf(substring);
const before = str.slice(0, lastIndex);
const after = str.slice(lastIndex + 1);
return [before, after];
}
(function() {
waitForElm('.c-tile').then((elms) => {
addGenericButton(function()
{
let comicInfo = document.querySelector("h1").innerText;
let comicName = splitLastOccurrence(comicInfo, "-")[0].trim();
let comicChapter = splitLastOccurrence(comicInfo, "-")[1].trim().split(" ")[1].trim();
let elms = document.querySelectorAll(".c-tile");
for (let i = 0; i < elms.length; i++) {
const img = elms[i].firstElementChild.firstElementChild;
let filename = /[^/]*$/.exec(img.src)[0];
downloadImage(img.src, filename, comicName, comicChapter);
}
});
unsafeWindow.downloadAll = function()
{
let comicInfo = document.querySelector("h1").innerText;
let comicName = comicInfo.split("-")[0].trim();
let comicChapter = comicInfo.split("-")[1].trim().split(" ")[1].trim();
let elms = document.querySelectorAll(".c-tile");
for (let i = 0; i < elms.length; i++) {
const img = elms[i].firstElementChild.firstElementChild;
let filename = /[^/]*$/.exec(img.src)[0];
downloadImage(img.src, filename, comicName, comicChapter);
}
}
});
})();