Display Toc On kernel.org[JS|UI]

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

当前为 2024-10-26 提交的版本,查看 最新版本

  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.2
  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. text-wrap: wrap!important;
  81. max-width: 400px;
  82. }
  83. body > pre.toc .container{
  84. /* display: flex;/ */
  85. /* flex-direction: column; */
  86. }
  87. body > pre.toc .container .item{
  88. white-space: pre;
  89. text-wrap: wrap;
  90. margin: 0;
  91. padding: 0;
  92. /* display: inline-block; */
  93.  
  94. /* max-width:200px!important; */
  95. /* overflow: hidden; */
  96. }`)
  97. window.onload = () => {
  98. document.querySelectorAll("*.q").forEach((el) => {
  99. el.childNodes.forEach((child) => {
  100. if (child.nodeType === Node.TEXT_NODE) {
  101. // console.log(child.textContent);
  102. // child.textContent = child.textContent.replace(/^>\n\[\.\.\.\]/gm, "> [...]");
  103. // child.textContent = child.textContent.replace(/^>( |$)/gm, " ");
  104. }
  105. // if (child.nodeType === Node.ELEMENT_NODE) console.log(child.innerHTML);
  106. });
  107. });
  108. let pre = document.querySelectorAll("body > pre")[1];
  109. pre.remove();
  110. pre.classList.add("toc");
  111. document.body.insertBefore(pre, document.querySelector("body > form"));
  112. let tmp = [];
  113. /**
  114. * @type {{date:string,linkNode:ChildNode}[]}
  115. */
  116. let strucured = [];
  117. let parse = false;
  118. let toc = document.querySelector("pre.toc")
  119. toc.childNodes.forEach((node) => {
  120. if (
  121. node.nodeType === Node.TEXT_NODE &&
  122. node.textContent.indexOf(
  123. "-- links below jump to the message on this page --"
  124. ) != -1
  125. ) {
  126. parse = true;
  127. let idx =
  128. node.textContent.indexOf(
  129. "-- links below jump to the message on this page --"
  130. ) + "-- links below jump to the message on this page --".length;
  131. let tmpNode = document.createTextNode(node.textContent.substring(0, idx));
  132. tmp.push(tmpNode);
  133. node = document.createTextNode(node.textContent.substring(idx));
  134. // console.log(idx, node.textContent, tmpNode.textContent);
  135. } else {
  136. if (!parse) tmp.push(node);
  137. }
  138. // console.log(parse, node);
  139. if (parse) {
  140. if (node.nodeType === Node.TEXT_NODE) {
  141. let content = node.textContent.trimStart();
  142. if (!content) return;
  143. if (!/^\d{4}-\d{2}-\d{2}/.test(content)) {
  144. let match = /^\s*(.*)\s*(\d{4}-\d{2}-\d{2} +\d+:\d+.*)/gs.exec(content);
  145. if(!match){
  146. strucured.push({
  147. date: content,
  148. linkNode: null,
  149. });
  150. return;
  151. }
  152. console.log(content, match);
  153. strucured.push({
  154. date: match[2],
  155. linkNode: match[1],
  156. });
  157. } else
  158. strucured.push({
  159. date: content,
  160. linkNode: null,
  161. });
  162. } else {
  163. if (typeof strucured[strucured.length - 1].linkNode ==="string") {
  164. console.log(strucured[strucured.length - 1])
  165. node.textContent += " "+strucured[strucured.length - 1].linkNode;
  166. }
  167. strucured[strucured.length - 1].linkNode = node;
  168. }
  169. }
  170. });
  171. console.log(tmp);
  172. console.log(strucured);
  173. toc.innerHTML=""
  174. tmp.forEach(node=>toc.appendChild(node))
  175. let tocContainer = document.createElement("div")
  176. tocContainer.classList.add("container")
  177. strucured.forEach(item=>{
  178. let el = document.createElement("div")
  179. el.classList.add("item")
  180. el.innerHTML = `${item.date}<a href="${item.linkNode.href}" id="${item.linkNode.id}">${item.linkNode.textContent}</a>`
  181. tocContainer.appendChild(el)
  182. })
  183. toc.appendChild(tocContainer)
  184. };

QingJ © 2025

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