Better Google

Don't be evil::revert google search results to older style

目前为 2023-09-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Better Google
  3. // @namespace google
  4. // @version 0.1.16.5
  5. // @description Don't be evil::revert google search results to older style
  6. // @author aligo, adambh, tejaslodaya, drwonky, yut23
  7. // @license MIT
  8. // @supportURL https://github.com/aligo/better-google
  9. // @match https://*.google.com/search?*
  10. // @include /^https?://(?:www|encrypted|ipv[46])\.google\.[^/]+/(?:$|[#?]|search|webhp)/
  11. // @grant none
  12. // @run-at document-start
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. var betterGoogleRow = function(el) {
  19. var tbwUpd = el.querySelectorAll('.TbwUpd');
  20. if (tbwUpd.length > 0) {
  21. /* Google does A/B testing on the search results page, so the
  22. * structure of the page is not always the same. This code
  23. * tries to find the link element in a few different ways.
  24. * If it can't find it, it just gives up and doesn't do
  25. * anything.
  26. */
  27. var selectors = [
  28. '.yuRUbf > a',
  29. '.yuRUbf > div > a',
  30. '.yuRUbf > div > span > a',
  31. ];
  32. for (const selector of selectors) {
  33. var linkEl = el.querySelector(selector);
  34. if (linkEl) {
  35. break;
  36. }
  37. }
  38. var addEl = linkEl.nextSibling;
  39. if (!addEl) {
  40. // try the parent's sibling, for the span case
  41. addEl = linkEl.parentElement.nextSibling;
  42. }
  43.  
  44. var betterAddEl = document.createElement('div');
  45. betterAddEl.className = 'btrAdd';
  46.  
  47. if (addEl) {
  48. // this loop moves the "More options" button into betterAddEl
  49. for (var i = 0; i < addEl.children.length; i++) {
  50. var _el = addEl.children[i];
  51. if (_el.className.indexOf('TbwUpd') == -1) {
  52. betterAddEl.appendChild(_el);
  53. }
  54. }
  55. } else {
  56. // entry isn't fully loaded yet
  57. betterAddEl.remove();
  58. return;
  59. }
  60.  
  61. var betterEl = document.createElement('div');
  62. betterEl.className = 'btrG';
  63. betterEl.appendChild(betterAddEl);
  64.  
  65. el.appendChild(betterEl);
  66.  
  67. var urlEl = document.createElement('a');
  68. urlEl.href = linkEl.href;
  69. urlEl.target = '_blank';
  70. urlEl.className = 'btrLink';
  71.  
  72. var urlCiteEl = document.createElement('cite');
  73. urlCiteEl.innerText = linkEl.href;
  74. urlCiteEl.className = 'iUh30 bc';
  75. urlEl.appendChild(urlCiteEl);
  76.  
  77.  
  78. var maxWidth = el.clientWidth - betterAddEl.offsetWidth - 10;
  79.  
  80. betterEl.insertBefore(urlEl, betterAddEl);
  81. if (urlEl.offsetWidth > maxWidth) {
  82. urlEl.style.width = maxWidth.toString() + 'px';
  83. }
  84.  
  85. var aboutResult = el.querySelectorAll('.csDOgf');
  86. if (aboutResult.length > 0) {
  87. betterEl.appendChild(aboutResult[0]);
  88. }
  89. tbwUpd.forEach(function(el) { el.remove() });
  90. linkEl.querySelector('br:first-child').remove();
  91. }
  92. }
  93.  
  94. var prevResultCount = 0;
  95. var bettered = false;
  96.  
  97. var runBetterGoogle = function() {
  98. if (prevResultCount != document.querySelectorAll('.g .yuRUbf').length) {
  99. document.querySelectorAll('.g .yuRUbf').forEach(betterGoogleRow);
  100. prevResultCount = document.querySelectorAll('.g .yuRUbf').length;
  101. }
  102. if ( !bettered ) {
  103. if ( MutationObserver != undefined ) {
  104. var searchEl = document.getElementById('rcnt');
  105. var observer = new MutationObserver(runBetterGoogle);
  106. observer.observe(searchEl, {childList: true, subtree: true});
  107. }
  108. bettered = true;
  109. }
  110. };
  111.  
  112. var prepareStyleSheet = function() {
  113. // if dark mode is enabled (either manually or by device default),
  114. // Google adds a meta tag to the document which we can check
  115. var link_color = '#006621';
  116. var meta_color_scheme = document.querySelector('meta[name="color-scheme"]');
  117. if (meta_color_scheme != undefined && meta_color_scheme.content == 'dark') {
  118. // use a lighter green in dark mode
  119. link_color = '#40965b';
  120. }
  121. var style = document.createElement('style');
  122. style.setAttribute('media', 'screen');
  123. style.appendChild(document.createTextNode(''));
  124. document.head.appendChild(style);
  125. style.sheet.insertRule(`:root { --btrG-link-color: ${link_color}; }`);
  126. style.sheet.insertRule('.btrG { word-break: normal; line-height: 18px; }');
  127. style.sheet.insertRule('.btrG .btrAdd { display: inline-block; vertical-align: top; line-height: 0; }');
  128. style.sheet.insertRule('.btrG .btrLink { display: inline-block; vertical-align: top; line-height: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-decoration: none !important; color: var(--btrG-link-color); }');
  129. style.sheet.insertRule('.btrG .btrLink cite.iUh30 { color: var(--btrG-link-color); font-size: 16px; }');
  130. // remove extra space used for new multiline link info card
  131. style.sheet.insertRule('.yuRUbf h3.DKV0Md { margin-top: 0px; }');
  132. };
  133.  
  134. var checkElementThenRun = function(selector, func) {
  135. var el = document.querySelector(selector);
  136. if ( el == null ) {
  137. if (window.requestAnimationFrame != undefined) {
  138. window.requestAnimationFrame(function(){ checkElementThenRun(selector, func)});
  139. } else {
  140. document.addEventListener('readystatechange', function(e) {
  141. if (document.readyState == 'complete') {
  142. func();
  143. }
  144. });
  145. }
  146. } else {
  147. func();
  148. }
  149. }
  150.  
  151. checkElementThenRun('head', prepareStyleSheet);
  152. checkElementThenRun('#rcnt', runBetterGoogle);
  153. })();