Google interface cleanup

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

  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 129
  6. // @match https://*.google.com/search*
  7. // @match https://*.google.ca/search*
  8. // @match https://*.google.fr/search*
  9. // @match https://*.google.co.uk/search*
  10. // @run-at document-end
  11. // @namespace https://greasyfork.org/users/1354160
  12. // ==/UserScript==
  13.  
  14. const annoyances = [
  15. 'People also ask',
  16. 'People also search for',
  17. 'People also search',
  18. 'Videos',
  19. 'Short videos',
  20. 'Refine this search',
  21. 'Search a song',
  22. 'Related searches',
  23. 'Hum to search',
  24. 'Trending videos',
  25. 'Related 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. 'Related topics',
  48. 'Context',
  49. 'For reference',
  50. 'Helpful context',
  51. 'Recipes'
  52. ]
  53.  
  54. function waitForKeyElements(selectorOrFunction, callback, waitOnce, interval, maxIntervals) {
  55. if (typeof waitOnce === "undefined") {
  56. waitOnce = true;
  57. }
  58. if (typeof interval === "undefined") {
  59. interval = 300;
  60. }
  61. if (typeof maxIntervals === "undefined") {
  62. maxIntervals = -1;
  63. }
  64. var targetNodes =
  65. typeof selectorOrFunction === "function" ?
  66. selectorOrFunction() :
  67. document.querySelectorAll(selectorOrFunction);
  68.  
  69. var targetsFound = targetNodes && targetNodes.length > 0;
  70. if (targetsFound) {
  71. targetNodes.forEach(function (targetNode) {
  72. var attrAlreadyFound = "data-userscript-alreadyFound";
  73. var alreadyFound = targetNode.getAttribute(attrAlreadyFound) || false;
  74. if (!alreadyFound) {
  75. var cancelFound = callback(targetNode);
  76. if (cancelFound) {
  77. targetsFound = false;
  78. } else {
  79. targetNode.setAttribute(attrAlreadyFound, true);
  80. }
  81. }
  82. });
  83. }
  84.  
  85. if (maxIntervals !== 0 && !(targetsFound && waitOnce)) {
  86. maxIntervals -= 1;
  87. setTimeout(function () {
  88. waitForKeyElements(selectorOrFunction, callback, waitOnce, interval, maxIntervals);
  89. }, interval);
  90. }
  91. }
  92.  
  93. // Where el is the DOM element you'd like to test for visibility
  94. function isHidden(el) {
  95. if (el === null) {
  96. return true;
  97. } else {
  98. return (el.offsetParent === null)
  99. }
  100. }
  101.  
  102. function getbyXpath(xpath, contextNode) {
  103. let results = [];
  104. let query = document.evaluate(xpath, contextNode || document,
  105. null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  106. for (let i = 0, length = query.snapshotLength; i < length; ++i) {
  107. results.push(query.snapshotItem(i));
  108. }
  109. return results;
  110. }
  111.  
  112. function removeJunk(jNode) {
  113. let div = jNode
  114.  
  115. let matchingAnnoyances =
  116. annoyances
  117. .filter(a => div.innerHTML.indexOf(a) != -1)
  118. .flatMap(a => {
  119. let k = getbyXpath(`.//div[starts-with(text(), '${a}')]|//span[starts-with(text(), '${a}')]|//h2[starts-with(text(), '${a}')]`, div)
  120. // console.log(a, k)
  121. return k
  122. })
  123. .filter(node => !isHidden(node));
  124.  
  125. // console.log(matchingAnnoyances)
  126.  
  127. matchingAnnoyances.forEach(matchingAnnoyance => {
  128. if (matchingAnnoyance && !isHidden(matchingAnnoyance)) {
  129. console.log(div, matchingAnnoyance)
  130. traverseAncestors(matchingAnnoyance)
  131. }
  132. });
  133. }
  134.  
  135. function undesiredElement(jNode) {
  136. jNode.style.display = 'none'
  137. }
  138.  
  139. function destroyElement(jNode) {
  140. jNode.remove()
  141. }
  142.  
  143. function undesiredElementParent(jNode) {
  144. let parent = jNode.parentElement;
  145.  
  146. if (parent !== null) {
  147. parent.style.display = 'none';
  148. }
  149. }
  150.  
  151. function traverseAncestors(node) {
  152. if (node) {
  153. if (node.tagName == 'DIV') {
  154. let parentElement = node.parentElement
  155. let childDivs = [...parentElement.children].filter(c => c.tagName == "DIV")
  156. let hasInfoSection = node.querySelector('.kp-wholepage')
  157. // console.log(childDivs)
  158.  
  159. if (((childDivs.length > 1) && (node.hasAttribute('jsdata') || node.className == 'MjjYud')) || ((childDivs.length == 1) && (parentElement.id == 'bres'))) {
  160. // console.log(node)
  161. if (hasInfoSection === null) {
  162. node.style.display = 'none';
  163. }
  164. } else {
  165. traverseAncestors(node.parentNode);
  166. }
  167. } else traverseAncestors(node.parentNode)
  168. }
  169. }
  170.  
  171. function removeSearchSuggestions(jNode) {
  172. jNode.removeAttribute("jscontroller")
  173. }
  174.  
  175. function visualDigest(jNode) {
  176. jNode.closest('div.ycw3p').style.display = 'none'
  177. }
  178.  
  179. waitForKeyElements('#rso div.MjjYud', removeJunk);
  180. waitForKeyElements('#botstuff div.MjjYud', removeJunk, false);
  181. waitForKeyElements('#botstuff #bres div[id*=dub_]', undesiredElement);
  182. waitForKeyElements('#media_result_group', undesiredElement)
  183. waitForKeyElements('g-card:has(> div[class="mnr-c"])', undesiredElement, false)
  184. waitForKeyElements('div[data-attrid="VisualDigestFullBleedVideoResult"]', undesiredElement)
  185. waitForKeyElements('inline-video', undesiredElement)
  186. waitForKeyElements('product-viewer-group', undesiredElement, false)
  187. waitForKeyElements('block-component', undesiredElement, false) // featured snippets at top
  188. waitForKeyElements('form[action="/search"] > div > div[jscontroller]', removeSearchSuggestions)
  189. waitForKeyElements('div[data-attrid="VisualDigestNewsArticleResult"]', visualDigest)
  190. waitForKeyElements('div[data-attrid="VisualDigestSocialMediaResult"]', visualDigest)
  191. waitForKeyElements('div[data-attrid="VisualDigestWebResult"]', visualDigest)