python-search-no-highlight

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

目前為 2021-02-16 提交的版本,檢視 最新版本

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