Greasy Fork

New MangaDex Follows

Manage your follows

目前为 2021-08-21 提交的版本。查看 最新版本

// ==UserScript==
// @name         New MangaDex Follows
// @namespace    https://greasyfork.org/scripts/430295-new-mangadex-follows
// @version      1.0.2
// @description  Manage your follows
// @author       Australis
// @match        https://mangadex.org/*
// @icon         https://www.google.com/s2/favicons?domain=mangadex.org
// @grant        none
// ==/UserScript==
if(localStorage.getItem("seriesdex") == null) seriesdex = []
else seriesdex = JSON.parse(localStorage.getItem("seriesdex"))

function FS(array, number){//FindSeries
    return array.findIndex(q => q.id == number)
}

button_reload = 0
disURL = ""

function NextNext(array,pointer,next){
    if(next[0] < array[pointer].nextc*1 && array[pointer].last < next[0]) {
        array[pointer].nextc = next[0]
        array[pointer].nextl = next[1]
    }
}

function Reload1(){
    elem_no = follows.length
    if(before < elem_no) button_reload = setTimeout(Reload1,4000)
    else repeat = setTimeout(FollowsFeed,2000)
    before = follows.length
}

function Reload(){
    if(lista.length == 0) button_reload = setTimeout(Reload,1000)
    else repeat = setTimeout(FollowsFeed,2000)
}

