161 lines
5.5 KiB
JavaScript
161 lines
5.5 KiB
JavaScript
// ==UserScript==
|
|
// @name Furaffinity Comic Downloader
|
|
// @namespace Violentmonkey Scripts
|
|
// @match https://*.furaffinity.net/view/*
|
|
// @grant GM_download
|
|
// @grant GM.getValue
|
|
// @grant GM.setValue
|
|
// @grant GM.deleteValue
|
|
// @version 0.0
|
|
// @author -
|
|
// @description 14/10/2023, 20:01:08
|
|
// ==/UserScript==
|
|
/*img = document.getElementById("submissionImg")
|
|
GM_download({
|
|
url: img.src,
|
|
name: '01.png',
|
|
saveAs: false,
|
|
})*/
|
|
|
|
async function testScript() {
|
|
// let value = await GM.getValue('test')
|
|
// value += ' test'
|
|
// await GM.setValue('test', value)
|
|
// alert(await GM.getValue('test'))
|
|
await GM.deleteValue('test')
|
|
if (await GM.getValue('test') == null) {
|
|
alert('Value empty')
|
|
} else {
|
|
alert('Value set')
|
|
}
|
|
}
|
|
|
|
function addLeadingZeros(num, size) {
|
|
num = num.toString()
|
|
while (num.length < size) num = "0" + num
|
|
return num
|
|
}
|
|
|
|
function downloadImg(imgUrl, pageNumber) {
|
|
let fileExt = imgUrl.split('.')[imgUrl.split('.').length - 1]
|
|
let numberString = addLeadingZeros(pageNumber, 3)
|
|
GM_download({
|
|
url: imgUrl,
|
|
name: `${numberString}.${fileExt}`,
|
|
saveAs: false,
|
|
})
|
|
}
|
|
|
|
function getSubmissionImgUrlFromDOM(dom) {
|
|
return dom.getElementById("submissionImg").src
|
|
}
|
|
|
|
function getComicNavFromDom(dom) {
|
|
let navLinks = dom.getElementsByClassName('parsed_nav_links')
|
|
if (navLinks.length > 0) {
|
|
let prevLink
|
|
let firstLink
|
|
let nextLink
|
|
console.log(navLinks[0])
|
|
for(let childElem of navLinks[0].children) {
|
|
if (childElem.innerText.toLowerCase().includes('prev')) {
|
|
prevLink = childElem.href
|
|
} else if (childElem.innerText.toLowerCase().includes('first')) {
|
|
firstLink = childElem.href
|
|
} else if (childElem.innerText.toLowerCase().includes('next')) {
|
|
nextLink = childElem.href
|
|
}
|
|
}
|
|
return {
|
|
prev: prevLink,
|
|
first : firstLink,
|
|
next: nextLink,
|
|
}
|
|
} else {
|
|
return null
|
|
}
|
|
}
|
|
|
|
async function getComicPagesData(url, pageNumber) {
|
|
let responseText = await fetch(url).then(function (response) {
|
|
return response.text()
|
|
})
|
|
let parser = new DOMParser()
|
|
let doc = parser.parseFromString(responseText, 'text/html')
|
|
console.log(doc)
|
|
let imgUrl = getSubmissionImgUrlFromDOM(doc)
|
|
let navLinks = getComicNavFromDom(doc)
|
|
console.log("Page Num: "+ pageNumber)
|
|
return {navLinks: navLinks, currentUrl: imgUrl, pageNumber: ++pageNumber}
|
|
}
|
|
|
|
async function downloadStartCurrentSite() {
|
|
// TODO
|
|
}
|
|
|
|
async function downloadStartBeginning() {
|
|
let navLinksCurrent = getComicNavFromDom(document)
|
|
console.log(navLinksCurrent)
|
|
let data = await getComicPagesData(navLinksCurrent.first, 0)
|
|
console.log(data)
|
|
do {
|
|
downloadImg(data.currentUrl, data.pageNumber)
|
|
data = await getComicPagesData(data.navLinks.next, data.pageNumber)
|
|
setTimeout(500)
|
|
} while(data.navLinks.next)
|
|
downloadImg(data.currentUrl, data.pageNumber)
|
|
alert("Download abgeschlossen!")
|
|
}
|
|
|
|
function createHTMLforComicBox() {
|
|
let comicSection = document.createElement('section')
|
|
|
|
let boxHeader = document.createElement('div')
|
|
boxHeader.classList.add('section-header')
|
|
let boxSubmissionIdContainer = document.createElement('div')
|
|
boxSubmissionIdContainer.classList.add('submission-id-container')
|
|
let boxSubmissionIdSubContainer = document.createElement('div')
|
|
boxSubmissionIdSubContainer.classList.add('submission-id-sub-container')
|
|
let boxSubmissionTitle = document.createElement('div')
|
|
boxSubmissionTitle.classList.add('submission-title')
|
|
let titleElement = document.createElement('h2')
|
|
let textInTitle = document.createElement('p')
|
|
textInTitle.appendChild(document.createTextNode('Comic Downloader'))
|
|
titleElement.appendChild(textInTitle)
|
|
boxSubmissionTitle.appendChild(titleElement)
|
|
boxSubmissionIdSubContainer.appendChild(boxSubmissionTitle)
|
|
boxSubmissionIdContainer.appendChild(boxSubmissionIdSubContainer)
|
|
boxHeader.appendChild(boxSubmissionIdContainer)
|
|
|
|
let boxBody = document.createElement('div')
|
|
boxBody.classList.add('section-body')
|
|
let text = document.createElement('p')
|
|
text.appendChild(document.createTextNode('Starting at current page currently not working'))
|
|
let startCurrentPageButton = document.createElement('button')
|
|
startCurrentPageButton.addEventListener('click', downloadStartCurrentSite)
|
|
startCurrentPageButton.appendChild(document.createTextNode('Download comic starting at current page'))
|
|
let startBeginningButton = document.createElement('button')
|
|
startBeginningButton.addEventListener('click', downloadStartBeginning)
|
|
startBeginningButton.appendChild(document.createTextNode('Download full comic'))
|
|
boxBody.appendChild(text)
|
|
boxBody.appendChild(startCurrentPageButton)
|
|
boxBody.appendChild(startBeginningButton)
|
|
|
|
comicSection.appendChild(boxHeader)
|
|
comicSection.appendChild(boxBody)
|
|
return comicSection
|
|
}
|
|
|
|
function createBox() {
|
|
let comicSection = createHTMLforComicBox()
|
|
let submissionContent = document.getElementsByClassName('submission-content')[0]
|
|
let position = 0;
|
|
for(let index = 0; index < submissionContent.childElementCount; index++) {
|
|
if(submissionContent.children[index].tagName.toLowerCase() === 'section') {
|
|
position = index
|
|
}
|
|
}
|
|
submissionContent.insertBefore(comicSection, submissionContent.children[position])
|
|
}
|
|
|
|
createBox() |