Google interface cleanup

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

当前为 2024-10-14 提交的版本,查看 最新版本

  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 127
  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. 'For context',
  26. 'Also searched for',
  27. 'Often searched together',
  28. 'Others searched',
  29. 'Local news',
  30. 'Popular on X',
  31. 'People also watch',
  32. 'Events',
  33. 'Profiles',
  34. 'Perspectives',
  35. 'What to watch',
  36. 'Posts on X',
  37. 'Nearby stores',
  38. 'People also buy from',
  39. 'Trending movies',
  40. 'Ticket prices',
  41. 'Mentioned in the news',
  42. 'Visual stories',
  43. 'Latest posts from',
  44. 'Twitter Results',
  45. 'Images',
  46. 'Related topics',
  47. 'Context',
  48. 'For reference',
  49. 'Helpful context',
  50. 'Recipes'
  51. ]
  52.  
  53. function waitForKeyElements(selectorOrFunction, callback, waitOnce, interval, maxIntervals) {
  54. if (typeof waitOnce === "undefined") {
  55. waitOnce = true;
  56. }
  57. if (typeof interval === "undefined") {
  58. interval = 300;
  59. }
  60. if (typeof maxIntervals === "undefined") {
  61. maxIntervals = -1;
  62. }
  63. var targetNodes =
  64. typeof selectorOrFunction === "function" ?
  65. selectorOrFunction() :
  66. document.querySelectorAll(selectorOrFunction);
  67.  
  68. var targetsFound = targetNodes && targetNodes.length > 0;
  69. if (targetsFound) {
  70. targetNodes.forEach(function (targetNode) {
  71. var attrAlreadyFound = "data-userscript-alreadyFound";
  72. var alreadyFound = targetNode.getAttribute(attrAlreadyFound) || false;
  73. if (!alreadyFound) {
  74. var cancelFound = callback(targetNode);
  75. if (cancelFound) {
  76. targetsFound = false;
  77. } else {
  78. targetNode.setAttribute(attrAlreadyFound, true);
  79. }
  80. }
  81. });
  82. }
  83.  
  84. if (maxIntervals !== 0 && !(targetsFound && waitOnce)) {
  85. maxIntervals -= 1;
  86. setTimeout(function () {
  87. waitForKeyElements(selectorOrFunction, callback, waitOnce, interval, maxIntervals);
  88. }, interval);
  89. }
  90. }
  91.  
  92. // Where el is the DOM element you'd like to test for visibility
  93. function isHidden(el) {
  94. if (el === null) {
  95. return true;
  96. } else {
  97. return (el.offsetParent === null)
  98. }
  99. }
  100.  
  101. function getbyXpath(xpath, contextNode) {
  102. let results = [];
  103. let query = document.evaluate(xpath, contextNode || document,
  104. null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  105. for (let i = 0, length = query.snapshotLength; i < length; ++i) {
  106. results.push(query.snapshotItem(i));
  107. }
  108. return results;
  109. }
  110.  
  111. function removeJunk(jNode) {
  112. let div = jNode
  113.  
  114. let matchingAnnoyances =
  115. annoyances
  116. .filter(a => div.innerHTML.indexOf(a) != -1)
  117. .flatMap(a => {
  118. let k = getbyXpath(`.//div[starts-with(text(), '${a}')]|//span[starts-with(text(), '${a}')]|//h2[starts-with(text(), '${a}')]`, div)
  119. // console.log(a, k)
  120. return k
  121. })
  122. .filter(node => !isHidden(node));
  123.  
  124. // console.log(matchingAnnoyances)
  125.  
  126. matchingAnnoyances.forEach(matchingAnnoyance => {
  127. if (matchingAnnoyance && !isHidden(matchingAnnoyance)) {
  128. console.log(div, matchingAnnoyance)
  129. traverseAncestors(matchingAnnoyance)
  130. }
  131. });
  132. }
  133.  
  134. function undesiredElement(jNode) {
  135. jNode.style.display = 'none'
  136. }
  137.  
  138. function destroyElement(jNode) {
  139. jNode.remove()
  140. }
  141.  
  142. function undesiredElementParent(jNode) {
  143. let parent = jNode.parentElement;
  144.  
  145. if (parent !== null) {
  146. parent.style.display = 'none';
  147. }
  148. }
  149.  
  150. function traverseAncestors(node) {
  151. if (node) {
  152. if (node.tagName == 'DIV') {
  153. let parentElement = node.parentElement
  154. let childDivs = [...parentElement.children].filter(c => c.tagName == "DIV")
  155. let hasInfoSection = node.querySelector('.kp-wholepage')
  156. // console.log(childDivs)
  157.  
  158. if (((childDivs.length > 1) && (node.hasAttribute('jsdata') || node.className == 'MjjYud')) || ((childDivs.length == 1) && (parentElement.id == 'bres'))) {
  159. // console.log(node)
  160. if (hasInfoSection === null) {
  161. node.style.display = 'none';
  162. }
  163. } else {
  164. traverseAncestors(node.parentNode);
  165. }
  166. } else traverseAncestors(node.parentNode)
  167. }
  168. }
  169.  
  170. function removeSearchSuggestions(jNode) {
  171. jNode.removeAttribute("jscontroller")
  172. }
  173.  
  174. function visualDigest(jNode) {
  175. jNode.closest('div.ycw3p').style.display = 'none'
  176. }
  177.  
  178. function dynamicFilters(jNode) {
  179. jNode.style.display = 'none';
  180.  
  181. jNode.closest('#hdtb-sc').classList.remove('boECb')
  182. }
  183.  
  184. waitForKeyElements('#rso div.MjjYud', removeJunk);
  185. waitForKeyElements('#botstuff div.MjjYud', removeJunk, false);
  186. waitForKeyElements('#botstuff #bres div[id*=dub_]', undesiredElement);
  187. waitForKeyElements('#media_result_group', undesiredElement)
  188. waitForKeyElements('g-card:has(> div[class="mnr-c"])', undesiredElement, false)
  189. waitForKeyElements('div[data-attrid="VisualDigestFullBleedVideoResult"]', undesiredElement)
  190. waitForKeyElements('inline-video', undesiredElement)
  191. waitForKeyElements('product-viewer-group', undesiredElement, false)
  192. waitForKeyElements('block-component', undesiredElement, false) // featured snippets at top
  193. waitForKeyElements('form[action="/search"] > div > div[jscontroller]', removeSearchSuggestions)
  194. waitForKeyElements('div[data-attrid="VisualDigestNewsArticleResult"]', visualDigest)
  195. waitForKeyElements('div[data-attrid="VisualDigestSocialMediaResult"]', visualDigest)
  196. waitForKeyElements('div[data-attrid="VisualDigestWebResult"]', visualDigest)
  197. waitForKeyElements('span[data-ad="50ms"]', dynamicFilters)