日·键圈时刻表目录生成(合并版)

合并了两个目录生成脚本,支持opus和read页面,修复了评论区显示、动态更新等问题,增加多区块滚动隐藏功能。

目前为 2024-11-29 提交的版本。查看 最新版本

// ==UserScript==
// @name         日·键圈时刻表目录生成(合并版)
// @namespace    http://tampermonkey.net/
// @version      3.4
// @description  合并了两个目录生成脚本,支持opus和read页面,修复了评论区显示、动态更新等问题,增加多区块滚动隐藏功能。
// @match        https://www.bilibili.com/opus/*
// @match        https://www.bilibili.com/read/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const isOpusPage = window.location.href.includes('/opus/');
    const isReadPage = window.location.href.includes('/read/');

    // 定义需要隐藏的元素
    let lastScrollTop = 0;
    const headersToHide = [];

    if (isOpusPage) {
        const opusHeader1 = document.querySelector('.bili-header__bar.mini-header');
        const opusHeader2 = document.querySelector('.fixed-author-header');
        if (opusHeader1) headersToHide.push(opusHeader1);
        if (opusHeader2) headersToHide.push(opusHeader2);
    } else if (isReadPage) {
        const readHeader1 = document.querySelector('#bili-header-container');
        const readHeader2 = document.querySelector('.fixed-top-header');
        if (readHeader1) headersToHide.push(readHeader1);
        if (readHeader2) headersToHide.push(readHeader2);
    }

    if (headersToHide.length > 0) {
        window.addEventListener('scroll', () => {
            const currentScrollTop = window.scrollY || document.documentElement.scrollTop;
            headersToHide.forEach(header => {
                if (currentScrollTop > lastScrollTop) {
                    // 向下滚动时隐藏
                    header.style.transition = 'top 0.3s';
                    header.style.top = '-100px';
                } else {
                    // 向上滚动时显示
                    header.style.transition = 'top 0.3s';
                    header.style.top = '0';
                }
            });
            lastScrollTop = currentScrollTop <= 0 ? 0 : currentScrollTop;
        });
    }

    // 创建目录容器
    const tocContainer = document.createElement('div');
    tocContainer.style.position = 'fixed';
    tocContainer.style.left = '10px';
    tocContainer.style.top = '10px';
    tocContainer.style.width = '388px';
    tocContainer.style.maxHeight = '96vh';
    tocContainer.style.overflowY = 'auto';
    tocContainer.style.padding = '5px';
    tocContainer.style.backgroundColor = 'rgba(255, 255, 255, 0.95)';
    tocContainer.style.borderRadius = '8px';
    tocContainer.style.boxShadow = '0 4px 15px rgba(0, 0, 0, 0.2)';
    tocContainer.style.zIndex = '1000';

    // 添加目录标题
    const tocTitle = document.createElement('h3');
    tocTitle.innerText = '目录';
    tocTitle.style.textAlign = 'center';
    tocTitle.style.marginBottom = '15px';
    tocTitle.style.fontFamily = 'Arial, sans-serif';
    tocTitle.style.color = '#333';
    tocTitle.style.fontSize = '18px';
    tocContainer.appendChild(tocTitle);

    // 特别显示逻辑关键字
    const highlightKeywords = ["置顶区", "消息区", "日常区", "常驻区"];

    // 根据页面类型生成目录
    if (isOpusPage) {
        const pTags = document.querySelectorAll('p');
        pTags.forEach((pTag, index) => {
            const strongElements = pTag.querySelectorAll('strong');
            const combinedText = Array.from(strongElements).map(el => el.innerText.trim()).join(' ');

            if (combinedText && strongElements.length > 0) {
                const fontSize = window.getComputedStyle(strongElements[0]).fontSize;

                if (fontSize === '24px') {
                    const id = 'toc-header-' + index;
                    pTag.id = id;

                    const tocItem = document.createElement('a');
                    tocItem.href = '#' + id;
                    tocItem.innerText = combinedText;
                    tocItem.style.display = 'block';
                    tocItem.style.color = '#007bff';
                    tocItem.style.textDecoration = 'none';
                    tocItem.style.fontSize = '13px';
                    tocItem.style.marginBottom = '5px';

                    if (highlightKeywords.some(keyword => combinedText.includes(keyword))) {
                        tocItem.style.fontWeight = 'bold';
                        tocItem.style.color = '#ff4500';
                        tocItem.style.fontSize = '16px';
                    }

                    tocItem.onmouseover = function() {
                        tocItem.style.textDecoration = 'underline';
                    };
                    tocItem.onmouseout = function() {
                        tocItem.style.textDecoration = 'none';
                    };

                    tocContainer.appendChild(tocItem);
                }
            }
        });

    } else if (isReadPage) {
        const h1Tags = document.querySelectorAll('h1');
        h1Tags.forEach((h1Tag, index) => {
            const combinedText = h1Tag.textContent.trim();

            if (combinedText) {
                const id = 'toc-header-' + index;
                h1Tag.id = id;

                const tocItem = document.createElement('a');
                tocItem.href = '#' + id;
                tocItem.innerText = combinedText;
                tocItem.style.display = 'block';
                tocItem.style.color = '#007bff';
                tocItem.style.textDecoration = 'none';
                tocItem.style.fontSize = '13px';
                tocItem.style.marginBottom = '5px';

                if (highlightKeywords.some(keyword => combinedText.includes(keyword))) {
                    tocItem.style.fontWeight = 'bold';
                    tocItem.style.color = '#ff4500';
                    tocItem.style.fontSize = '16px';
                }

                tocItem.onmouseover = function() {
                    tocItem.style.textDecoration = 'underline';
                };
                tocItem.onmouseout = function() {
                    tocItem.style.textDecoration = 'none';
                };

                tocContainer.appendChild(tocItem);
            }
        });
    }

    // 添加评论区入口
    const commentItem = document.createElement('a');
    commentItem.style.display = 'block';
    commentItem.style.color = '#dc3545';
    commentItem.style.textDecoration = 'none';
    commentItem.style.fontSize = '16px';
    commentItem.style.fontWeight = 'bold';
    commentItem.style.marginTop = '10px';

    if (isReadPage) {
        const commentWrapper = document.querySelector('#comment-wrapper.comment-wrapper');
        if (commentWrapper) {
            commentItem.href = '#comment-wrapper';
            commentItem.innerText = '评论区';
        }
    } else if (isOpusPage) {
        const commentSection = document.querySelector('.bili-tabs.opus-tabs');
        if (commentSection) {
            commentSection.id = 'opus-comment-section';
            commentItem.href = '#opus-comment-section';
            commentItem.innerText = '评论区';
        }
    }

    commentItem.onmouseover = function() {
        commentItem.style.textDecoration = 'underline';
    };
    commentItem.onmouseout = function() {
        commentItem.style.textDecoration = 'none';
    };

    tocContainer.appendChild(commentItem);

    // 添加到页面
    if (tocContainer.childElementCount > 1) {
        document.body.appendChild(tocContainer);
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址