Display Toc On kernel.org[JS|UI]

10/25/2024, 9:59:16 AM

  1. // ==UserScript==
  2. // @name Display Toc On kernel.org[JS|UI]
  3. // @namespace https://github.com/rikacelery
  4. // @match https://lore.kernel.org/*
  5. // @grant none
  6. // @license MIT
  7. // @compatible chrome
  8. // @grant GM_addStyle
  9. // @license MIT
  10. // @version 0.0.3
  11. // @author RikaCelery
  12. // @supportURL https://github.com/RikaCelery/UserScriptSupport/issues
  13. // @description 10/25/2024, 9:59:16 AM
  14. // ==/UserScript==
  15. GM_addStyle(`
  16. *{
  17. background: unset !important;
  18. }
  19. *.q {
  20. display: block;
  21. background: rgb(227, 249, 255)!important;
  22. width: fit-content;
  23. border-radius: 10px;
  24. padding: 10px;
  25. position: relative;
  26. }
  27. *.q::before{
  28. content: '';
  29. position: absolute;
  30. left: 5px;
  31. top: calc(10px);
  32. height: calc(100% - 2 * 10px);
  33. width: 5px;
  34. border-radius: 5px;
  35. background-color: rgb(166, 220, 228);
  36. }
  37.  
  38. ul,body > form + pre {
  39. position: relative;
  40. background-color: rgba(200, 224, 228, 0.11)!important;
  41. width: fit-content!important;
  42. border-radius: 10px;
  43. padding-top: 10px;
  44. padding-bottom: 10px;
  45. padding-right: 10px;
  46. /* border: solid 1px rgba(39, 182, 175, 0.418); */
  47. box-shadow: 2px 2px 15px rgba(0, 0, 0, 0.089);
  48. }
  49. body > form + pre {
  50. padding-left: 10px;
  51. padding-right: 10px;
  52. }
  53. ul li {
  54. background: transparent!important;
  55. width: fit-content!important;
  56. }
  57. ul::after{
  58. content: '';
  59. position: absolute;
  60. left: 0px;
  61. top: calc(3px);
  62. height: calc(100% - 2 * 3px);
  63. width: 2px;
  64. border-radius: 5px;
  65. background-color: rgba(65, 104, 101, 0.432);
  66. }
  67. body {
  68. display: flex;
  69. flex-direction: column;
  70. }
  71. body > pre.toc{
  72. position: fixed;
  73. top: 0;
  74. right: 0;
  75. width: fit-content;
  76. align-self: flex-end;
  77. z-index: 100;
  78. padding-left: 10px;
  79. padding-right: 10px;
  80. }
  81. body > pre.toc .container{
  82. /* display: flex;/ */
  83. /* flex-direction: column; */
  84. }
  85. body > pre.toc .container .item{
  86. white-space: pre;
  87. margin: 0;
  88. padding: 0;
  89. height: 1em;
  90. /* display: inline-block; */
  91.  
  92. /* max-width:200px!important; */
  93. /* overflow: hidden; */
  94. }`)
  95. window.onload = () => {
  96. document.querySelectorAll("*.q").forEach((el) => {
  97. el.childNodes.forEach((child) => {
  98. if (child.nodeType === Node.TEXT_NODE) {
  99. // console.log(child.textContent);
  100. // child.textContent = child.textContent.replace(/^>\n\[\.\.\.\]/gm, "> [...]");
  101. // child.textContent = child.textContent.replace(/^>( |$)/gm, " ");
  102. }
  103. // if (child.nodeType === Node.ELEMENT_NODE) console.log(child.innerHTML);
  104. });
  105. });
  106. let pre = document.querySelectorAll("body > pre")[1];
  107. pre.remove();
  108. pre.classList.add("toc");
  109. document.body.insertBefore(pre, document.querySelector("body > form"));
  110. let tmp = [];
  111. /**
  112. * @type {{date:string,linkNode:ChildNode}[]}
  113. */
  114. let strucured = [];
  115. let parse = false;
  116. let toc = document.querySelector("pre.toc")
  117. toc.childNodes.forEach((node) => {
  118. if (
  119. node.nodeType === Node.TEXT_NODE &&
  120. node.textContent.indexOf(
  121. "-- links below jump to the message on this page --"
  122. ) != -1
  123. ) {
  124. parse = true;
  125. let idx =
  126. node.textContent.indexOf(
  127. "-- links below jump to the message on this page --"
  128. ) + "-- links below jump to the message on this page --".length;
  129. let tmpNode = document.createTextNode(node.textContent.substring(0, idx));
  130. tmp.push(tmpNode);
  131. node = document.createTextNode(node.textContent.substring(idx));
  132. console.log(idx, node.textContent, tmpNode.textContent);
  133. } else {
  134. if (!parse) tmp.push(node);
  135. }
  136. console.log(parse, node);
  137. if (parse) {
  138. if (node.nodeType === Node.TEXT_NODE) {
  139. let content = node.textContent.trim();
  140. if (!content) return;
  141. if (!/^\d{4}-\d{2}-\d{2}$/.test(content)) {
  142. let match = /^\s*(.*)\s*(\d{4}-\d{2}-\d{2} +\d+:\d+.*)/gs.exec(content);
  143. console.log(content, match);
  144. content = match[2];
  145. strucured.push({
  146. date: content,
  147. linkNode: match[1],
  148. });
  149. } else
  150. strucured.push({
  151. date: content,
  152. linkNode: null,
  153. });
  154. } else {
  155. if (typeof strucured[strucured.length - 1].linkNode ==="string") {
  156. strucured[strucured.length - 1].linkNode.textContent += strucured[strucured.length - 1].linkNode;
  157. }
  158. strucured[strucured.length - 1].linkNode = node;
  159. }
  160. }
  161. });
  162. console.log(tmp);
  163. console.log(strucured);
  164. toc.innerHTML=""
  165. tmp.forEach(node=>toc.appendChild(node))
  166. let tocContainer = document.createElement("div")
  167. tocContainer.classList.add("container")
  168. strucured.forEach(item=>{
  169. let el = document.createElement("div")
  170. el.classList.add("item")
  171. el.innerHTML = `
  172. ${item.date}<a href="${item.linkNode.href}" id="${item.linkNode.id}">${item.linkNode.textContent}</a>
  173. `
  174. tocContainer.appendChild(el)
  175. })
  176. toc.appendChild(tocContainer)
  177. };

QingJ © 2025

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