function FollowsFeed(){
    try{
        console.log("FollowsFeed")
        lista = document.getElementsByClassName("mb-12")[0].getElementsByClassName("mb-4")
        console.log("lista loaded")
        try{
            clearInterval(button_reload)
        }
        catch(e){
            console.log("no reload")
        }
        try{
            clearInterval(repeat)
        }
        catch(e){
            console.log("no repeat")
        }

        for(let l of lista){
            if(!l.className.includes("my-6") && l.style.display != "none") {
                try{
                    var id_series = l.getElementsByTagName("a")[0].href.split("title/")[1]
                    var batch = l.getElementsByClassName("chapter-list__section flex-grow")
                    var chap, link, read, nextc, nextl, last
                    nextc = 10000
                    nextl = ""
                    pseries = FS(seriesdex,id_series)
                    if(pseries < 0) last = -10
                    else last = seriesdex[pseries].last
                    chap = JSON.parse(batch[0].getElementsByTagName("a")[0].innerText.split(" ")[1])
                    link = batch[0].getElementsByTagName("a")[0].href.split("chapter/")[1]
                    read = (batch[0].getElementsByClassName("chapter-list read dot").length != 0)

                    if(!read && last < chap) {
                        nextc = chap
                        nextl = link
                    }
                    else if(last < chap){
                        if(read) last = chap
                    }

                    if(batch.length > 1){
                        for(let i=1; i<batch.length; i++){
                            var current = batch[i].firstChild.innerText.split(" ")[1]*1
                            if(batch[i].getElementsByClassName("chapter-list read dot").length == 0 && current < nextc){
                                if(last < current && nextc > last && nextc < current){//arreglar?
                                    nextc = current
                                    nextl = batch[i].getElementsByTagName("a")[0].href.split("chapter/")[1]
                                }
                            }
                            else if(batch[i].getElementsByClassName("chapter-list read dot").length != 0){
                                if(last < current) last = current
                            }
                        }
                    }

                    if(pseries >= 0) NextNext(seriesdex,pseries,[nextc,nextl])
                    else seriesdex.push({id:id_series, name:l.firstChild.innerText, last:last, nextc:nextc, nextl:nextl})

                    index = FS(follows,id_series)
                    if(index >= 0){
                        if(seriesdex[pseries].last < last || seriesdex[pseries].last == null) seriesdex[pseries].last = last
                        if(nextc > last){
                            NextNext(follows,index,[nextc,nextl])
                            NextNext(seriesdex,pseries,[nextc,nextl])
                        }
                        if(l != follows[index].pointer) {
                            while(l.getElementsByClassName("chapter-feed__chapters-list")[0] != undefined) follows[index].pointer.getElementsByClassName("chapter-feed__chapters")[0].append(l.getElementsByClassName("chapter-feed__chapters-list")[0])
                        }
                        //else console.log("new?")
                    }
                    else{
                        follows.push({id:id_series, ch:last, url:link, nextc:nextc, nextl:nextl, pointer:l})
                    }

                    // if(pseries >= 0){
                    if(pseries == -1) pseries = FS(seriesdex,id_series)
                    if(seriesdex[pseries].nextc == 0 || seriesdex[pseries].nextc == null || seriesdex[pseries].nextc == 10000 || (seriesdex[pseries].nextc > nextc && seriesdex[pseries].last < nextc)){
                        seriesdex[pseries].nextc = nextc
                        seriesdex[pseries].nextl = nextl
                    }
                    // }
                }
                catch(e){
                    console.log(e)
                    //  console.log(l)
                }
            }
        }
        for(let f of follows){
            let avail = f.pointer.getElementsByClassName("chapter-feed__chapters-list")
            let lastest = -10
            let newest = 10000
            let link = null
            let current = FS(seriesdex,f.id)
            if(current >= 0){
                lastest = seriesdex[current].last
                newest = seriesdex[current].nextc
            }
            for(let a of avail){
                let disONE = a.getElementsByTagName("a")[0].innerText.split(" ")[1]*1
                let unread = (a.getElementsByClassName("read").length == 0)
                if(disONE > lastest && !unread) lastest = disONE
                if(unread && newest > disONE && disONE > lastest) {
                    newest = disONE
                    link = a.getElementsByTagName("a")[0].href.split("chapter/")[1]
                }
            }
            // console.log(f.pointer.getElementsByTagName("h6")[0].innerText+" last: "+lastest+" nextc: "+newest)
            f.ch = lastest
            if(newest > lastest) {
                f.nextc = newest
                f.nextl = link
            }
            else{
                f.nextc = 10000
                f.nextl = null
            }
            if(lastest > seriesdex[current].last) {
                seriesdex[current].last = lastest
                // seriesdex[current].nextc = f.nextc
                // seriesdex[current].nextl = f.nextl
            }
            else{//lastest <= seriesdex[current].last
                if(seriesdex[current].nextc != f.nextc){
                    seriesdex[current].nextc = f.nextc
                    seriesdex[current].nextl = f.nextl
                }
            }
        }
        for(let l of lista){
            if(l.firstChild.getElementsByTagName("a")[0]){
                let daONE = seriesdex[FS(seriesdex,l.firstChild.getElementsByTagName("a")[0].href.split("title/")[1])]
                if(l.getElementsByClassName("chapter-feed__chapters-list").length == 0) l.style.display = "none"
                else if(daONE.nextc > daONE.last && daONE.nextl){
                    let newbutton = document.createElement("div")
                    newbutton.className = "chapter-feed__chapters-list"
                    newbutton.innerHTML = "<button class=\"continue\"><a href=\"/chapter/"+daONE.nextl+"\">CONTINUE to Chapter "+daONE.nextc+"</button>"
                    if(l.getElementsByClassName("continue").length == 0) l.getElementsByClassName("chapter-feed__chapters")[0].insertBefore(newbutton,l.getElementsByClassName("chapter-feed__chapters-list")[0])
                    else l.getElementsByClassName("continue")[0].innerHTML = newbutton.innerHTML
                }
                if((daONE.nextl == null || daONE.nextl == "") && l.getElementsByClassName("continue")[0] != undefined) l.getElementsByClassName("continue")[0].parentElement.remove()
            }
        }

        for(let n=follows.length-1; n >= 0; n--){
            if(follows[n].pointer.getElementsByClassName("continue")[0] != undefined) lista[0].parentElement.insertBefore(follows[n].pointer,lista[0])
        }
        localStorage.setItem("seriesdex",JSON.stringify(seriesdex))
    }
    catch(e){
        console.log("error FollowsFeed")
        console.log(e)
        repeat = setTimeout(FollowsFeed,5000)
    }
}

function Clickear(){
    hola = document.getElementsByClassName("v-window-item")[0]
    try{
        boton = hola.getElementsByClassName("v-btn--block")[0]
        if(boton != undefined){
            boton.click()
            setTimeout(Clickear,2500)
        }
        else{
            FillTitles()
        }
    }
    catch(e){
        setTimeout(Clickear,2500)
    }
}

