一键批量加载页面[EX绅士][exhentai]

一键批量加载所有后续页面内容

Od 11.07.2020.. Pogledajte najnovija verzija.

// ==UserScript==
// @name        一键批量加载页面[EX绅士][exhentai]
// @namespace   exhentaigetallpage
// @description 一键批量加载所有后续页面内容
// @include     https://exhentai.org/tag/*
// @include     https://exhentai.org/*f_search*
// @include     https://exhentai.org/g/*
// @version     0.1.0
// @require     https://cdn.jsdelivr.net/npm/zepto/dist/zepto.min.js
// @grant       none
// ==/UserScript==

// 一次性加载的页数
const baseNum = 100;
// 已下载次数
var downTimes = 0;
// 获取第一个下一页URL
var nextPageUrl = setNextPageUrl($('table.ptt td:last-child a').attr('href'))
// 加载按钮
setBtn()
// 绑定按钮行为
$('.onekey-get-all-page').on('click', function () {
    setPageData()
    downTimes = 0
})

// 设置操作按钮
function setBtn() {
    var eqNum = -1;
    var allPageNum = $('table.ptt td a').eq(eqNum).text()
    while (!parseInt(allPageNum)) {
        --eqNum
        allPageNum = $('table.ptt td a').eq(eqNum).text()
    }
    var btnHtml = '<div class="show-info"><div class="onekey-get-all-page">一键加载后续' + baseNum + '个页面</div><div class="onekey-get-all-page-info">当前已加载 <span class="page-now">' + $('.ptds a').eq(0).text() + '</span> / <span class="page-all">' + allPageNum + '</span></div></div>'
    var cssHtml = '<style>\
        .show-info{\
            position: fixed;\
            top:10px;\
            left:10px;\
            z-index: 5000;\
            opacity: 0.3;\
            -webkit-user-select:none;\
            -moz-user-select:none;\
            -ms-user-select:none;\
            user-select:none;\
        }\
        .show-info:hover{\
            opacity:1;\
        }\
        .onekey-get-all-page{\
            min-width:120px;\
            height:30px;\
            line-height:30px;\
            color:#EEEEEE;\
            background: #4f535b;\
            border: 1px solid #000000;\
            text-align: center;\
            font-size:14px;\
            cursor:pointer;\
            box-sizing: border-box;\
            padding: 0 6px;\
        }\
        .onekey-get-all-page-info{\
            min-width:120px;\
            height:30px;\
            line-height:30px;\
            color:#EEEEEE;\
            background: #4f535b;\
            border: 1px solid #000000;\
            text-align: center;\
            font-size:14px;\
            box-sizing: border-box;\
            padding: 0 6px;\
            margin-top: 10px;\
            cursor: default;\
        }\
    </style>'

    $('body').before(btnHtml + cssHtml)
}

// 设置下一页的数据
function setPageData() {
    if (stopDown()) {
        return false
    }
    downTimes++
    if (downTimes === baseNum) {
        return false;
    }

    webApi(nextPageUrl, function (response) {
        nextPageUrl = getNextPageUrl(response)
        pageData = getPageData(response)
        if (pageData) {
            if ($('.itg.gld').length) {
                $('.itg.gld').append(pageData)
            }
            if ($('#gdt').length) {
                $('#gdt').children('.c').before(pageData)
            }
            var nowpage = getPageNum(response)
            $('.onekey-get-all-page-info .page-now').text(nowpage)
        }
        if (nextPageUrl != '') {
            nextPageUrl = setNextPageUrl(nextPageUrl)
            setPageData()
        }
    })
}

// 获取页面页码
function getPageNum(htmlText) {
    var pageNumObj = htmlText.match(/<td class="ptds">.*?\/td>/)
    pageNumObj = $(pageNumObj[0])
    var pageNum = pageNumObj.children('a').text()
    if (pageNum) {
        return pageNum
    } else {
        return '0'
    }
}

// 获取下一页URL
function getNextPageUrl(htmlText) {
    var nextPageObj = htmlText.match(/<table class="ptt".*?\/table>/)
    nextPageObj = $(nextPageObj[0])
    var nextPageUrl = nextPageObj.find('td:last-child a').attr('href')
    if (nextPageUrl) {
        return nextPageUrl
    } else {
        return ''
    }
}

// 获取页面列表数据
function getPageData(htmlText) {
    var pageDataObj1 = htmlText.match(/(<div class="itg gld">.*)<table class="ptb"/)
    var pageDataObj2 = htmlText.match(/(<div class="gdtl".*?<\/a><\/div>)<div class="c"><\/div>/)

    var pageDataObj = null
    if (pageDataObj1) {
        pageDataObj = $(pageDataObj1[0]).children('.gl1t')
    }
    if (pageDataObj2) {
        pageDataObj = $(pageDataObj2[0])
    }
    return pageDataObj
}

// 网络请求
function webApi(api, callback) {
    $.get(api, function (response) {
        callback(response);
    })
}

// 停止下载页面
function stopDown() {
    var nowGetPage = $('.onekey-get-all-page-info .page-now').text()
    nowGetPage = parseInt(nowGetPage)
    var allGetPage = $('.onekey-get-all-page-info .page-all').text()
    allGetPage = parseInt(allGetPage)

    // 已经采集完毕,禁止继续采集
    if (nowGetPage >= allGetPage) {
        return true
    }
    return false
}

// 获取下一页链接
function setNextPageUrl(urlOrgin) {
    if (urlOrgin) {
        if (urlOrgin.split('from')) {
            urlOrgin = urlOrgin.split('from')
            urlOrgin = urlOrgin[0]
        } else {
            urlOrgin = ''
        }
    } else {
        urlOrgin = ''
    }
    return urlOrgin
}