OldGoogleIsBetter

Old google results is better

  1. // ==UserScript==
  2. // @name OldGoogleIsBetter
  3. // @namespace google
  4. // @version 0.1
  5. // @description Old google results is better
  6. // @author PINHOf
  7. // @license MIT
  8. // @supportURL https://github.com/PINHOf/OldGoogleIsBetter
  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 prevResultCount = 0;
  19. var bettered = false;
  20.  
  21. checkElementThenRun('#rcnt', run);
  22.  
  23. // Loops through all the results rows & watches for changes in the main div
  24. // Function extracted from https://github.com/aligo/better-google
  25. function run()
  26. {
  27. if (prevResultCount != document.querySelectorAll('.MjjYud').length)
  28. {
  29. document.querySelectorAll('.MjjYud').forEach(row);
  30. prevResultCount = document.querySelectorAll('.MjjYud').length;
  31. }
  32.  
  33. if (!bettered)
  34. {
  35. if (MutationObserver != undefined)
  36. {
  37. var searchEl = document.getElementById('rcnt');
  38. var observer = new MutationObserver(run);
  39. observer.observe(searchEl, {childList: true, subtree: true});
  40. }
  41.  
  42. bettered = true;
  43. }
  44. }
  45.  
  46. // Checks if certain element exists and runs the function associated
  47. // Function extracted from https://github.com/aligo/better-google
  48. function checkElementThenRun(selector, func)
  49. {
  50. var el = document.querySelector(selector);
  51.  
  52. if (el != null)
  53. {
  54. func();
  55. return;
  56. }
  57.  
  58. if (window.requestAnimationFrame != undefined)
  59. window.requestAnimationFrame(function(){ checkElementThenRun(selector, func)});
  60. else
  61. {
  62. document.addEventListener('readystatechange', function(e)
  63. {
  64. if (document.readyState == 'complete')
  65. func();
  66. });
  67. }
  68. }
  69.  
  70. // Formats the row / result to the old style
  71. function row(el)
  72. {
  73. const url = getUrl(el);
  74.  
  75. removeHeaderInformation(el);
  76. removeUnnecessaryLineBreak(el);
  77. adjustTitleMargin(el);
  78. appendUrlAfterTitle(el, url);
  79. }
  80.  
  81. // Extracts the URL from the <a href> result
  82. function getUrl(el)
  83. {
  84. const linkEl = el.querySelector('a');
  85. const url = linkEl ? linkEl.getAttribute('href') : '';
  86.  
  87. return url;
  88. }
  89.  
  90. // Removes the header information (icon; url; three dots; etc)
  91. function removeHeaderInformation(el)
  92. {
  93. el.querySelectorAll('.TbwUpd').forEach((elem) => elem.remove());
  94. el.querySelectorAll('.B6fmyf').forEach((elem) => elem.remove());
  95. }
  96.  
  97. // Removes extra <br> tags
  98. function removeUnnecessaryLineBreak(el)
  99. {
  100. const link = el.querySelector('a');
  101.  
  102. if (link)
  103. link.querySelectorAll('br').forEach((elem) => elem.remove());
  104. }
  105.  
  106. // Adjusts the title margin top (after the header information has been removed from DOM)
  107. function adjustTitleMargin(el)
  108. {
  109. const titleEl = el.querySelector('.LC20lb');
  110.  
  111. if (titleEl)
  112. titleEl.style.marginTop = '0px';
  113. }
  114.  
  115. // Appends the URL after the title in a green color
  116. function appendUrlAfterTitle(el, url)
  117. {
  118. const titleParentEl = el.querySelector('.yuRUbf');
  119. const newEl = document.createElement('a');
  120. newEl.setAttribute('href', url);
  121. newEl.innerHTML = url;
  122. newEl.style.color = '#006621';
  123. newEl.style.fontSize = '15px';
  124. newEl.style.display = 'block';
  125.  
  126. if (titleParentEl)
  127. {
  128. const parent = titleParentEl.parentElement;
  129. const totalLinks = parent.querySelectorAll('a').length;
  130.  
  131. if (totalLinks == 1)
  132. titleParentEl.after(newEl);
  133. }
  134. }
  135. })();