Better Google

Don't be evil

目前為 2020-01-17 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Better Google
  3. // @namespace google
  4. // @version 0.1.9
  5. // @description Don't be evil
  6. // @author aligo
  7. // @match https://*.google.com/search?*
  8. // @include /^https?://(?:www|encrypted|ipv[46])\.google\.[^/]+/(?:$|[#?]|search|webhp)/
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. var betterGoogleRow = (el) => {
  16. var tbwUpd = el.querySelectorAll('.TbwUpd');
  17. if (tbwUpd.length > 0) {
  18. var linkEl = el.querySelector('.r > a');
  19. var addEl = linkEl.nextSibling;
  20.  
  21. var betterAddEl = document.createElement('div');
  22. betterAddEl.style.display = 'inline-block';
  23. betterAddEl.style.position = 'relative';
  24. betterAddEl.style.verticalAlign = 'top';
  25.  
  26. if (addEl) {
  27. for (var _el of addEl.children) {
  28. if (_el.className.indexOf('TbwUpd') == -1) {
  29. betterAddEl.appendChild(_el);
  30. }
  31. }
  32. }
  33.  
  34. var betterEl = document.createElement('div');
  35. betterEl.style.wordBreak = 'break-all';
  36. betterEl.style.lineHeight = '18px';
  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.innerText = linkEl.href;
  45. urlEl.style.lineHeight = '18px';
  46. urlEl.style.marginTop = '2px';
  47. urlEl.style.display = 'inline-block';
  48. urlEl.style.color = '#006621';
  49. urlEl.style.fontSize = '16px';
  50. urlEl.style.overflow = 'hidden';
  51. urlEl.style.textOverflow = 'ellipsis';
  52. urlEl.style.whiteSpace = 'nowrap';
  53. urlEl.style.verticalAlign = 'top';
  54. urlEl.style.textDecoration = 'none';
  55.  
  56. var maxWidth = el.clientWidth - betterAddEl.offsetWidth - 10;
  57.  
  58. betterEl.insertBefore(urlEl, betterAddEl);
  59. if (urlEl.offsetWidth > maxWidth) {
  60. urlEl.style.width = maxWidth.toString() + 'px';
  61. }
  62.  
  63.  
  64. tbwUpd.forEach(el => el.remove());
  65. linkEl.querySelector('h3').previousSibling.remove();
  66. }
  67. }
  68.  
  69. var prevResultCount = 0;
  70. var runBetterGoogle = () => {
  71. if (prevResultCount != document.querySelectorAll('.g .rc').length) {
  72. document.querySelectorAll('.g .rc').forEach(betterGoogleRow);
  73. prevResultCount = document.querySelectorAll('.g .rc').length;
  74. }
  75. };
  76.  
  77. runBetterGoogle();
  78.  
  79. var searchEl = document.getElementById('search');
  80. var observer = new MutationObserver(runBetterGoogle);
  81. observer.observe(searchEl, {childList: true, subtree: true});
  82. })();