Add Site Search Links To Google Search Result

Add a "Site Results" link 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.

当前为 2023-08-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Add Site Search Links To Google Search Result
  3. // @namespace https://greasyfork.org/en/users/85671-jcunews
  4. // @version 1.0.12
  5. // @license AGPL v3
  6. // @author jcunews
  7. // @description Add a "Site Results" link 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, 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. location.search.substring(1).split("&").some(function(v) {
  19. if (v.indexOf("q=") === 0) {
  20. if (a = decodeURIComponent(v.substr(2).trim().replace(/\+/g, " ")).match(/(?:^|\s)site:([^\s]+)/)) {
  21. a = a[1];
  22. } else a = "";
  23. return true;
  24. }
  25. });
  26. if (!a) document.querySelectorAll("#ires .g,#search .g,#rso>.hlcw0c>.g,#rso>div:not([class])>.nChh6e").forEach(function(entry, menu, point, e, p) {
  27. if (!(point = entry.querySelector(".r>a,.yuRUbf>a:last-of-type,.yuRUbf>div>a:last-of-type,.dbsr>a"))) return;
  28. if (menu = entry.querySelector(".action-menu-item")) { //v2: has menu
  29. if ((/:\/\/webcache/).test(menu.firstElementChild.href)) {
  30. e = menu.nextElementSibling;
  31. } else if ((/=related:http/).test(menu.firstElementChild.href)) {
  32. e = menu;
  33. } else e = null;
  34. menu = menu.parentNode.insertBefore(menu.cloneNode(true), e).firstElementChild;
  35. } else if (menu = entry.querySelector(".f+span") || entry.querySelector(".f")) { //v1
  36. menu.insertAdjacentHTML("beforeend", ' - <a class="fl"></a>');
  37. menu = menu.lastElementChild;
  38. } else { //v2: no menu
  39. if (p = point.querySelector(".XTjFC")) { //news
  40. p.appendChild(menu = document.createElement("SPAN"));
  41. } else if (p = point.parentNode.querySelector(".eFM0qc")) { //video
  42. p.appendChild(menu = document.createElement("SPAN"));
  43. } else point.parentNode.insertBefore(menu = document.createElement("SPAN"), point.nextSibling); //others
  44. menu.innerHTML = `${
  45. next ? '' : `<style>
  46. .action-menu {
  47. display: inline;
  48. margin: 0 3px;
  49. position: relative;
  50. user-select: none;
  51. margin-top: 1px;
  52. vertical-align: middle;
  53. }
  54. .GHDvEf, .GHDvEf:hover, .GHDvEf.selected, .GHDvEf.selected:hover {
  55. display: inline-block;
  56. background-color: #fff;
  57. height: 12px;
  58. margin-top: 1px;
  59. user-select: none;
  60. width: 13px;
  61. }
  62. .mn-dwn-arw {
  63. border-color: #70757a transparent;
  64. border-style: solid;
  65. border-width: 5px 4px 0 4px;
  66. width: 0;
  67. height: 0;
  68. margin-left: -2px;
  69. top: 50%;
  70. margin-top: -2px;
  71. position: absolute;
  72. }
  73. .action-menu .mn-dwn-arw {
  74. border-color: #202124 transparent;
  75. margin-top: -3px;
  76. margin-left: 3px;
  77. left: 0;
  78. border-color: #70757a transparent;
  79. }
  80. .action-menu-panel {
  81. padding: 6px 0;
  82. position: absolute;
  83. left: 0;
  84. padding: 0;
  85. top: 12px;
  86. visibility: hidden;
  87. background: #fff;
  88. border: 1px solid #dadce0;
  89. border: 1px solid rgba(0,0,0,.20);
  90. font-size: 13px;
  91. white-space: nowrap;
  92. z-index: 3;
  93. transition: opacity 0.218s;
  94. box-shadow: 0 2px 4px rgba(0,0,0,.20);
  95. }
  96. .action-menu-item {
  97. cursor: pointer;
  98. user-select: none;
  99. }
  100. .action-menu-item {
  101. margin: 0;
  102. padding: 0;
  103. -moz-user-select: none;
  104. }
  105. .action-menu-item:hover {
  106. background-color: #f8f9fa;
  107. }
  108. #rcnt .action-menu-item a.fl, .action-menu-item a.fl {
  109. color: #3c4043;
  110. display: block;
  111. padding: 7px 18px;
  112. text-decoration: none;
  113. outline: 0;
  114. }
  115. .XTjFC{overflow:visible}
  116. </style>`
  117. }<div class="action-menu ac_ctl">
  118. <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>
  119. <div class="action-menu-panel ab_dropdown" role="menu" tabindex="-1" jsaction="keydown:m.hdke;mouseover:m.hdhne;mouseout:m.hdhue" style="visibility:hidden">
  120. <ol><li class="action-menu-item ab_dropdownitem" role="menuitem"><a class="fl"></a></li></ol>
  121. </div>
  122. </div>`;
  123. menu.querySelector("a").onclick = dropdownClick;
  124. menu = menu.querySelector(".fl");
  125. }
  126. menu.textContent = "Site Results";
  127. if (point.pathname === "/url") {
  128. point = unescape(point.search.match(/&url=([^&]+)/)[1]).match(/:\/\/([^:/]+)/)[1];
  129. } else point = point.hostname;
  130. menu.href = location.href.replace(/([&?]q=)([^&]+)/, "$1site:" + encodeURIComponent(point) + "+$2").replace(/&start=\d+/, "");
  131. menu.onmousedown = null;
  132. });
  133. })();