Better Google

Don't be evil

目前为 2020-01-19 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Better Google
  3. // @namespace google
  4. // @version 0.1.12
  5. // @description Don't be evil
  6. // @author aligo, adambh
  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. var linkEl = el.querySelector('.r > a');
  22. var addEl = linkEl.nextSibling;
  23.  
  24. var betterAddEl = document.createElement('div');
  25. betterAddEl.className = 'btrAdd';
  26.  
  27. if (addEl) {
  28. for (var _el of addEl.children) {
  29. if (_el.className.indexOf('TbwUpd') == -1) {
  30. betterAddEl.appendChild(_el);
  31. }
  32. }
  33. }
  34.  
  35. var betterEl = document.createElement('div');
  36. betterEl.className = 'btrG';
  37. betterEl.appendChild(betterAddEl);
  38.  
  39. el.querySelector('.r').appendChild(betterEl);
  40.  
  41. var urlEl = document.createElement('a');
  42. urlEl.href = linkEl.href;
  43. urlEl.target = '_blank';
  44. urlEl.className = 'btrLink';
  45.  
  46. var urlCiteEl = document.createElement('cite');
  47. urlCiteEl.innerText = linkEl.href;
  48. urlCiteEl.className = 'iUh30 bc';
  49. urlEl.appendChild(urlCiteEl);
  50.  
  51.  
  52. var maxWidth = el.clientWidth - betterAddEl.offsetWidth - 10;
  53.  
  54. betterEl.insertBefore(urlEl, betterAddEl);
  55. if (urlEl.offsetWidth > maxWidth) {
  56. urlEl.style.width = maxWidth.toString() + 'px';
  57. }
  58.  
  59.  
  60. tbwUpd.forEach(function(el) { el.remove() });
  61. linkEl.querySelector('h3').previousSibling.remove();
  62. }
  63. }
  64.  
  65. var prevResultCount = 0;
  66. var bettered = false;
  67.  
  68. var runBetterGoogle = function() {
  69. if (prevResultCount != document.querySelectorAll('.g .rc').length) {
  70. document.querySelectorAll('.g .rc').forEach(betterGoogleRow);
  71. prevResultCount = document.querySelectorAll('.g .rc').length;
  72. }
  73. if ( !bettered ) {
  74. if ( MutationObserver != undefined ) {
  75. var searchEl = document.getElementById('rcnt');
  76. var observer = new MutationObserver(runBetterGoogle);
  77. observer.observe(searchEl, {childList: true, subtree: true});
  78. }
  79. bettered = true;
  80. }
  81. };
  82.  
  83. var prepareStyleSheet = function() {
  84. var style = document.createElement('style');
  85. style.setAttribute('media', 'screen');
  86. style.appendChild(document.createTextNode(''));
  87. document.head.appendChild(style);
  88. style.sheet.insertRule('.btrG { word-break: break-all; line-height: 18px; }');
  89. style.sheet.insertRule('.btrG .btrAdd { display: inline-block; vertical-align: top; }');
  90. 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; }');
  91. style.sheet.insertRule('.btrG .btrLink cite.iUh30 { color: #006621; font-size: 16px; }');
  92. };
  93.  
  94. var checkElementThenRun = function(selector, func) {
  95. var el = document.querySelector(selector);
  96. if ( el == null ) {
  97. if (window.requestAnimationFrame != undefined) {
  98. window.requestAnimationFrame(function(){ checkElementThenRun(selector, func)});
  99. } else {
  100. document.addEventListener('readystatechange', function(e) {
  101. if (document.readyState == 'complete') {
  102. func();
  103. }
  104. });
  105. }
  106. } else {
  107. func();
  108. }
  109. }
  110.  
  111. checkElementThenRun('head', prepareStyleSheet);
  112. checkElementThenRun('#rcnt', runBetterGoogle);
  113. })();