bk

用于下载bk的书籍图片,下载下来之后需要自己用pdf软件合并

目前为 2024-02-16 提交的版本。查看 最新版本

// ==UserScript==
// @name         bk
// @version      0.3
// @author       Fructose
// @match        https://wk5.bookan.com.cn/*
// @grant        GM_addStyle
// @require      https://code.jquery.com/jquery-3.1.0.js
// @license      MIT
// @description  用于下载bk的书籍图片,下载下来之后需要自己用pdf软件合并
// @namespace https://greasyfork.org/users/1261551
// ==/UserScript==
'use strict';

/*用于页面添加下载按钮*/
GM_addStyle(`
            #downloadMain { position: fixed; z-index: 999; right: 0; bottom: 20px; background: #fff; }
            #downloadMain-bd { box-sizing: border-box; height: 60px; padding: 10px; border: 1px solid #ccc; }
            #downloadMainBtn { font-size: 13px; line-height: 39px; display: inline-block; width: 60px; height: 40px; margin-left: -5px; text-align: center; text-decoration: none; color: #fff; background: #4d90fe; }
            #zizhuPopupMainInput { display: inline-block; box-sizing: border-box; width: 210px; height: 40px; padding: 0 10px; border: 1px solid #ccc; outline: none; }
    `)
// 添加内容
var smallCnt =
    `<div id="downloadMain">
            <input type="text" name="" placeholder="与真实页面的偏移量,没啥用填0" id="zizhuPopupMainInput" />
			<a href="javascript:void(0);" id="downloadMainBtn">下载</a>
		</div>`
        // 添加到 body
    var odom = document.createElement("div");
odom.id = "downloadMain";
odom.innerHTML = smallCnt;
document.body.appendChild(odom);



/* 这是一个封装好的下载文件的函数 */
const downFile = (url, name, type) => {//在这里传进来的url必须是https,type为后缀,可以是jpg\pdf
    // 发送http请求,将文件链接转换成文件流
    fileAjax(url, function (xhr) {
        downloadFile(xhr.response, name.concat(type))
    }, {
        responseType: 'blob'
    })

    function fileAjax(url, callback, options) {
        let xhr = new XMLHttpRequest()
        xhr.open('get', url, true)
        if (options.responseType) {
            xhr.responseType = options.responseType
        }
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                callback(xhr)
            }
        }
        xhr.send()
    }

    function downloadFile(content, filename) {
        window.URL = window.URL || window.webkitURL
        let a = document.createElement('a')
        let blob = new Blob([content])
        // 通过二进制文件创建url
        let url = window.URL.createObjectURL(blob)
        a.href = url
        a.download = filename
        a.click()
        // 销毁创建的url
        window.URL.revokeObjectURL(url)
    }
}

/*实现sleep函数,减慢速度*/
const sleep = (time) => {
    return new Promise(resolve => setTimeout(resolve, time))
}
/*将http链接追加上https */
const http2https = (http) => {
    return http.slice(0, 4).concat("s", http.slice(4))
}


/*以下为针对本页面的逻辑 */
/*以下为针对本页面获取 提取该页面的图片地址、页数(不是页码)、#id*/
const page = (type) => {
    switch (type) {
        case "src": return document.querySelector("img").src; break;
        case "num": return document.querySelector("img").getAttribute("id").substr(6); break; //获取到id后用substr取第6个字符以后的内容
        case "id": return document.querySelector("img").id; break;
    }

}

const imgID = document.querySelector("img");
const downloadStart = async() => {
    const offset = new Number(document.getElementById("zizhuPopupMainInput").value);//页码和页数不同,在这里设置一个偏移量(从页面的input框中获取的)
    for (var i = page("num"); i >= 0; i--) { //根据获取到的当前页码数循环
        console.log("第" + String(page("num") - offset) + "页") //打印当前下载的页码(不是页数)是多少
        console.log(page("src"))//打印图片http地址,下载的时候复制下来所有的src,然后批量下载改名合并就可以了
        await sleep(1000)
        downFile(http2https(page("src")), String(page("num") - offset), ".jpg")
        await sleep(3000)
        document.querySelector("div[class='page-wrapper']").click()
        await sleep(4000)
    }
}
document.getElementById('downloadMainBtn').addEventListener('click', downloadStart);//监听按钮是否被点击,如果点击则开始下载