Greasy Fork

Greasy Fork is available in English.

微信读书沉浸式阅读主题

微信读书上拉显示头部和侧栏,下拉隐藏,主题切换和侧边栏开关按钮,顶部毛玻璃效果,支持默认字体但允许页面切换

当前为 2025-03-20 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         微信读书沉浸式阅读主题
// @version      1.2.2
// @description  微信读书上拉显示头部和侧栏,下拉隐藏,主题切换和侧边栏开关按钮,顶部毛玻璃效果,支持默认字体但允许页面切换
// @icon         https://i.miji.bid/2025/03/15/560664f99070e139e28703cf92975c73.jpeg
// @author       Grok
// @match        https://weread.qq.com/web/reader/*
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// @grant        GM_setValue
// @grant        GM_getValue
// @license      MIT
// @namespace    http://github.com/lossj
// ==/UserScript==

(function() {
    'use strict';

    // 定义主题对象,可编辑主题名称和对应的背景色、字体颜色
    const themes = {
        // 白天主题(第一行,6个)
        '极光灰': { background: '#E0E0E0', fontColor: '#222222' },
        '浅咖色': { background: '#FAEBD7', fontColor: '#222222' },
        '米黄色': { background: '#F5F5DC', fontColor: '#332222' },
        '豆沙绿': { background: '#E6F5E6', fontColor: '#223322' },
        '海天蓝': { background: '#EBF5FF', fontColor: '#444444' },
        '晨曦白': { background: '#F8F8F8', fontColor: '#333333' },

        // 黑夜主题(第二行,6个,命名统一为三个字)
        '夜空黑': { background: '#1A1A2E', fontColor: '#E5E5E5' },
        '星辰黑': { background: '#2D2D2D', fontColor: '#F0F0F0' },
        '幽暗黑': { background: '#121212', fontColor: '#FFFFFF' },
        '月影黑': { background: '#2E3440', fontColor: '#D8DEE9' },
        '深林黑': { background: '#1C2526', fontColor: '#B0BEC5' },
        '静夜黑': { background: '#0F1419', fontColor: '#E0E0E0' }
    };

    // 获取或设置当前主题、侧边栏状态和页面宽度,保存在本地存储中
    let currentTheme = GM_getValue('currentTheme', '豆沙绿'); // 默认主题为“豆沙绿”
    let hideSidebarOnScrollDown = GM_getValue('hideSidebarOnScrollDown', false); // 默认侧边栏不随滚动隐藏
    let pageWidth = GM_getValue('pageWidth', 1200); // 默认页面宽度为1200px

    // 自定义样式,包含所有界面元素的样式调整
    const customStyle = `
        /* 阅读内容区域的样式 */
        .readerContent, .app_content, .readerChapterContent {
            font-family: "霞鹜文楷", "PingFang SC", "宋体"; /* 设置默认字体,但不使用 !important */
            font-size: 18px !important;
            color: ${themes[currentTheme].fontColor} !important;
            background-color: ${themes[currentTheme].background} !important;
            max-width: ${pageWidth}px !important;
            margin: 0 auto !important;
            padding: 20px !important;
        }

        /* 章节内容的子元素样式,继承父元素 */
        .readerChapterContent p, .readerChapterContent div, .readerChapterContent span {
            font-family: inherit; /* 继承父元素的字体 */
            font-size: inherit !important;
            color: inherit !important;
        }

        /* 页面整体背景 */
        body, html {
            background-color: ${themes[currentTheme].background} !important;
        }

        /* 顶部导航栏样式 */
        .readerTopBar, .navBar {
            max-width: 1200px !important; /* 最大宽度 */
            width: 100% !important;
            margin-left: auto !important;
            margin-right: auto !important;
            position: fixed !important; /* 固定在顶部 */
            top: 4px !important; /* 悬空10px */
            left: 50% !important;
            transform: translateX(-50%) !important; /* 水平居中 */
            transition: transform 0.15s ease-out !important; /* 加快动画 */
            z-index: 1000 !important; /* 层级 */
            backdrop-filter: blur(10px) !important; /* 毛玻璃效果 */
            -webkit-backdrop-filter: blur(10px) !important; /* 兼容 Safari */
            background: rgba(255, 255, 255, 0.8) !important; /* 半透明背景 */
            box-shadow: 0 1px 10px rgba(0, 0, 0, 0.1) !important; /* 阴影 */
            padding: 5px 10px !important; /* 高度缩小 */
            border-radius: 12px !important; /* 圆角 */
            /* 移除字体颜色跟随主题的逻辑,恢复默认 */
        }

        /* 顶部导航栏隐藏时的样式 */
        .readerTopBar.hidden {
            transform: translateX(-50%) translateY(-115%) !important; /* 完全隐藏 */
        }

        /* 顶部导航栏内的链接和按钮样式 */
        .readerTopBar a, .readerTopBar button {
            margin: 0 5px !important; /* 间距 */
            /* 移除字体颜色跟随主题的逻辑,恢复默认 */
        }

        /* 响应式设计:屏幕宽度大于768px时的样式 */
        @media (min-width: 768px) {
            .readerTopBar, .navBar {
                max-width: 1200px !important;
                left: 50% !important;
                transform: translateX(-50%) !important;
                padding: 5px 10px !important; /* 高度缩小 */
            }
            .readerTopBar.hidden {
                transform: translateX(-50%) translateY(-115%) !important; /* 完全隐藏 */
            }
            .readerTopBar a, .readerTopBar button {
                margin: 0 5px !important;
            }
        }

        /* 侧边栏(阅读控制栏和目录)的样式 */
        .readerControls, .readerCatalog {
            position: fixed !important;
            left: 1000px !important;
            bottom: 200px !important;
            transition: opacity 0.3s ease !important;
            opacity: 1 !important;
        }

        /* 侧边栏隐藏时的样式 */
        .readerControls.hidden, .readerCatalog.hidden {
            opacity: 0 !important;
        }

        /* 工具栏内项目的样式 */
        .reader_toolbar_container .toolbarItem {
            color: #ffffff !important;
            font-size: 12px !important;
        }

        /* 主题切换按钮样式 */
        #themeToggleBtn {
            position: fixed;
            bottom: calc(710px); /* 保持当前值 */
            right: 310px;
            width: 48px;
            height: 48px;
            background-color: #ffffff !important;
            color: #333 !important;
            border: none;
            border-radius: 50%;
            cursor: pointer;
            z-index: 2000;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1) !important;
            transition: opacity 0.3s ease !important;
        }

        /* 主题切换按钮隐藏时的样式 */
        #themeToggleBtn.hidden {
            opacity: 0 !important;
        }

        /* 自定义主题圆点提示框样式 */
        .themeDot {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            cursor: pointer;
            border: 2px solid #fff;
            box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
            position: relative;
        }

        /* 当前选中主题圆点的样式 */
        .themeDot.active {
            border: 2px solid #000;
        }

        /* 自定义提示框样式 */
        .themeDot::before {
            content: attr(title);
            position: absolute;
            top: -30px; /* 显示在正上方 */
            left: 50%;
            transform: translateX(-50%);
            background-color: rgba(0, 0, 0, 0.8);
            color: #fff;
            padding: 3px 6px;
            border-radius: 4px;
            font-size: 12px;
            white-space: nowrap;
            visibility: hidden;
            opacity: 0;
            transition: opacity 0.2s ease;
            z-index: 2001;
        }

        .themeDot:hover::before {
            visibility: visible;
            opacity: 1;
        }

        /* 主题选择面板样式 */
        #themePanel {
            position: fixed;
            background-color: rgba(255, 255, 255, 0.9);
            padding: 10px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            z-index: 2000;
            display: none;
            flex-direction: column; /* 改为列布局,分两行 */
            gap: 10px; /* 行间距 */
            max-width: 250px;
            backdrop-filter: blur(5px);
            transition: all 0.3s ease-out !important; /* 添加动画 */
            opacity: 0; /* 默认隐藏 */
            transform: translateY(20px); /* 默认向下偏移 */
        }

        /* 主题选择面板显示时的样式 */
        #themePanel.show {
            opacity: 1;
            transform: translateY(0); /* 显示时恢复原位 */
        }

        /* 每行主题容器 */
        .themeRow {
            display: flex;
            flex-direction: row;
            gap: 5px; /* 圆点间距 */
            flex-wrap: nowrap; /* 不换行 */
        }

        /* 侧边栏开关按钮样式 */
        #sidebarToggleBtn {
            position: fixed;
            bottom: calc(637px); /* 保持当前值 */
            right: 310px;
            width: 48px;
            height: 48px;
            background-color: #ffffff !important;
            color: #333 !important;
            border: none;
            border-radius: 50%;
            cursor: pointer;
            z-index: 2000;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1) !important;
            transition: opacity 0.3s ease !important;
        }

        /* 侧边栏开关按钮隐藏时的样式 */
        #sidebarToggleBtn.hidden {
            opacity: 0 !important;
        }

        /* 侧边栏开关激活时的样式 */
        #sidebarToggleBtn.active {
            background-color: #e0e0e0 !important;
        }

        /* 页面宽度调整按钮样式 */
        #widthAdjustBtn {
            position: fixed;
            bottom: calc(564px); /* 保持当前值 */
            right: 310px;
            width: 48px;
            height: 48px;
            background-color: #ffffff !important;
            color: #333 !important;
            border: none;
            border-radius: 50%;
            cursor: pointer;
            z-index: 2000;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1) !important;
            transition: opacity 0.3s ease !important;
        }

        /* 宽度调整按钮隐藏时的样式 */
        #widthAdjustBtn.hidden {
            opacity: 0 !important;
        }

        /* 宽度调整滑块面板样式 */
        #widthPanel {
            position: fixed;
            background-color: rgba(255, 255, 255, 0.9);
            padding: 10px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            z-index: 2000;
            display: none;
            flex-direction: row;
            align-items: center;
            gap: 10px;
            max-width: 250px;
            backdrop-filter: blur(5px);
            transition: all 0.3s ease-out !important; /* 添加动画 */
            opacity: 0; /* 默认隐藏 */
            transform: translateY(20px); /* 默认向下偏移 */
        }

        /* 宽度调整面板显示时的样式 */
        #widthPanel.show {
            opacity: 1;
            transform: translateY(0); /* 显示时恢复原位 */
        }

        /* 宽度滑块样式 */
        #widthSlider {
            -webkit-appearance: none;
            appearance: none;
            width: 150px;
            height: 2px;
            background: linear-gradient(to right, #000 0%, #000 var(--progress, 0%), #D3D3D3 var(--progress, 0%) 100%); /* 进度条效果,左侧黑色,右侧浅灰色 */
            outline: none;
            border-radius: 1px;
            cursor: pointer;
        }

        #widthSlider::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 8px;
            height: 8px;
            background: #333;
            border-radius: 50%;
            cursor: pointer;
        }

        #widthSlider::-moz-range-thumb {
            width: 8px;
            height: 8px;
            background: #333;
            border-radius: 50%;
            cursor: pointer;
        }

        /* 宽度显示标签样式 */
        #widthLabel {
            font-size: 14px;
            color: #333;
        }

        /* 响应式调整:屏幕宽度不足时缩小弹窗 */
        @media (max-width: 768px) {
            #themePanel, #widthPanel {
                max-width: 200px;
                padding: 5px;
            }
            #widthSlider {
                width: 120px;
            }
            .themeDot {
                width: 18px;
                height: 18px;
            }
        }

        @media (max-width: 500px) {
            #themePanel, #widthPanel {
                max-width: 150px;
                padding: 3px;
            }
            #widthSlider {
                width: 90px;
            }
            .themeDot {
                width: 16px;
                height: 16px;
            }
        }
    `;

    // 应用自定义样式到页面
    const styleSheet = document.createElement('style');
    styleSheet.type = 'text/css';
    styleSheet.innerText = customStyle;
    document.head.appendChild(styleSheet);

    // 创建主题切换按钮
    const toggleBtn = document.createElement('button');
    toggleBtn.id = 'themeToggleBtn';
    toggleBtn.innerText = '🎨';
    document.body.appendChild(toggleBtn);

    // 创建主题选择面板
    const themePanel = document.createElement('div');
    themePanel.id = 'themePanel';
    document.body.appendChild(themePanel);

    // 分组主题:白天和黑夜
    const dayThemes = ['极光灰', '浅咖色', '米黄色', '豆沙绿', '海天蓝', '晨曦白'];
    const nightThemes = ['夜空黑', '星辰黑', '幽暗黑', '月影黑', '深林黑', '静夜黑'];

    // 创建两行主题
    const dayRow = document.createElement('div');
    dayRow.className = 'themeRow';
    const nightRow = document.createElement('div');
    nightRow.className = 'themeRow';

    // 添加白天主题到第一行
    dayThemes.forEach(theme => {
        const dot = document.createElement('div');
        dot.className = 'themeDot';
        dot.style.backgroundColor = themes[theme].background;
        dot.setAttribute('title', theme);
        if (theme === currentTheme) dot.classList.add('active');
        dot.addEventListener('click', () => {
            GM_setValue('currentTheme', theme);
            location.reload();
            themePanel.classList.remove('show');
            setTimeout(() => {
                themePanel.style.display = 'none';
            }, 300); // 匹配动画时间
        });
        dayRow.appendChild(dot);
    });

    // 添加黑夜主题到第二行
    nightThemes.forEach(theme => {
        const dot = document.createElement('div');
        dot.className = 'themeDot';
        dot.style.backgroundColor = themes[theme].background;
        dot.setAttribute('title', theme);
        if (theme === currentTheme) dot.classList.add('active');
        dot.addEventListener('click', () => {
            GM_setValue('currentTheme', theme);
            location.reload();
            themePanel.classList.remove('show');
            setTimeout(() => {
                themePanel.style.display = 'none';
            }, 300); // 匹配动画时间
        });
        nightRow.appendChild(dot);
    });

    // 将两行添加到主题面板
    themePanel.appendChild(dayRow);
    themePanel.appendChild(nightRow);

    // 切换主题面板的显示/隐藏并动态定位
    toggleBtn.addEventListener('click', () => {
        if (themePanel.style.display === 'none' || themePanel.style.display === '') {
            themePanel.style.display = 'flex';
            setTimeout(() => themePanel.classList.add('show'), 10); // 延迟添加show类以触发动画
            const btnRect = toggleBtn.getBoundingClientRect();
            themePanel.style.left = `${btnRect.right + 10}px`;
            themePanel.style.top = `${btnRect.top}px`;
        } else {
            themePanel.classList.remove('show');
            setTimeout(() => {
                themePanel.style.display = 'none';
            }, 300); // 匹配动画时间
        }
    });

    // 更新选中圆点的样式
    function updateActiveDot() {
        document.querySelectorAll('.themeDot').forEach(dot => {
            dot.classList.remove('active');
            if (dot.style.backgroundColor === themes[currentTheme].background) {
                dot.classList.add('active');
            }
        });
    }

    // 创建侧边栏开关按钮
    const sidebarToggleBtn = document.createElement('button');
    sidebarToggleBtn.id = 'sidebarToggleBtn';
    sidebarToggleBtn.innerText = hideSidebarOnScrollDown ? '🙈' : '👁️';
    if (hideSidebarOnScrollDown) sidebarToggleBtn.classList.add('active');
    document.body.appendChild(sidebarToggleBtn);

    // 切换侧边栏隐藏状态
    sidebarToggleBtn.addEventListener('click', () => {
        hideSidebarOnScrollDown = !hideSidebarOnScrollDown;
        GM_setValue('hideSidebarOnScrollDown', hideSidebarOnScrollDown);
        sidebarToggleBtn.innerText = hideSidebarOnScrollDown ? '🙈' : '👁️';
        sidebarToggleBtn.classList.toggle('active');
    });

    // 创建页面宽度调整按钮
    const widthAdjustBtn = document.createElement('button');
    widthAdjustBtn.id = 'widthAdjustBtn';
    widthAdjustBtn.innerText = '📏';
    document.body.appendChild(widthAdjustBtn);

    // 创建宽度调整面板
    const widthPanel = document.createElement('div');
    widthPanel.id = 'widthPanel';
    document.body.appendChild(widthPanel);

    // 创建宽度滑块
    const widthSlider = document.createElement('input');
    widthSlider.type = 'range';
    widthSlider.id = 'widthSlider';
    widthSlider.min = '600';
    widthSlider.max = '1800';
    widthSlider.step = '10';
    widthSlider.value = pageWidth;
    widthPanel.appendChild(widthSlider);

    // 创建宽度显示标签
    const widthLabel = document.createElement('span');
    widthLabel.id = 'widthLabel';
    widthLabel.innerText = `${pageWidth}px`;
    widthPanel.appendChild(widthLabel);

    // 动态更新进度条样式
    function updateProgress() {
        const slider = document.getElementById('widthSlider');
        const value = ((slider.value - slider.min) / (slider.max - slider.min)) * 100;
        slider.style.setProperty('--progress', `${value}%`);
    }

    // 切换宽度调整面板的显示/隐藏并动态定位
    widthAdjustBtn.addEventListener('click', () => {
        if (widthPanel.style.display === 'none' || widthPanel.style.display === '') {
            widthPanel.style.display = 'flex';
            setTimeout(() => widthPanel.classList.add('show'), 10); // 延迟添加show类以触发动画
            const btnRect = widthAdjustBtn.getBoundingClientRect();
            widthPanel.style.left = `${btnRect.right + 10}px`;
            widthPanel.style.top = `${btnRect.top}px`;
        } else {
            widthPanel.classList.remove('show');
            setTimeout(() => {
                widthPanel.style.display = 'none';
            }, 300); // 匹配动画时间
        }
    });

    // 监听宽度滑块变化
    widthSlider.addEventListener('input', () => {
        pageWidth = parseInt(widthSlider.value);
        GM_setValue('pageWidth', pageWidth);
        widthLabel.innerText = `${pageWidth}px`;
        updateProgress(); // 更新进度条
        // location.reload(); // 注释掉自动刷新,实时更新样式
    });

    // 初始化进度条
    updateProgress();

    // 沉浸式阅读功能:上拉显示顶部和侧边栏,下拉隐藏顶部,侧边栏根据开关决定
    let windowTop = 0;
    $(window).scroll(function() {
        let scrollS = $(window).scrollTop();
        let topBar = document.querySelector('.readerTopBar');
        let readerControl = document.querySelector('.readerControls');
        let readerCatalog = document.querySelector('.readerCatalog');
        let themeBtn = document.querySelector('#themeToggleBtn');
        let sidebarBtn = document.querySelector('#sidebarToggleBtn');
        let widthBtn = document.querySelector('#widthAdjustBtn');

        if (scrollS > windowTop && scrollS > 50) {
            if (topBar && hideSidebarOnScrollDown) topBar.classList.add('hidden'); // 隐藏顶部
            if (readerControl && hideSidebarOnScrollDown) readerControl.classList.add('hidden'); // 隐藏右侧控制栏
            if (readerCatalog && hideSidebarOnScrollDown) readerCatalog.classList.add('hidden'); // 隐藏右侧目录
            if (themeBtn && hideSidebarOnScrollDown) themeBtn.classList.add('hidden'); // 隐藏主题按钮
            if (sidebarBtn && hideSidebarOnScrollDown) sidebarBtn.classList.add('hidden'); // 隐藏侧边栏开关
            if (widthBtn && hideSidebarOnScrollDown) widthBtn.classList.add('hidden'); // 隐藏宽度调整按钮
            if (themePanel) themePanel.style.display = 'none';
            if (widthPanel) widthPanel.style.display = 'none';
        } else {
            if (topBar) topBar.classList.remove('hidden'); // 显示顶部
            if (readerControl) readerControl.classList.remove('hidden'); // 显示右侧控制栏
            if (readerCatalog) readerCatalog.classList.remove('hidden'); // 显示右侧目录
            if (themeBtn) themeBtn.classList.remove('hidden'); // 显示主题按钮
            if (sidebarBtn) sidebarBtn.classList.remove('hidden'); // 显示侧边栏开关
            if (widthBtn) widthBtn.classList.remove('hidden'); // 显示宽度调整按钮
        }
        windowTop = scrollS;
    });

    updateActiveDot();
})();