TOC of Jianshu

为简书文章生成目录

目前為 2023-04-23 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         TOC of Jianshu
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  为简书文章生成目录
// @author       RobinTsai
// @match        https://www.jianshu.com/p/*
// @grant        none
// @require      https://code.jquery.com/jquery-latest.js
// ==/UserScript==

// jquery API: http://jquery.cuishifeng.cn/
(function() {
    'use strict';

    $('#adinteract').hide() // 去除广告
    $('#adModule').hide()   // 去除广告

    setTimeout(function () {
        $('div[style="width: 200px; height: 400px; position: fixed; top:35%; margin-top:-200px; z-index: 2147483647; right:0px;"]').hide()
        $("div#note").hide()
        $('div[style="width: 200px; height: 400px; position: fixed; top: 35%; margin-top:-200px; z-index: 2147483647; left:0px;"]').hide()
        $('div[style="display: block; padding: 0px; margin: 0px; z-index:2147483648; position: fixed; right: 0px; bottom: auto; left: auto; bottom: 0px; width: 320px; height: 270px;"]').hide()
    }, 800);

    var idx = 0
    var hNames = []
    var content = $("body,.__next,._21bLU4._3kbg6I._gp-ck,section")
    var headings = content.find("h1,h2,h3,h4,h5")
    // 获取 headings 并设置 id
    var minLevel = 5 // 收集最小的层级 h1 => 1, h2 => 2
    var levels = []
    for (idx in headings) {
        if (isNaN(Number(idx))) {
            continue
        }
        var heading = headings[idx]
        if (heading.classList.length > 0) {
            continue
        }
        heading.id = heading.innerText

        var level = parseInt(heading.tagName.replace("H", ""))
        if (levels.indexOf(level) == -1) {
            levels.push(level)
        }

        if (level < minLevel) {
            minLevel = level
        }
        hNames.push({
            tagLevel: level,
            name: heading.innerText,
        })
    }

    var mapLevelToMarginLeft = {}
    levels.sort()
    for (idx in levels) {
        mapLevelToMarginLeft[levels[idx]] = 20 * idx + "px"
    }
    var innerH = []

    for (idx in hNames) {
        var elem = $("<a>", {
            style: "white-space: nowrap; margin-left: " + mapLevelToMarginLeft[hNames[idx].tagLevel],
            innerText: hNames[idx].name,
            text: hNames[idx].name,
            href: "#"+hNames[idx].name,
        })
        innerH.push( elem )
    }

    var headingWrap = $("<div>", {
        id: "TOCofJianShu",
        style: "\
            position: fixed; \
            z-index: 1000; \
            top: 100px; \
            max-height: 400px; \
            width: 174px; \
            overflow: auto; \
            border: solid 1px #826b6b; \
            padding: 5px; \
            padding-left: 12px; \
            background-color: #dedede; \
        ".replace(/ /g, ""),
        mouseover: function() {
        },
        mouseout: function() {
        },
    })
    // hNames.push(content.find("h1._1RuRku").text())

    for (idx in innerH) {
        innerH[idx].appendTo(headingWrap)
        $("<br>").appendTo(headingWrap)
    }
    headingWrap.appendTo("body");
})();