TOC of Jianshu

为简书文章生成目录

  1. // ==UserScript==
  2. // @name TOC of Jianshu
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.5
  5. // @description 为简书文章生成目录
  6. // @author RobinTsai
  7. // @match https://www.jianshu.com/p/*
  8. // @grant none
  9. // @require https://code.jquery.com/jquery-latest.js
  10. // ==/UserScript==
  11.  
  12. // jquery API: http://jquery.cuishifeng.cn/
  13. (function() {
  14. 'use strict';
  15.  
  16. $('#adinteract').hide() // 去除广告
  17. $('#adModule').hide() // 去除广告
  18.  
  19. setTimeout(function () {
  20. $('div[style="width: 200px; height: 400px; position: fixed; top:35%; margin-top:-200px; z-index: 2147483647; right:0px;"]').hide()
  21. $("div#note").hide()
  22. $('div[style="width: 200px; height: 400px; position: fixed; top: 35%; margin-top:-200px; z-index: 2147483647; left:0px;"]').hide()
  23. $('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()
  24. }, 800);
  25.  
  26. var idx = 0
  27. var hNames = []
  28. var content = $("body,.__next,._21bLU4._3kbg6I._gp-ck,section")
  29. var headings = content.find("h1,h2,h3,h4,h5")
  30. // 获取 headings 并设置 id
  31. var minLevel = 5 // 收集最小的层级 h1 => 1, h2 => 2
  32. var levels = []
  33. for (idx in headings) {
  34. if (isNaN(Number(idx))) {
  35. continue
  36. }
  37. var heading = headings[idx]
  38. if (heading.classList.length > 0) {
  39. continue
  40. }
  41. heading.id = heading.innerText
  42.  
  43. var level = parseInt(heading.tagName.replace("H", ""))
  44. if (levels.indexOf(level) == -1) {
  45. levels.push(level)
  46. }
  47.  
  48. if (level < minLevel) {
  49. minLevel = level
  50. }
  51. hNames.push({
  52. tagLevel: level,
  53. name: heading.innerText,
  54. })
  55. }
  56.  
  57. var mapLevelToMarginLeft = {}
  58. levels.sort()
  59. for (idx in levels) {
  60. mapLevelToMarginLeft[levels[idx]] = 20 * idx + "px"
  61. }
  62. var innerH = []
  63.  
  64. for (idx in hNames) {
  65. var elem = $("<a>", {
  66. style: "white-space: nowrap; margin-left: " + mapLevelToMarginLeft[hNames[idx].tagLevel],
  67. innerText: hNames[idx].name,
  68. text: hNames[idx].name,
  69. href: "#"+hNames[idx].name,
  70. })
  71. innerH.push( elem )
  72. }
  73.  
  74. var headingWrap = $("<div>", {
  75. id: "TOCofJianShu",
  76. style: "\
  77. position: fixed; \
  78. z-index: 1000; \
  79. top: 100px; \
  80. max-height: 400px; \
  81. width: 174px; \
  82. overflow: auto; \
  83. border: solid 1px #826b6b; \
  84. padding: 5px; \
  85. padding-left: 12px; \
  86. background-color: #dedede; \
  87. ".replace(/ /g, ""),
  88. mouseover: function() {
  89. },
  90. mouseout: function() {
  91. },
  92. })
  93. // hNames.push(content.find("h1._1RuRku").text())
  94.  
  95. for (idx in innerH) {
  96. innerH[idx].appendTo(headingWrap)
  97. $("<br>").appendTo(headingWrap)
  98. }
  99. headingWrap.appendTo("body");
  100. })();

QingJ © 2025

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