Greasy Fork is available in English.

gitlab issue TOC

为 gitlab issue 生成目录(需要自己改下 domain)

  1. // ==UserScript==
  2. // @name gitlab issue TOC
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description 为 gitlab issue 生成目录(需要自己改下 domain)
  6. // @author RobinTsai
  7. // @match https://gitlab.com/*/issues/*
  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. var tocID = "TOC"
  17. var moveTOC = function() { // 调整 TOC 位置
  18. var pos = $("a.toggle-sidebar-button").css("width");
  19. $("#"+tocID).animate({"left": pos}, "slow");
  20. }
  21.  
  22. $(document).ready(function(){ // dom 加载完成后将 TOC 放到合适的位置
  23. moveTOC();
  24. });
  25.  
  26. $("a.toggle-sidebar-button").on("click", function() { // 监听左侧栏事件,调整 TOC 位置
  27. setTimeout(moveTOC, 255)
  28. });
  29.  
  30. var idx = 0
  31. var hNames = []
  32. var content = $("body,.detail-page-description")
  33. var headings = content.find("h1,h2,h3,h4,h5")
  34. // 获取 headings 并设置 id
  35. var minLevel = 5 // 收集最小的层级 h1 => 1, h2 => 2
  36. var levels = []
  37. for (idx in headings) {
  38. if (isNaN(Number(idx))) {
  39. continue
  40. }
  41. var heading = headings[idx]
  42. if (heading.classList.length > 0) {
  43. continue
  44. }
  45. heading.id = heading.innerText
  46.  
  47. var level = parseInt(heading.tagName.replace("H", ""))
  48. if (levels.indexOf(level) == -1) {
  49. levels.push(level)
  50. }
  51.  
  52. if (level < minLevel) {
  53. minLevel = level
  54. }
  55. hNames.push({
  56. tagLevel: level,
  57. name: heading.innerText,
  58. })
  59. }
  60.  
  61. var mapLevelToMarginLeft = {}
  62. levels.sort()
  63. for (idx in levels) {
  64. mapLevelToMarginLeft[levels[idx]] = 20 * idx + "px"
  65. }
  66. var innerH = []
  67.  
  68. for (idx in hNames) {
  69. var elem = $("<a>", {
  70. style: "white-space: nowrap; margin-left: " + mapLevelToMarginLeft[hNames[idx].tagLevel],
  71. innerText: hNames[idx].name,
  72. text: hNames[idx].name,
  73. href: "#"+hNames[idx].name,
  74. })
  75. innerH.push( elem )
  76. }
  77.  
  78. var headingWrap = $("<div>", {
  79. id: tocID,
  80. style: "\
  81. position: fixed; \
  82. z-index: 1000; \
  83. top: 100px; \
  84. max-height: 400px; \
  85. width: 174px; \
  86. overflow: auto; \
  87. border: solid 1px #826b6b; \
  88. padding: 5px; \
  89. padding-left: 12px; \
  90. left: 220px; \
  91. background-color: #dedede; \
  92. ".replace(/ /g, ""),
  93. mouseover: function() {
  94. },
  95. mouseout: function() {
  96. },
  97. })
  98. // hNames.push(content.find("h1._1RuRku").text())
  99.  
  100. for (idx in innerH) {
  101. innerH[idx].appendTo(headingWrap)
  102. $("<br>").appendTo(headingWrap)
  103. }
  104. headingWrap.appendTo("body");
  105. })();