Black HyRead

Download hyread ebook

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Black HyRead
// @namespace    Yr
// @version      1.0
// @description  Download hyread ebook
// @author       toudaimori
// @match        https://service.ebook.hyread.com.tw/ebookservice/epubreader/hyread/v3/reader.jsp
// @require  https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js
// @require  https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.0/jszip.js
// @require  https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js
// @grant    GM.xmlHttpRequest
// ==/UserScript==

var container = []

async function Download(event)
{
    event.stopPropagation()

    const realContainer = []
    for(let i = 0; i < container.length; ++i){
        console.log(i, container[i])
        realContainer.push(await getImageBase64(i, container[i]))
    }

    Compress('Download', realContainer)
}

function Compress(title, pics)
{
    return new Promise((resolve, reject) => {
        console.log(`Start Compress`)
        const zip = new JSZip()
        const folder = zip.folder(`${title}`);
        for(let i = 0; i < pics.length; ++i){
            folder.file(`${pics[i].index}.jpg`, pics[i].base64, {base64: true})
        }

        zip.generateAsync({type:"blob", streamFiles: true}, metadata => {
            console.log(`Compress Progress = ${metadata.percent} %`)
        })
        .then((content) => {
            // see FileSaver.js
            console.log(`All Done, Save to ${title}.zip`);
            resolve(saveAs(content, `${title}.zip`))
        });
    })
}

async function processSinglePage(){
    const pages = [...document.querySelectorAll('.hyr-hejview__content')].map(x => x.style['background-image'])
    pages.filter(x => x.match(/(blob.*)\"/)).map(x => container.push(x.match(/(blob.*)\"/)[1]))

    container = [...new Set(container)]
    document.title = `HyRead (${container.length})`
}

function getImageBase64(index, path)
{
    return new Promise((resolve, reject) => {
        const extension = path.substring(path.lastIndexOf('.') + 1)
        GM.xmlHttpRequest({
            method: "GET",
            url: path,
            overrideMimeType: 'text/plain; charset=x-user-defined',
            onload: response => {
                let binary = "";
                const responseText = response.responseText;
                const responseTextLen = responseText.length;
                for ( let i = 0; i < responseTextLen; i++ ) {
                    binary += String.fromCharCode(responseText.charCodeAt(i) & 255)
                }

                // Note there is no 'data:image/jpeg;base64,' Due to JSZip
                let src = btoa(binary)

                console.log(`Downloaded: ${index}.${extension}, src=${path}`)
                resolve({'index': index, 'base64': src, 'extension': extension })
            }
        })
    })
}

const btn = $(`<a id="yrBtn" style="position: absolute;top: 50px;left: 10px;">下載</a>`)

$('body').append(btn)

btn.on('click', event => {
    console.log('click')
    Download(event)
})

setInterval(processSinglePage, 500)