Last active
October 17, 2022 20:56
-
-
Save WIIASD/c46686b85405439d4b5b920e99685c70 to your computer and use it in GitHub Desktop.
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 NovelAiHistoryDownloader | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Please use Chrome and set your tampermonkey download mode to "Browser API"! | |
// @author You | |
// @match https://novelai.net | |
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== | |
// @grant GM_download | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
let id = 0 | |
let btn = createBtn() | |
const timer = ms => new Promise(res => setTimeout(res, ms)) | |
function createBtn(){ | |
let page = document.getElementById("__next") | |
let button = document.createElement("button") | |
button.innerText = "Save History" | |
button.style.color = '#f4f3c5'; | |
button.style.backgroundColor = '#14153b' | |
button.style.float = 'right' | |
button.style.marginRight = '15px' | |
button.style.cursor = 'pointer' | |
button.onclick = async () => {await download(); id=0} | |
if(page){ | |
console.log("Button created!") | |
page.appendChild(button) | |
}else{ | |
alert("holy sh**t! Sth wrong when loading the button!") | |
} | |
return button | |
} | |
async function download(){ | |
let elements = document.getElementById("historyContainer").querySelectorAll('[role="button"]') | |
let projectName = "defaultProjectName" | |
if(elements.length > 0){ | |
projectName = prompt("Please enter project name", ""); | |
if (projectName == ""){ | |
alert("Please enter something!"); | |
return | |
} | |
}else{ | |
alert("No History!") | |
return | |
} | |
for(let i = elements.length - 1 ; i >= 0 ; i--){ | |
elements[i].click() | |
await timer(1000) | |
let images = document.getElementsByTagName("img") | |
downloadImages(images, projectName) | |
await timer(1000) | |
} | |
} | |
function downloadImages(imgs, projectName){ | |
let sourceFolder = "NovelAI_History/" + projectName + "/" | |
let subFolder = "" | |
let fileName = "" + id + ".jpg" | |
if(imgs.length > 1){ | |
subFolder = "" + id + "/" | |
} | |
for(let img of imgs){ | |
let downloadArgs = { | |
url: img.currentSrc, | |
name: sourceFolder + subFolder + fileName | |
} | |
GM_download(downloadArgs) | |
id++; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment