Greasy Fork

文泉学堂JPG下载

try to take over the world!

当前为 2020-02-13 提交的版本,查看 最新版本

// ==UserScript==
// @name         文泉学堂JPG下载
// @namespace    https://52pojie.cn
// @version      0.54
// @description  try to take over the world!
// @author       Culaccino
// @match        https://*.wqxuetang.com/read/pdf/*
// @grant        none
// ==/UserScript==

(function() {
    var downloadNum = 0
    var imgBox, nowPage, allPage, doc, size, name, startNum = 1, isStart = false, pageList = [],beginTime = new Date()
    const baseURL = `https://${window.location.host}/`
    if(baseURL.indexOf("www") > -1){window.location.href=window.location.href.replace("www","lib-nuanxin")}
    Array.prototype.remove = function(val) {
        const index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    }
    function print(){console.log(...arguments)}

    function createList(now){
        let arr = []
        for(let i = now || nowPage;i < Math.min((now || nowPage) + downloadNum, allPage + 1); i++) arr.push(i)
        startNum = arr[0]
        return arr
    }

    async function autoScroll(num){
        print(pageList[num],num,pageList.length)
        document.documentElement.scrollTop = imgBox[pageList[num]].offsetTop
        var ys = setInterval(function(){
          var h = imgBox[pageList[num]].style.height;
          if(h=='auto'){
            let src = imgBox[pageList[num]].firstChild.getAttribute("src")
            var a = document.createElement('a')
            var event = new MouseEvent('click')
            a.download = pageList[num] + ".jpg"
            a.href = src
            a.dispatchEvent(event)
            clearInterval(ys);
            autoScroll(num += 1);
          }
        },3000)
    }

    window.onload = function(){
        document.getElementById("pagebox").onclick = function(){
            if(!isStart){
                const numBox = document.getElementsByClassName("page-head-tol")[0].innerHTML
                imgBox = document.getElementsByClassName("page-img-box")
                nowPage = parseInt(numBox.slice(0, numBox.indexOf("/") - 1))
                allPage = imgBox.length - 1
                downloadNum = allPage
                isStart = !isStart
                pageList = createList()
                autoScroll(0)
            }else{
                return
            }
        }
    }
})();