DOC of Jianshu

为简书文章生成目录

目前為 2020-03-22 提交的版本,檢視 最新版本

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

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

    var idx = 0
    var hNames = []
    var content = $("body,.__next,._21bLU4._3kbg6I._gp-ck,section")
    var headings = content.find("h1,h2,h3,h4")
    // 获取 headings 并设置 id
    for (idx in headings) {
        if (isNaN(Number(idx))) {
            continue
        }
        var heading = headings[idx]
        if (heading.classList.length > 0) {
            continue
        }
        heading.id = heading.innerText
        hNames.push(heading.innerText)
    }

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

    var headingWrap = $("<div>", {
        id: "DOCofJianShu",
        style: "\
            position: fixed; \
            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");
})();