知乎列表

将标题列在右侧

  1. // ==UserScript==
  2. // @name 知乎列表
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.0
  5. // @license MPL-2.0
  6. // @description 将标题列在右侧
  7. // @author c4r
  8. // @match https://www.zhihu.com/
  9. // @grant none
  10. // @require https://code.jquery.com/jquery-latest.js
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. /*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
  17. that detects and handles AJAXed content.
  18. auther : BrockA
  19. homepage : https://gist.github.com/BrockA/2625891#file-waitforkeyelements-js
  20. Usage example:
  21.  
  22. waitForKeyElements (
  23. "div.comments"
  24. , commentCallbackFunction
  25. );
  26.  
  27. //--- Page-specific function to do what we want when the node is found.
  28. function commentCallbackFunction (jNode) {
  29. jNode.text ("This comment changed by waitForKeyElements().");
  30. }
  31.  
  32. IMPORTANT: This function requires your script to have loaded jQuery.
  33. */
  34. function waitForKeyElements (
  35. selectorTxt, /* Required: The jQuery selector string that
  36. specifies the desired element(s).
  37. */
  38. actionFunction, /* Required: The code to run when elements are
  39. found. It is passed a jNode to the matched
  40. element.
  41. */
  42. bWaitOnce, /* Optional: If false, will continue to scan for
  43. new elements even after the first match is
  44. found.
  45. */
  46. iframeSelector /* Optional: If set, identifies the iframe to
  47. search.
  48. */
  49. ) {
  50. var targetNodes, btargetsFound;
  51.  
  52. if (typeof iframeSelector == "undefined")
  53. targetNodes = $(selectorTxt);
  54. else
  55. targetNodes = $(iframeSelector).contents ()
  56. .find (selectorTxt);
  57.  
  58. if (targetNodes && targetNodes.length > 0) {
  59. btargetsFound = true;
  60. /*--- Found target node(s). Go through each and act if they
  61. are new.
  62. */
  63. targetNodes.each ( function () {
  64. var jThis = $(this);
  65. var alreadyFound = jThis.data ('alreadyFound') || false;
  66.  
  67. if (!alreadyFound) {
  68. //--- Call the payload function.
  69. var cancelFound = actionFunction (jThis);
  70. if (cancelFound)
  71. btargetsFound = false;
  72. else
  73. jThis.data ('alreadyFound', true);
  74. }
  75. } );
  76. }
  77. else {
  78. btargetsFound = false;
  79. }
  80.  
  81. //--- Get the timer-control variable for this selector.
  82. var controlObj = waitForKeyElements.controlObj || {};
  83. var controlKey = selectorTxt.replace (/[^\w]/g, "_");
  84. var timeControl = controlObj [controlKey];
  85.  
  86. //--- Now set or clear the timer as appropriate.
  87. if (btargetsFound && bWaitOnce && timeControl) {
  88. //--- The only condition where we need to clear the timer.
  89. clearInterval (timeControl);
  90. delete controlObj [controlKey]
  91. }
  92. else {
  93. //--- Set a timer, if needed.
  94. if ( ! timeControl) {
  95. timeControl = setInterval ( function () {
  96. waitForKeyElements ( selectorTxt,
  97. actionFunction,
  98. bWaitOnce,
  99. iframeSelector
  100. );
  101. },
  102. 300
  103. );
  104. controlObj [controlKey] = timeControl;
  105. }
  106. }
  107. waitForKeyElements.controlObj = controlObj;
  108. }
  109.  
  110. function actionFindTitle(){
  111.  
  112. console.log("检测标题");
  113. $("#id-title-list").empty()
  114. $("h2.ContentItem-title").each((index,element) =>{
  115.  
  116. $(element).find("a").attr("id", "id-title-"+(index+1))
  117.  
  118. $("#id-title-list").append("<li><a class=\"Button\" href=\"#" + "id-title-"+(index) + "\">" + $(element).find("a").text() + "</a></li>")
  119.  
  120.  
  121. })
  122. }
  123.  
  124. // ===================
  125.  
  126.  
  127. $( document ).ready(function() {
  128.  
  129.  
  130. $("div.Sticky footer").parent().prepend("<div class=\"Card\"><ul class=\"GlobalSideBar-navList\" style=\"height:200px;overflow:scroll;\" id=\"id-title-list\"> </ul> </div>");
  131. $("main").attr("id", "id-title-0");
  132.  
  133. waitForKeyElements("h2.ContentItem-title",actionFindTitle)
  134. })
  135.  
  136. })();

QingJ © 2025

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