Greasy Fork is available in English.

Add Site Search Links To Google Search Result

Add a "Site Results" and "Cached Page" links onto each Google search result entries to search from that site. The link will be added either in the search result entry's popup menu, or after the green URL below the entry's title.

  1. // ==UserScript==
  2. // @name Add Site Search Links To Google Search Result
  3. // @namespace https://greasyfork.org/en/users/85671-jcunews
  4. // @version 1.1.20
  5. // @license AGPL v3
  6. // @author jcunews
  7. // @description Add a "Site Results" and "Cached Page" links onto each Google search result entries to search from that site. The link will be added either in the search result entry's popup menu, or after the green URL below the entry's title.
  8. // @include *://www.google.*/search*
  9. // @include *://www.google.*.*/search*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function(a, t, next) {
  14. function dropdownClick(a) {
  15. this.setAttribute("aria-expanded", a = this.classList.toggle("selected"));
  16. this.parentNode.querySelector(".action-menu-panel").style.visibility = a ? "inherit" : "";
  17. }
  18. function process(m) {
  19. location.search.substring(1).split("&").some(function(v) {
  20. if (v.indexOf("q=") === 0) {
  21. if (a = decodeURIComponent(v.substr(2).trim().replace(/\+/g, " ")).match(/(?:^|\s)site:([^\s]+)/)) {
  22. a = a[1];
  23. } else a = "";
  24. return true;
  25. }
  26. });
  27. document.querySelectorAll(
  28. '#ires .g,#search .g,#rso>.hlcw0c>.g,#rso>div:not([class])>.nChh6e,#rso>div:not(:has(>[class])),#kp-wp-tab-overview>div[data-ved]'
  29. ).forEach(function(entry, menu, point, e, p) {
  30. if (
  31. !(point = entry.querySelector('.r>a,.yuRUbf>a:last-of-type,.yuRUbf>div>a:last-of-type,.dbsr>a,[jsaction]>a[jsname],div[id] div[jsslot] span>span')) ||
  32. entry.querySelector('a.fl.sr')
  33. ) return;
  34. if (menu = entry.querySelector(".action-menu-item")) { //v2: has menu
  35. if ((/:\/\/webcache/).test(menu.firstElementChild.href)) {
  36. e = menu.nextElementSibling;
  37. } else if ((/=related:http/).test(menu.firstElementChild.href)) {
  38. e = menu;
  39. } else e = null;
  40. menu = menu.parentNode.insertBefore(menu.cloneNode(true), e).firstElementChild;
  41. menu.classList.add("sr")
  42. } else if (menu = entry.querySelector(".f+span") || entry.querySelector(".f")) { //v1
  43. menu.insertAdjacentHTML("beforeend", ' - <a class="fl sr"></a> <a class="fl cp"></a>');
  44. menu = menu.lastElementChild.previousElementSibling;
  45. } else { //v2: no menu
  46. if (p = point.querySelector(".XTjFC")) { //news
  47. p.appendChild(menu = document.createElement("SPAN"));
  48. } else if (p = point.parentNode.querySelector(".eFM0qc")) { //video
  49. p.appendChild(menu = document.createElement("SPAN"));
  50. } else { //others
  51. if (m = entry.closest('#kp-wp-tab-overview')) point = entry.querySelector('a');
  52. point.parentNode.insertBefore(menu = document.createElement("SPAN"), point.nextSibling);
  53. if (point.closest('div[jsslot]')) {
  54. point = entry.querySelector('a');
  55. if (m) {
  56. menu.style.cssText = "display:inline-block;position:absolute;right:1em;bottom:1em"
  57. } else menu.style.cssText = "display:inline-block;margin-top:1.8em"
  58. }
  59. }
  60. menu.innerHTML = `${
  61. next ? '' : `<style>
  62. .cvP2Ce {
  63. overflow: revert!important;
  64. contain: revert!important;
  65. }
  66. .action-menu {
  67. display: inline;
  68. margin: 0 3px;
  69. position: relative;
  70. user-select: none;
  71. margin-top: 1px;
  72. vertical-align: middle;
  73. }
  74. .GHDvEf, .GHDvEf:hover, .GHDvEf.selected, .GHDvEf.selected:hover {
  75. display: inline-block;
  76. background-color: #fff;
  77. height: 12px;
  78. margin-top: 1px;
  79. user-select: none;
  80. width: 13px;
  81. }
  82. .mn-dwn-arw {
  83. border-color: #70757a transparent;
  84. border-style: solid;
  85. border-width: 5px 4px 0 4px;
  86. width: 0;
  87. height: 0;
  88. margin-left: -2px;
  89. top: 50%;
  90. margin-top: -2px;
  91. position: absolute;
  92. }
  93. .action-menu .mn-dwn-arw {
  94. border-color: #202124 transparent;
  95. margin-top: -3px;
  96. margin-left: 3px;
  97. left: 0;
  98. border-color: #70757a transparent;
  99. }
  100. .action-menu-panel {
  101. padding: 6px 0;
  102. position: absolute;
  103. left: 0;
  104. padding: 0;
  105. top: 12px;
  106. visibility: hidden;
  107. background: #fff;
  108. border: 1px solid #dadce0;
  109. border: 1px solid rgba(0,0,0,.20);
  110. font-size: 13px;
  111. white-space: nowrap;
  112. z-index: 3;
  113. transition: opacity 0.218s;
  114. box-shadow: 0 2px 4px rgba(0,0,0,.20);
  115. }
  116. div#gsr .action-menu-panel {
  117. left: auto;
  118. right: 0;
  119. }
  120. .action-menu-item {
  121. cursor: pointer;
  122. user-select: none;
  123. }
  124. .action-menu-item {
  125. margin: 0;
  126. padding: 0;
  127. -moz-user-select: none;
  128. }
  129. .action-menu-item:hover {
  130. background-color: #f8f9fa;
  131. }
  132. #rcnt .action-menu-item a.fl, .action-menu-item a.fl {
  133. color: #3c4043;
  134. display: block;
  135. padding: 7px 18px;
  136. text-decoration: none;
  137. outline: 0;
  138. }
  139. .XTjFC,.cvP2Ce{contain:revert;overflow:revert}
  140. </style>`
  141. }<div class="action-menu ac_ctl">
  142. <a class="GHDvEf ab_button" href="#" aria-label="Result Options" aria-expanded="false" aria-haspopup="true" role="button" jsaction="m.tdd;keydown:m.hbke;keypress:m.mskpe"><span class="mn-dwn-arw"></span></a>
  143. <div class="action-menu-panel ab_dropdown" role="menu" tabindex="-1" jsaction="keydown:m.hdke;mouseover:m.hdhne;mouseout:m.hdhue" style="visibility:hidden">
  144. <ol>
  145. <li class="action-menu-item ab_dropdownitem" role="menuitem"><a class="fl sr"></a></li>
  146. <li class="action-menu-item ab_dropdownitem" role="menuitem"><a class="fl cp"></a></li>
  147. </ol>
  148. </div>
  149. </div>`;
  150. menu.querySelector("a").onclick = dropdownClick;
  151. menu = menu.querySelector(".sr");
  152. }
  153. menu.textContent = "Site Results";
  154. if (point.pathname === "/url") {
  155. point = unescape(point.search.match(/&url=([^&]+)/)[1]).match(/:\/\/([^:/]+)/)[1];
  156. } else point = point.hostname;
  157. menu.href = location.href.replace(/&start=\d+/, "").replace(/([&?]q=)([^&]+)/, "$1site:" + encodeURIComponent(point) + "+$2");
  158. menu.onmousedown = null;
  159. menu.addEventListener("click", ev => { ev.stopImmediatePropagation(); ev.stopPropagation() }, true);
  160. if (a) menu.style.display = "none";
  161. if ((menu = menu.nextElementSibling || menu.parentNode.nextElementSibling?.firstElementChild) && menu.classList.contains("cp")) {
  162. menu.textContent = "Cached Page";
  163. if (point = menu.closest('[jscontroller]')?.querySelector('a[jsname]')?.href || entry.querySelector('a')?.href) {
  164. menu.href = location.href.replace(/&start=\d+/, "").replace(/([&?]q=)([^&]+)/, "$1cache:" + encodeURIComponent(point));
  165. menu.onmousedown = null;
  166. menu.addEventListener("click", ev => { ev.stopImmediatePropagation(); ev.stopPropagation() }, true);
  167. } else menu.style.display = "none"
  168. }
  169. })
  170. }
  171. (new MutationObserver(() => {
  172. clearTimeout(t);
  173. t = setTimeout(process, 100)
  174. })).observe(document.body, {childList: true, subtree: true});
  175. process()
  176. })();