第一版主小说阅读

阅读小说时增强功能:1. 自动预读下一页;

当前为 2024-04-06 提交的版本,查看 最新版本

// ==UserScript==
// @name         第一版主小说阅读
// @namespace    https://diyibanzhu.org/
// @version      0.1
// @description  阅读小说时增强功能:1. 自动预读下一页;
// @author       Essence
// @match        https://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=diyibanzhu.org
// @run-at       document-end
// @license MIT
// ==/UserScript==


(function() {
    'use strict';

    // Your code here...

    // 依次找到"下一页"的元素:优先“下一页”,其次“下一章”
    const nextLink = document.querySelector("a.curr")?.nextElementSibling || document.querySelector("a.next");

    console.log("脚本将预加载下一页", nextLink, nextLink?.href);

    // If we found a matching link
    if (nextLink && nextLink.href) {
        console.log("脚本开始预加载下一页", nextLink, nextLink?.href);

        // 通过"link prefetching"实现预加载下一页
        const link = document.createElement("link");

        // 注意:最后第一版主网站的章中最后的一页的 URL 的 href 是以"javascript:"开头,其它页是正常的 URL
        let href = nextLink.href;
        if(href.startsWith("javascript:")){
            const params = href.match(/\d+/g)
            href = "/" + params.slice(0, -1).join("/") + "_" + params.slice(-1) + ".html";
        }

        link.href = href;
        link.rel = "prefetch";
        document.head.appendChild(link);
    }
})();