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.

当前为 2024-05-14 提交的版本,查看 最新版本

  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.16
  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() {
  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("#ires .g,#search .g,#rso>.hlcw0c>.g,#rso>div:not([class])>.nChh6e").forEach(function(entry, menu, point, e, p) {
  28. if (!(point = entry.querySelector(".r>a,.yuRUbf>a:last-of-type,.yuRUbf>div>a:last-of-type,.dbsr>a,[jsaction]>a[jsname]")) || entry.querySelector('a.fl.sr')) return;
  29. if (menu = entry.querySelector(".action-menu-item")) { //v2: has menu
  30. if ((/:\/\/webcache/).test(menu.firstElementChild.href)) {
  31. e = menu.nextElementSibling;
  32. } else if ((/=related:http/).test(menu.firstElementChild.href)) {
  33. e = menu;
  34. } else e = null;
  35. menu = menu.parentNode.insertBefore(menu.cloneNode(true), e).firstElementChild;
  36. menu.classList.add("sr")
  37. } else if (menu = entry.querySelector(".f+span") || entry.querySelector(".f")) { //v1
  38. menu.insertAdjacentHTML("beforeend", ' - <a class="fl sr"></a> <a class="fl cp"></a>');
  39. menu = menu.lastElementChild.previousElementSibling;
  40. } else { //v2: no menu
  41. if (p = point.querySelector(".XTjFC")) { //news
  42. p.appendChild(menu = document.createElement("SPAN"));
  43. } else if (p = point.parentNode.querySelector(".eFM0qc")) { //video
  44. p.appendChild(menu = document.createElement("SPAN"));
  45. } else point.parentNode.insertBefore(menu = document.createElement("SPAN"), point.nextSibling); //others
  46. menu.innerHTML = `${
  47. next ? '' : `<style>
  48. .action-menu {
  49. display: inline;
  50. margin: 0 3px;
  51. position: relative;
  52. user-select: none;
  53. margin-top: 1px;
  54. vertical-align: middle;
  55. }
  56. .GHDvEf, .GHDvEf:hover, .GHDvEf.selected, .GHDvEf.selected:hover {
  57. display: inline-block;
  58. background-color: #fff;
  59. height: 12px;
  60. margin-top: 1px;
  61. user-select: none;
  62. width: 13px;
  63. }
  64. .mn-dwn-arw {
  65. border-color: #70757a transparent;
  66. border-style: solid;
  67. border-width: 5px 4px 0 4px;
  68. width: 0;
  69. height: 0;
  70. margin-left: -2px;
  71. top: 50%;
  72. margin-top: -2px;
  73. position: absolute;
  74. }
  75. .action-menu .mn-dwn-arw {
  76. border-color: #202124 transparent;
  77. margin-top: -3px;
  78. margin-left: 3px;
  79. left: 0;
  80. border-color: #70757a transparent;
  81. }
  82. .action-menu-panel {
  83. padding: 6px 0;
  84. position: absolute;
  85. left: 0;
  86. padding: 0;
  87. top: 12px;
  88. visibility: hidden;
  89. background: #fff;
  90. border: 1px solid #dadce0;
  91. border: 1px solid rgba(0,0,0,.20);
  92. font-size: 13px;
  93. white-space: nowrap;
  94. z-index: 3;
  95. transition: opacity 0.218s;
  96. box-shadow: 0 2px 4px rgba(0,0,0,.20);
  97. }
  98. .action-menu-item {
  99. cursor: pointer;
  100. user-select: none;
  101. }
  102. .action-menu-item {
  103. margin: 0;
  104. padding: 0;
  105. -moz-user-select: none;
  106. }
  107. .action-menu-item:hover {
  108. background-color: #f8f9fa;
  109. }
  110. #rcnt .action-menu-item a.fl, .action-menu-item a.fl {
  111. color: #3c4043;
  112. display: block;
  113. padding: 7px 18px;
  114. text-decoration: none;
  115. outline: 0;
  116. }
  117. .XTjFC,.cvP2Ce{contain:revert;overflow:revert}
  118. </style>`
  119. }<div class="action-menu ac_ctl">
  120. <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>
  121. <div class="action-menu-panel ab_dropdown" role="menu" tabindex="-1" jsaction="keydown:m.hdke;mouseover:m.hdhne;mouseout:m.hdhue" style="visibility:hidden">
  122. <ol>
  123. <li class="action-menu-item ab_dropdownitem" role="menuitem"><a class="fl sr"></a></li>
  124. <li class="action-menu-item ab_dropdownitem" role="menuitem"><a class="fl cp"></a></li>
  125. </ol>
  126. </div>
  127. </div>`;
  128. menu.querySelector("a").onclick = dropdownClick;
  129. menu = menu.querySelector(".sr");
  130. }
  131. menu.textContent = "Site Results";
  132. if (point.pathname === "/url") {
  133. point = unescape(point.search.match(/&url=([^&]+)/)[1]).match(/:\/\/([^:/]+)/)[1];
  134. } else point = point.hostname;
  135. menu.href = location.href.replace(/&start=\d+/, "").replace(/([&?]q=)([^&]+)/, "$1site:" + encodeURIComponent(point) + "+$2");
  136. menu.onmousedown = null;
  137. if (a) menu.style.display = "none";
  138. if ((menu = menu.nextElementSibling || menu.parentNode.nextElementSibling.firstElementChild) && menu.classList.contains("cp")) {
  139. menu.textContent = "Cached Page";
  140. if (point = menu.closest('[jscontroller]')?.querySelector('a[jsname]')?.href) {
  141. menu.href = location.href.replace(/&start=\d+/, "").replace(/([&?]q=)([^&]+)/, "$1cache:" + encodeURIComponent(point) + "+$2");
  142. menu.onmousedown = null
  143. } else menu.style.display = "none"
  144. }
  145. })
  146. }
  147. (new MutationObserver(() => {
  148. clearTimeout(t);
  149. t = setTimeout(process, 100)
  150. })).observe(document.body, {childList: true, subtree: true});
  151. process()
  152. })();