微信读书沉浸式阅读主题

微信读书上拉显示头部和侧栏,下拉隐藏,主题切换和侧边栏开关按钮,顶部毛玻璃效果

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         微信读书沉浸式阅读主题
// @version      1.1.0
// @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 = {
        '极光灰': { background: '#E0E0E0', fontColor: '#333333' },
        '浅咖色': { background: '#FAEBD7', fontColor: '#403020' },
        '米黄色': { background: '#F5F5DC', fontColor: '#403020' },
        '豆沙绿': { background: '#E6F5E6', fontColor: '#203020' },
        '海天蓝': { background: '#EBF5FF', fontColor: '#203040' },
        '墨渊黑': { background: '#282828', fontColor: '#C8C8C8' },
        '米黄黑': { background: '#F5F5DC', fontColor: '#333333' },
        '豆沙黑': { background: '#E6F5E6', fontColor: '#333333' },
        '海天黑': { background: '#EBF5FF', fontColor: '#333333' }
    };

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

    // 自定义样式,包含所有界面元素的样式调整
    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: 1200px !important; /* 最大宽度 */
            margin: 0 auto !important; /* 居中 */
            padding: 20px !important; /* 内边距 */
        }

        /* 章节内容的子元素样式,继承父元素 */
        .readerChapterContent p, .readerChapterContent div, .readerChapterContent span {
            font-family: inherit !important;
            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; /* 圆角 */
            color: ${themes[currentTheme].fontColor} !important; /* 字体颜色随主题变化 */
        }

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

        /* 顶部导航栏内的链接和按钮样式 */
        .readerTopBar a, .readerTopBar button {
            margin: 0 5px !important; /* 间距 */
            color: ${themes[currentTheme].fontColor} !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.15s ease-out !important; /* 加快动画 */
            opacity: 1 !important; /* 默认显示 */
        }

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

        /* 全局字体设置 */
        * {
            font-family: "霞鹜文楷", "PingFang SC", "宋体" !important;
        }

        /* 主题切换按钮样式 */
        #themeToggleBtn {
            position: fixed; /* 固定位置 */
            bottom: 430px; /* 原130px + 300px = 430px,向下增加表示向上移动 */
            right: 110px; /* 原310px - 200px = 110px,向右减少表示向右移动 */
            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.15s ease-out !important; /* 加快动画 */
        }

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

        /* 主题选择面板样式 */
        #themePanel {
            position: fixed; /* 固定位置 */
            bottom: 490px; /* 原190px + 300px = 490px,跟随按钮向上移动 */
            right: 110px; /* 原310px - 200px = 110px,与按钮同步向右移动 */
            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; /* 主题圆点间距 */
            backdrop-filter: blur(5px); /* 毛玻璃效果 */
            transition: all 0.15s ease-out !important; /* 加快动画 */
            opacity: 0; /* 默认隐藏 */
            transform: translateY(100%); /* 初始位置在下方 */
        }

        /* 主题选择面板显示时的样式 */
        #themePanel.show {
            opacity: 1; /* 显示 */
            transform: translateY(0); /* 移动到正确位置 */
        }

        /* 主题圆点样式 */
        .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); /* 阴影 */
            transition: transform 0.15s ease-out !important; /* 加快动画 */
        }

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

        /* 侧边栏开关按钮样式 */
        #sidebarToggleBtn {
            position: fixed; /* 固定位置 */
            bottom: 360px; /* 原60px + 300px = 360px,向下增加表示向上移动 */
            right: 110px; /* 原310px - 200px = 110px,向右减少表示向右移动 */
            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.15s ease-out !important; /* 加快动画 */
        }

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

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

    // 应用自定义样式到页面
    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);

    // 添加主题圆点到面板
    Object.keys(themes).forEach((theme, index) => {
        const dot = document.createElement('div');
        dot.className = 'themeDot';
        dot.style.backgroundColor = themes[theme].background; // 设置圆点颜色
        dot.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'; // 动画结束后隐藏
            }, 150); // 匹配动画时间
        });
        themePanel.appendChild(dot);
    });

    // 切换主题面板的显示/隐藏
    toggleBtn.addEventListener('click', () => {
        if (themePanel.style.display === 'none' || themePanel.style.display === '') {
            themePanel.style.display = 'flex';
            setTimeout(() => themePanel.classList.add('show'), 10); // 延迟添加show类以触发动画
        } else {
            themePanel.classList.remove('show');
            setTimeout(() => {
                themePanel.style.display = 'none'; // 动画结束后隐藏
            }, 150); // 匹配动画时间
        }
    });

    // 更新选中圆点的样式
    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'); // 更新样式
    });

    // 沉浸式阅读功能:上拉显示所有区域,下拉根据开关隐藏
    let windowTop = 0;
    $(window).scroll(function() {
        let scrollS = $(window).scrollTop(); // 当前滚动位置
        let topBar = document.querySelector('.readerTopBar'); // 顶部导航栏
        let readerControl = document.querySelector('.readerControls'); // 侧边栏
        let themeBtn = document.querySelector('#themeToggleBtn'); // 主题切换按钮
        let sidebarBtn = document.querySelector('#sidebarToggleBtn'); // 侧边栏开关按钮

        if (scrollS > windowTop && scrollS > 50) { // 下拉超过50px
            if (topBar && hideSidebarOnScrollDown) topBar.classList.add('hidden'); // 隐藏顶部
            if (readerControl && hideSidebarOnScrollDown) {
                readerControl.style.setProperty('opacity', '0', 'important'); // 隐藏侧边栏
            }
            if (themeBtn && hideSidebarOnScrollDown) themeBtn.classList.add('hidden'); // 隐藏主题按钮
            if (sidebarBtn && hideSidebarOnScrollDown) sidebarBtn.classList.add('hidden'); // 隐藏侧边栏开关
        } else { // 上拉
            if (topBar) topBar.classList.remove('hidden'); // 显示顶部
            if (readerControl) {
                readerControl.style.setProperty('opacity', '1', 'important'); // 显示侧边栏
            }
            if (themeBtn) themeBtn.classList.remove('hidden'); // 显示主题按钮
            if (sidebarBtn) sidebarBtn.classList.remove('hidden'); // 显示侧边栏开关
        }
        windowTop = scrollS; // 更新滚动位置
    });

    updateActiveDot(); // 初始化时更新圆点状态
})();