function Llenar(X){
    let temp = []
    console.log("now filling "+X.firstChild.firstChild.firstChild.innerText)
    for(let t of X.firstChild.lastChild.children){
        var id = t.firstChild.firstChild.href.split("title/")[1]
        var name = t.getElementsByTagName("h4")[0].innerText.replaceAll("\n          ","").replaceAll("\n        ","")
        temp.push({id:id, name:name, last:null, nextc:null, nextl:null })
        if(temp.length == 0) temp = [{id:id, name:name, last:null, nextc:null, nextl:null}]
    }
    console.log("Done Filling!!")
    return temp
}

function NewButton(){
    try{
        newb = document.createElement("button")
        newb.innerHTML = "<button data-v-621772ff=\"\" type=\"button\" class=\"v-btn v-btn--is-elevated v-btn--has-bg theme--dark v-size--default\"><span class=\"v-btn__content\"><span data-v-621772ff=\"\" aria-hidden=\"true\" class=\"v-icon notranslate theme--dark\" onclick=\"Fill()\"><a>FILL</a></span></span></button>"
        document.getElementsByClassName("v-btn-toggle v-btn-toggle--borderless")[0].appendChild(newb)
    }
    catch(e){
        setTimeout(NewButton,500)
    }
}

function FillTitles(){
    try{
        titles = document.getElementsByClassName("v-window-item v-window-item--active")[0]
        if(titles != undefined){
            console.log("Filling Titles")
            for(t of titles.children){
                if(t.innerHTML != "<!---->"){
                    if(t.firstChild.firstChild.firstChild.innerText == "Reading") reading = Llenar(t)
                    if(t.firstChild.firstChild.firstChild.innerText == "On Hold") onhold = Llenar(t)
                    if(t.firstChild.firstChild.firstChild.innerText == "Plan To Read") planto = Llenar(t)
                    if(t.firstChild.firstChild.firstChild.innerText == "Dropped") dropped = Llenar(t)
                    if(t.firstChild.firstChild.firstChild.innerText == "Completed") completed = Llenar(t)
                }
            }
            for(let r of reading){
                if(FS(seriesdex,r.id) == -1) seriesdex.push(r)
            }

            function Copiar(arrA,argB){
                if(FS(arrA, argB.id) >= 0){
                    arrA[FS(arrA, argB.id)].last = argB.last
                }
            }

            for(let i = seriesdex.length-1; i >= 0; i--){
                Copiar(completed, seriesdex[i])
                Copiar(onhold, seriesdex[i])
                Copiar(reading, seriesdex[i])
                Copiar(dropped, seriesdex[i])
                //Copiar(planto, seriesdex[i])
            }
            localStorage.setItem("reading",JSON.stringify(reading))
            localStorage.setItem("planto",JSON.stringify(planto))
            localStorage.setItem("onhold",JSON.stringify(onhold))
            localStorage.setItem("dropped",JSON.stringify(dropped))
            localStorage.setItem("completed",JSON.stringify(completed))
            alert("Done!")
        }
    }
    catch(e){
        console.log(e)
        // setTimeout(FillTitles, 5500)
    }
}

function main(){
    if(document.URL == "https://mangadex.org/user/me"){
        console.log("Script ready")

        document.Fill = function(){
            console.log("Loading series...")
            alert("Please wait until the next alert, this can take several minutes depending of how many series you follow.")
            Clickear()
        }

        if(document.getElementsByClassName("user__tab-active")[0].innerText == "Follows"){
            NewButton()
        }
    }

    if(document.URL.includes("https://mangadex.org/titles/feed")){
        follows = []
        elem_no = 0
        before = 0

        FollowsFeed()
        repeat = setTimeout(FollowsFeed,12500)
    }

}

function Loop(){
    try{
        if(disURL != document.URL){
            console.log("loop")
            repeat = setTimeout(main,5000)
            disURL = document.URL
        }
    }
    catch(e){
        console.log("error loop")
    }
    frutyloop = setTimeout(Loop,1000)
}

frutyloop = setTimeout(Loop,1000)