Greasy Fork

Greasy Fork is available in English.

Notion 浮动 TOC

Make Notion TOC Fixed.

当前为 2020-07-05 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Notion Fixed TOC
// @name:zh-CN   Notion 浮动 TOC
// @namespace    https://notion.cx/
// @version      0.1.0
// @description  Make Notion TOC Fixed.
// @description:zh-cn 让 Notion 的 TOC 浮动显示。
// @author       Ruter Lü
// @match        https://www.notion.so/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    /* Helper function to wait for the element ready */
    const waitFor = (...selectors) => new Promise(resolve => {
        const delay = 500;
        const f = () => {
            const elements = selectors.map(selector => document.querySelector(selector));
            if (elements.every(element => element != null)) {
                resolve(elements);
            } else {
                setTimeout(f, delay);
            }
        }
        f();
    });
    /* Helper function to wait for the element ready */

    // If flag is true, that means should make TOC fixed.
    var flag = true;
    let oldHref = '';
    let callback = function(mutations) {
        if (oldHref != document.location.href) {
            oldHref = document.location.href;
            waitFor('.notion-table_of_contents-block').then(([el]) => {
                document.querySelector('.notion-frame .notion-scroller').addEventListener('scroll', function (e) {
                    const toc = document.querySelector('.notion-table_of_contents-block');
                    if (toc) {
                        const toc_p = toc.parentElement;
                        if (!toc_p.classList.contains('notion-column-block')) {
                            return;
                        }
                        const toc_p_p = toc_p.parentElement;
                        const rect = toc_p_p.getBoundingClientRect();
                        if (rect.top <= 45) {
                            if (flag) {
                                flag = !flag;
                                toc_p_p.style.position = 'relative';
                                toc_p.style.position = 'fixed';
                                toc_p.style.top = '45px';
                            }
                        } else {
                            if (!flag) {
                                flag = !flag;
                            }
                            toc_p_p.style.position = null;
                            toc_p.style.position = null;
                            toc_p.style.top = null;
                        }
                    }
                });
            });
        }
    };
    const observer = new MutationObserver(callback);
    let notionApp = document.getElementById('notion-app');
    const config = { childList: true, subtree: true };
    observer.observe(notionApp, config);
})();