Google Site Filter (Overlay on Toolbar)

将站点筛选按钮以绝对定位方式放置在工具栏右侧,不影响原有页面布局

  1. // ==UserScript==
  2. // @name Google Site Filter (Overlay on Toolbar)
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 将站点筛选按钮以绝对定位方式放置在工具栏右侧,不影响原有页面布局
  6. // @match https://www.google.com/search*
  7. // @run-at document-end
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. const sites = ["site1.com", "site2.com", "site3.com"];
  16.  
  17. function addSiteFilterOverlay() {
  18. // 定位工具栏容器
  19. const toolbar = document.getElementById("hdtb");
  20. if (!toolbar) {
  21. setTimeout(addSiteFilterOverlay, 500);
  22. return;
  23. }
  24. // 确保工具栏容器是相对定位(绝对定位的子元素以此为参考)
  25. if (getComputedStyle(toolbar).position === "static") {
  26. toolbar.style.position = "relative";
  27. }
  28.  
  29. // 创建自定义按钮,绝对定位于工具栏右侧,不占据正常文档流
  30. const customBtn = document.createElement("div");
  31. customBtn.innerText = "站点筛选 ▼";
  32. Object.assign(customBtn.style, {
  33. position: "absolute",
  34. right: "10px", // 离工具栏右边10px
  35. top: "50%", // 垂直居中
  36. transform: "translateY(-50%)",
  37. padding: "4px 8px",
  38. background: "#fff",
  39. border: "1px solid #ccc",
  40. borderRadius: "2px",
  41. cursor: "pointer",
  42. userSelect: "none",
  43. fontSize: "13px",
  44. zIndex: "1000"
  45. });
  46.  
  47. // 创建下拉菜单,作为自定义按钮的子元素
  48. const dropdown = document.createElement("div");
  49. Object.assign(dropdown.style, {
  50. position: "absolute",
  51. top: "110%", // 显示在按钮下方
  52. right: "0",
  53. background: "#fff",
  54. border: "1px solid #ccc",
  55. boxShadow: "0 2px 4px rgba(0,0,0,0.2)",
  56. padding: "10px",
  57. zIndex: "1001",
  58. display: "none",
  59. minWidth: "150px"
  60. });
  61.  
  62. // 为每个站点添加复选框
  63. sites.forEach(site => {
  64. const label = document.createElement("label");
  65. label.style.display = "block";
  66. label.style.marginBottom = "5px";
  67. const checkbox = document.createElement("input");
  68. checkbox.type = "checkbox";
  69. checkbox.value = `site:${site}`;
  70. checkbox.style.marginRight = "5px";
  71. label.appendChild(checkbox);
  72. label.appendChild(document.createTextNode(site));
  73. dropdown.appendChild(label);
  74. });
  75.  
  76. // 添加“应用筛选”按钮
  77. const applyBtn = document.createElement("button");
  78. applyBtn.innerText = "应用筛选";
  79. Object.assign(applyBtn.style, {
  80. marginTop: "5px",
  81. padding: "5px 10px",
  82. background: "#4285f4",
  83. color: "#fff",
  84. border: "none",
  85. borderRadius: "2px",
  86. cursor: "pointer",
  87. fontSize: "13px"
  88. });
  89. applyBtn.addEventListener("click", (e) => {
  90. e.stopPropagation();
  91. const queryInput = document.querySelector('input[name="q"]');
  92. if (!queryInput) return;
  93. const query = queryInput.value;
  94. const selected = Array.from(dropdown.querySelectorAll("input:checked"))
  95. .map(c => c.value);
  96. if (selected.length > 0 && query) {
  97. const newQuery = `${query} ${selected.join(" ")}`;
  98. window.location.href = `https://www.google.com/search?q=${encodeURIComponent(newQuery)}`;
  99. }
  100. });
  101. dropdown.appendChild(applyBtn);
  102. dropdown.addEventListener("click", (e) => e.stopPropagation());
  103.  
  104. // 将下拉菜单添加为自定义按钮的子元素
  105. customBtn.appendChild(dropdown);
  106.  
  107. // 点击自定义按钮时切换下拉菜单显示状态
  108. customBtn.addEventListener("click", (e) => {
  109. e.stopPropagation();
  110. dropdown.style.display = dropdown.style.display === "none" ? "block" : "none";
  111. });
  112. // 点击页面其他地方关闭下拉菜单
  113. document.addEventListener("click", () => {
  114. dropdown.style.display = "none";
  115. });
  116.  
  117. // 将自定义按钮追加到工具栏中(不会影响工具栏布局,因为按钮是绝对定位)
  118. toolbar.appendChild(customBtn);
  119. }
  120.  
  121. window.addEventListener("load", addSiteFilterOverlay);
  122. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址