Notion标题自动编号

为Notion标题自动添加序号

当前为 2025-08-09 提交的版本,查看 最新版本

// ==UserScript==
// @name         Notion标题自动编号
// @name:en      Notion Auto Heading Numbering
// @description  为Notion标题自动添加序号
// @description:en Automatically add numbering to Notion headings
// @author       Gou Tan
// @namespace    https://floritange.github.io/
// @license      MIT
// @version      1.0.0
// @match        https://www.notion.so/*
// @grant        GM_addStyle
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 注入标题编号CSS
    function injectTitleNumberingCSS() {
        const css = `
        /* 正文标题编号 */
        body {
            counter-reset: headings1 headings2 headings3;
        }

        /* 重置计数器 */
        .notranslate[placeholder="Heading 1"] {
            counter-reset: headings2 headings3;
        }

        .notranslate[placeholder="Heading 2"] {
            counter-reset: headings3;
        }

        /* 添加编号前缀 */
        .notranslate[placeholder="Heading 1"]::before {
            counter-increment: headings1;
            content: counter(headings1) ". ";
        }

        .notranslate[placeholder="Heading 2"]::before {
            counter-increment: headings2;
            content: counter(headings1) "." counter(headings2) " ";
        }

        .notranslate[placeholder="Heading 3"]::before {
            counter-increment: headings3;
            content: counter(headings1) "." counter(headings2) "." counter(headings3) " ";
        }

        /* 目录编号 */
        .table_of_contents {
            counter-reset: toc-h1 toc-h2 toc-h3;
        }

        /* 目录中的h1重置下级计数器 */
        .table_of_contents .nb-h1 {
            counter-reset: toc-h2 toc-h3;
        }

        .table_of_contents .nb-h2 {
            counter-reset: toc-h3;
        }

        /* 目录编号前缀 */
        .table_of_contents .nb-h1 .text::before {
            counter-increment: toc-h1;
            content: counter(toc-h1) ". ";
        }

        .table_of_contents .nb-h2 .text::before {
            counter-increment: toc-h2;
            content: counter(toc-h1) "." counter(toc-h2) " ";
        }

        .table_of_contents .nb-h3 .text::before {
            counter-increment: toc-h3;
            content: counter(toc-h1) "." counter(toc-h2) "." counter(toc-h3) " ";
        }
            
        /* 可选:为目录添加一些样式美化 */
        .table_of_contents .nb-h1 .text {
            font-weight: bold;
        }

        .table_of_contents .nb-h2 .text {
            padding-left: 1em;
        }

        .table_of_contents .nb-h3 .text {
            padding-left: 2em;
        }
        `;

        GM_addStyle(css);
    }

    // 初始化
    function init() {
        injectTitleNumberingCSS();
    }

    // 页面加载时初始化
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }

    // 监听页面变化(Notion是SPA应用)
    let currentUrl = location.href;
    const observer = new MutationObserver(() => {
        if (location.href !== currentUrl) {
            currentUrl = location.href;
            setTimeout(init, 500);
        }
    });
    observer.observe(document, { subtree: true, childList: true });

})();

QingJ © 2025

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