Google interface cleanup

Remove junk from Google search results like "People also ask", etc.

当前为 2024-08-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Google interface cleanup
  3. // @description Remove junk from Google search results like "People also ask", etc.
  4. // @license MIT
  5. // @version 109
  6. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
  7. // @match https://*.google.com/search*
  8. // @match https://*.google.ca/search*
  9. // @match https://*.google.fr/search*
  10. // @match https://*.google.co.uk/search
  11. // @run-at document-idle
  12. // @namespace https://greasyfork.org/users/1354160
  13. // ==/UserScript==
  14.  
  15. const annoyances = [
  16. 'People also ask',
  17. 'People also search for',
  18. 'People also search',
  19. 'Videos',
  20. 'Short videos',
  21. 'Refine this search',
  22. 'Search a song',
  23. 'Related searches',
  24. 'Hum to search',
  25. 'Trending videos',
  26. 'For context',
  27. 'Also searched for',
  28. 'Often searched together',
  29. 'Others searched',
  30. 'Local news',
  31. 'Popular on X',
  32. 'People also watch',
  33. 'Events',
  34. 'Profiles',
  35. 'Perspectives',
  36. 'What to watch',
  37. 'Posts on X',
  38. 'Nearby stores',
  39. 'People also buy from',
  40. 'Trending movies',
  41. 'Ticket prices',
  42. 'Mentioned in the news',
  43. 'Visual stories',
  44. 'Latest posts from',
  45. 'Twitter Results',
  46. 'Images'
  47. ]
  48.  
  49. /*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
  50. that detects and handles AJAXed content.
  51.  
  52. Usage example:
  53.  
  54. waitForKeyElements (
  55. "div.comments"
  56. , commentCallbackFunction
  57. );
  58.  
  59. //--- Page-specific function to do what we want when the node is found.
  60. function commentCallbackFunction (jNode) {
  61. jNode.text ("This comment changed by waitForKeyElements().");
  62. }
  63.  
  64. IMPORTANT: This function requires your script to have loaded jQuery.
  65. */
  66. function waitForKeyElements (
  67. selectorTxt, /* Required: The jQuery selector string that
  68. specifies the desired element(s).
  69. */
  70. actionFunction, /* Required: The code to run when elements are
  71. found. It is passed a jNode to the matched
  72. element.
  73. */
  74. bWaitOnce, /* Optional: If false, will continue to scan for
  75. new elements even after the first match is
  76. found.
  77. */
  78. iframeSelector /* Optional: If set, identifies the iframe to
  79. search.
  80. */
  81. ) {
  82. var targetNodes, btargetsFound;
  83.  
  84. if (typeof iframeSelector == "undefined")
  85. targetNodes = $(selectorTxt);
  86. else
  87. targetNodes = $(iframeSelector).contents ()
  88. .find (selectorTxt);
  89.  
  90. if (targetNodes && targetNodes.length > 0) {
  91. btargetsFound = true;
  92. /*--- Found target node(s). Go through each and act if they
  93. are new.
  94. */
  95. targetNodes.each ( function () {
  96. var jThis = $(this);
  97. var alreadyFound = jThis.data ('alreadyFound') || false;
  98.  
  99. if (!alreadyFound) {
  100. //--- Call the payload function.
  101. var cancelFound = actionFunction (jThis);
  102. if (cancelFound)
  103. btargetsFound = false;
  104. else
  105. jThis.data ('alreadyFound', true);
  106. }
  107. } );
  108. }
  109. else {
  110. btargetsFound = false;
  111. }
  112.  
  113. //--- Get the timer-control variable for this selector.
  114. var controlObj = waitForKeyElements.controlObj || {};
  115. var controlKey = selectorTxt.replace (/[^\w]/g, "_");
  116. var timeControl = controlObj [controlKey];
  117.  
  118. //--- Now set or clear the timer as appropriate.
  119. if (btargetsFound && bWaitOnce && timeControl) {
  120. //--- The only condition where we need to clear the timer.
  121. clearInterval (timeControl);
  122. delete controlObj [controlKey]
  123. }
  124. else {
  125. //--- Set a timer, if needed.
  126. if ( ! timeControl) {
  127. timeControl = setInterval ( function () {
  128. waitForKeyElements ( selectorTxt,
  129. actionFunction,
  130. bWaitOnce,
  131. iframeSelector
  132. );
  133. },
  134. 300
  135. );
  136. controlObj [controlKey] = timeControl;
  137. }
  138. }
  139. waitForKeyElements.controlObj = controlObj;
  140. }
  141.  
  142. // Where el is the DOM element you'd like to test for visibility
  143. function isHidden(el) {
  144. if (el === null) {
  145. return true;
  146. } else {
  147. return (el.offsetParent === null)
  148. }
  149. }
  150.  
  151. function getbyXpath(xpath, contextNode) {
  152. let results = [];
  153. let query = document.evaluate(xpath, contextNode || document,
  154. null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  155. for (let i = 0, length = query.snapshotLength; i < length; ++i) {
  156. results.push(query.snapshotItem(i));
  157. }
  158. return results;
  159. }
  160.  
  161. function removeJunk(jNode) {
  162. let div = jNode[0]
  163.  
  164. let matchingAnnoyances =
  165. annoyances
  166. .filter(a => div.innerHTML.indexOf(a) != -1)
  167. .flatMap(a => {
  168. let k = getbyXpath(`.//div[starts-with(text(), '${a}')]|//span[starts-with(text(), '${a}')]|//h2[starts-with(text(), '${a}')]`, div)
  169. // console.log(a, k)
  170. return k
  171. })
  172. .filter(node => !isHidden(node));
  173.  
  174. // console.log(matchingAnnoyances)
  175.  
  176. matchingAnnoyances.forEach(matchingAnnoyance => {
  177. if (matchingAnnoyance && !isHidden(matchingAnnoyance)) {
  178. console.log(div, matchingAnnoyance)
  179. traverseAncestors(matchingAnnoyance)
  180. }
  181. });
  182. }
  183.  
  184. function undesiredElement(jNode) {
  185. jNode[0].style.display = 'none'
  186. }
  187.  
  188. function destroyElement(jNode) {
  189. jNode[0].remove()
  190. }
  191.  
  192. function undesiredElementParent(jNode) {
  193. let parent = jNode[0].parentElement;
  194.  
  195. if (parent !== null) {
  196. parent.style.display = 'none';
  197. }
  198. }
  199.  
  200. function traverseAncestors(node) {
  201. if (node) {
  202. if (node.tagName == 'DIV') {
  203. let parentElement = node.parentElement
  204. let childDivs = [...parentElement.children].filter(c => c.tagName == "DIV")
  205. // console.log(childDivs)
  206.  
  207. if (((childDivs.length > 1) && (node.hasAttribute('jsdata'))) || node.className == 'MjjYud') {
  208. // console.log(node)
  209. node.style.display = 'none';
  210. } else {
  211. traverseAncestors(node.parentNode);
  212. }
  213. } else traverseAncestors(node.parentNode)
  214. }
  215. }
  216.  
  217. waitForKeyElements('#rso div.MjjYud', removeJunk);
  218. waitForKeyElements('#botstuff div.MjjYud', removeJunk);
  219. waitForKeyElements('#iur div[jscontroller]', undesiredElement)
  220. waitForKeyElements('div[data-abe]', undesiredElement);
  221. waitForKeyElements('g-popup', undesiredElement)
  222. waitForKeyElements('div[data-peekaboo]', undesiredElement)
  223. waitForKeyElements('.U3THc', undesiredElement)
  224. waitForKeyElements('body #lb', destroyElement)
  225. waitForKeyElements('.PNZEbe', undesiredElementParent);
  226. waitForKeyElements('div[data-initq]', undesiredElement)
  227. waitForKeyElements('div[data-has-close]', undesiredElement)
  228. waitForKeyElements('#media_result_group', undesiredElement)
  229. waitForKeyElements('div[data-attrid="VisualDigestFullBleedVideoResult"]', undesiredElement)
  230. waitForKeyElements('inline-video', undesiredElement)
  231. waitForKeyElements('product-viewer-group', undesiredElement)
  232. waitForKeyElements('#iur', undesiredElement)