Greasy Fork

Greasy Fork is available in English.

笔趣阁外观优化

专注阅读

当前为 2021-11-01 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         笔趣阁外观优化
// @namespace    https://gitee.com/linhq1999/OhMyScript
// @version      1.7
// @description  专注阅读
// @author       LinHQ
// @match        https://www.shuquge.com/txt/*
// @match        https://www.sywx8.com/*
// @grant GM_addStyle
// @inject-into auto
// ==/UserScript==
'use strict';
(function () {
    var configs = {
        "shuquge": {
            "main": "div.reader",
            "title": ".reader h1",
            "txt": "#content",
            "filter": ["div.header", "div.nav", "div.link"],
            "txtfilter": ["shuqu"] /*带有此关键字的行将被删除*/
        },
        "sywx": {
            "main": "div#container",
            "title": "div>h1",
            "txt": "div#BookText",
            "filter": ["div.top", ".link.xb", "#footer"],
            "txtfilter": ["最快更新", "松语"] /*带有此关键字的行将被删除*/
        },
        "style": "\n            body {\n                background-color: #EAEAEF !important;\n            }\n\n            .bqg.inject.win {\n                width: 50% !important;\n                min-width: 600px;\n                border: 2px double gray !important;\n                border-radius: 8px;\n            }\n\n            .bqg.inject.txt {\n                font-family: '\u6977\u4F53'!important;\n                background-color: #EAEAEF !important;\n                padding: 0 10px 0 20px !important;\n            }\n\n            .bqg.inject.title {\n                color: black;\n                background-color: #EAEAEF;\n                font-family: '\u6977\u4F53';\n            }"
    };
    var lineHeight = 1.3;
    // 查询已经保存的字体信息
    var oldFontCfg = localStorage.getItem("bqg_cfg");
    var fontcfg = (oldFontCfg === null) ? {
        "fontSize": 16,
        "lineHeight": 16 * lineHeight,
        "viewMode": "unset"
    } : JSON.parse(oldFontCfg);
    // 检测当前的网址,应用对应的设置
    var cfg;
    if (document.URL.includes("shuqu")) {
        cfg = configs.shuquge;
    }
    else {
        cfg = configs.sywx;
    }
    GM_addStyle(configs.style);
    var doInject = function () {
        var _a, _b;
        // 执行过滤
        cfg.filter.forEach(function (filter) { var _a; return (_a = document.querySelectorAll(filter)) === null || _a === void 0 ? void 0 : _a.forEach(function (ele) { return ele.remove(); }); });
        var textWin = document.querySelector(cfg.txt);
        textWin.style.fontSize = fontcfg.fontSize + "px";
        textWin.style.lineHeight = fontcfg.lineHeight + "px";
        textWin.classList.add("bqg", "inject", "txt");
        // 执行文字过滤
        if (cfg.txtfilter !== undefined) {
            textWin.innerText = (_b = (_a = textWin.innerText) === null || _a === void 0 ? void 0 : _a.split("\n\n")) === null || _b === void 0 ? void 0 : _b.filter(function (line) {
                for (var _i = 0, _a = cfg.txtfilter; _i < _a.length; _i++) {
                    var key = _a[_i];
                    if (line.includes(key)) {
                        return false;
                    }
                }
                return true;
            }).join("\n\n");
        }
        var saveFontCfg = function () {
            fontcfg.lineHeight = fontcfg.fontSize * lineHeight;
            textWin.style.lineHeight = fontcfg.lineHeight + "px";
            localStorage.setItem("bqg_cfg", JSON.stringify(fontcfg));
        };
        var mainWin = document.querySelector(cfg.main);
        mainWin.classList.add("bqg", "inject", "win");
        // 旧版本过渡
        if (fontcfg.viewMode === undefined)
            fontcfg.viewMode = "90vh";
        // 切换视图
        mainWin.style.height = fontcfg.viewMode;
        // 检测滚动主体,不同的视图滚动主体不一样
        var scroller = (fontcfg.viewMode.includes("vh")) ? mainWin : window;
        // 检测滚动比例
        var modifier = (fontcfg.viewMode.includes("vh")) ? 0.9 : 1;
        var title = document.querySelector(cfg.title);
        title.classList.add("bqg", "inject", "title");
        document.onclick = function (ev) {
            // 下半屏单击下滚,反之上滚
            var direct = (ev.clientY - window.innerHeight / 2 > 0) ? 1 : -1;
            scroller.scrollBy({ top: (window.innerHeight * modifier - fontcfg.fontSize * 2) * direct });
        };
        document.body.onkeydown = function (ev) {
            var _a, _b;
            switch (ev.key) {
                case "-":
                    fontcfg.fontSize -= 2;
                    textWin.style.fontSize = fontcfg.fontSize + "px";
                    saveFontCfg();
                    break;
                case "=":
                    fontcfg.fontSize += 2;
                    textWin.style.fontSize = fontcfg.fontSize + "px";
                    saveFontCfg();
                    break;
                case "o":
                    fontcfg.viewMode = (fontcfg.viewMode.includes("vh")) ? "unset" : "90vh";
                    scroller = (fontcfg.viewMode.includes("vh")) ? mainWin : window;
                    mainWin.style.height = fontcfg.viewMode;
                    modifier = (fontcfg.viewMode.includes("vh")) ? 0.9 : 1;
                    saveFontCfg();
                    break;
                case "j":
                    scroller.scrollBy({ top: window.innerHeight * modifier - fontcfg.lineHeight });
                    break;
                case "k":
                    scroller.scrollBy({ top: window.innerHeight * -1 * modifier + fontcfg.lineHeight });
                    break;
                case "h":
                    var prevs = document.querySelectorAll("a");
                    for (var _i = 0, prevs_1 = prevs; _i < prevs_1.length; _i++) {
                        var prev = prevs_1[_i];
                        if ((_a = prev.textContent) === null || _a === void 0 ? void 0 : _a.includes("上一")) {
                            prev.click();
                            break;
                        }
                    }
                    break;
                case "l":
                    var nexts = document.querySelectorAll("a");
                    for (var _c = 0, nexts_1 = nexts; _c < nexts_1.length; _c++) {
                        var next = nexts_1[_c];
                        if ((_b = next.textContent) === null || _b === void 0 ? void 0 : _b.includes("下一")) {
                            next.click();
                            break;
                        }
                    }
                    break;
                default:
                    break;
            }
        };
    };
    // 先调用一次,后面是有变化时才会触发,避免有时无法起作用
    doInject();
    // 强力覆盖
    new MutationObserver(function (_, ob) {
        doInject();
    }).observe(document.body, { childList: true });
})();