python-search-no-highlight

Disables highlighting in docs.python.org's search results.

  1. // ==UserScript==
  2. // @name python-search-no-highlight
  3. // @version 0.2
  4. // @description Disables highlighting in docs.python.org's search results.
  5. // @match *://*.docs.python.org/*/search.html*
  6. // @namespace https://gf.qytechs.cn/users/217495-eric-toombs
  7. // ==/UserScript==
  8.  
  9.  
  10. script_text = `
  11. Search.query = function(query) {
  12. var i;
  13.  
  14. // stem the searchterms and add them to the correct list
  15. var stemmer = new Stemmer();
  16. var searchterms = [];
  17. var excluded = [];
  18. var hlterms = [];
  19. var tmp = splitQuery(query);
  20. var objectterms = [];
  21. for (i = 0; i < tmp.length; i++) {
  22. if (tmp[i] !== "") {
  23. objectterms.push(tmp[i].toLowerCase());
  24. }
  25.  
  26. if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\\d+$/) ||
  27. tmp[i] === "") {
  28. // skip this "word"
  29. continue;
  30. }
  31. // stem the word
  32. var word = stemmer.stemWord(tmp[i].toLowerCase());
  33. // prevent stemmer from cutting word smaller than two chars
  34. if(word.length < 3 && tmp[i].length >= 3) {
  35. word = tmp[i];
  36. }
  37. var toAppend;
  38. // select the correct list
  39. if (word[0] == '-') {
  40. toAppend = excluded;
  41. word = word.substr(1);
  42. }
  43. else {
  44. toAppend = searchterms;
  45. hlterms.push(tmp[i].toLowerCase());
  46. }
  47. // only add if not already in the list
  48. if (!$u.contains(toAppend, word))
  49. toAppend.push(word);
  50. }
  51. var highlightstring = '';
  52.  
  53. // console.debug('SEARCH: searching for:');
  54. // console.info('required: ', searchterms);
  55. // console.info('excluded: ', excluded);
  56.  
  57. // prepare search
  58. var terms = Search._index.terms;
  59. var titleterms = Search._index.titleterms;
  60.  
  61. // array of [filename, title, anchor, descr, score]
  62. var results = [];
  63. $('#search-progress').empty();
  64.  
  65. // lookup as object
  66. for (i = 0; i < objectterms.length; i++) {
  67. var others = [].concat(objectterms.slice(0, i),
  68. objectterms.slice(i+1, objectterms.length));
  69. results = results.concat(Search.performObjectSearch(objectterms[i], others));
  70. }
  71.  
  72. // lookup as search terms in fulltext
  73. results = results.concat(Search.performTermsSearch(searchterms, excluded, terms, titleterms));
  74.  
  75. // let the scorer override scores with a custom scoring function
  76. if (Scorer.score) {
  77. for (i = 0; i < results.length; i++)
  78. results[i][4] = Scorer.score(results[i]);
  79. }
  80.  
  81. // now sort the results by score (in opposite order of appearance, since the
  82. // display function below uses pop() to retrieve items) and then
  83. // alphabetically
  84. results.sort(function(a, b) {
  85. var left = a[4];
  86. var right = b[4];
  87. if (left > right) {
  88. return 1;
  89. } else if (left < right) {
  90. return -1;
  91. } else {
  92. // same score: sort alphabetically
  93. left = a[1].toLowerCase();
  94. right = b[1].toLowerCase();
  95. return (left > right) ? -1 : ((left < right) ? 1 : 0);
  96. }
  97. });
  98.  
  99. // for debugging
  100. //Search.lastresults = results.slice(); // a copy
  101. //console.info('search results:', Search.lastresults);
  102.  
  103. // print the results
  104. var resultCount = results.length;
  105. function displayNextItem() {
  106. // results left, load the summary and display it
  107. if (results.length) {
  108. var item = results.pop();
  109. var listItem = $('<li style="display:none"></li>');
  110. var requestUrl = "";
  111. if (DOCUMENTATION_OPTIONS.BUILDER === 'dirhtml') {
  112. // dirhtml builder
  113. var dirname = item[0] + '/';
  114. if (dirname.match(/\\/index\\/$/)) {
  115. dirname = dirname.substring(0, dirname.length-6);
  116. } else if (dirname == 'index/') {
  117. dirname = '';
  118. }
  119. requestUrl = DOCUMENTATION_OPTIONS.URL_ROOT + dirname;
  120.  
  121. } else {
  122. // normal html builders
  123. requestUrl = DOCUMENTATION_OPTIONS.URL_ROOT + item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX;
  124. }
  125. listItem.append($('<a/>').attr('href',
  126. requestUrl +
  127. highlightstring + item[2]).html(item[1]));
  128. if (item[3]) {
  129. listItem.append($('<span> (' + item[3] + ')</span>'));
  130. Search.output.append(listItem);
  131. listItem.slideDown(5, function() {
  132. displayNextItem();
  133. });
  134. } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
  135. $.ajax({url: requestUrl,
  136. dataType: "text",
  137. complete: function(jqxhr, textstatus) {
  138. var data = jqxhr.responseText;
  139. if (data !== '' && data !== undefined) {
  140. listItem.append(Search.makeSearchSummary(data, searchterms, hlterms));
  141. }
  142. Search.output.append(listItem);
  143. listItem.slideDown(5, function() {
  144. displayNextItem();
  145. });
  146. }});
  147. } else {
  148. // no source available, just display title
  149. Search.output.append(listItem);
  150. listItem.slideDown(5, function() {
  151. displayNextItem();
  152. });
  153. }
  154. }
  155. // search finished, update title and status message
  156. else {
  157. Search.stopPulse();
  158. Search.title.text(_('Search Results'));
  159. if (!resultCount)
  160. Search.status.text(_('Your search did not match any documents. Please make sure that all words are spelled correctly and that you\\'ve selected enough categories.'));
  161. else
  162. Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
  163. Search.status.fadeIn(500);
  164. }
  165. }
  166. displayNextItem();
  167. };
  168. `;
  169.  
  170.  
  171. script = document.createElement('script');
  172. script.type = 'text/javascript';
  173. script.text = script_text;
  174. document.getElementsByTagName('head')[0].appendChild(script);

QingJ © 2025

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