Google 高级搜索助手

在谷歌搜索页面顶部添加一个高级搜索表单

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

  1. // ==UserScript==
  2. // @name Advanced Search Assistant for Google
  3. // @name:zh-CN Google 高级搜索助手
  4. // @name:en Advanced Search Assistant for Google
  5. // @namespace http://tampermonkey.net/
  6. // @version 0.1.1
  7. // @description Add an advanced search form to the top of the page
  8. // @description:zh-CN 在谷歌搜索页面顶部添加一个高级搜索表单
  9. // @description:en Add an advanced search form to the top of the page
  10. // @author shiquda
  11. // @namespace https://github.com/shiquda/shiquda_UserScript
  12. // @supportURL https://github.com/shiquda/shiquda_UserScript/issues
  13. // @match *://www.google.com/search*
  14. // @grant GM_addStyle
  15. // @grant GM_setValue
  16. // @grant GM_getValue
  17. // @license MIT
  18. // ==/UserScript==
  19.  
  20. (function () {
  21. 'use strict';
  22. const style = `
  23. #advancedSearchToggleButton {
  24. margin-right: 10px;
  25. border: none;
  26. border-radius: 5px;
  27. background-color: #007bff;
  28. color: #fff;
  29. font-size: 14px;
  30. font-weight: bold;
  31. }
  32.  
  33.  
  34. #advancedSearchFormContainer {
  35. position: fixed;
  36. top: 130px;
  37. left: 40px;
  38. display: none;
  39. background: #fff;
  40. padding: 10px;
  41. border: 1px solid #ccc;
  42. border-radius: 5px;
  43. font-size: 14px;
  44. font-weight: bold;
  45. }
  46.  
  47.  
  48. #advancedSearchFormContainer label {
  49. display: block;
  50. margin-top: 5px;
  51. }
  52.  
  53.  
  54. #advancedSearchFormContainer input[type="text"] {
  55. margin-top: 5px;
  56. padding: 5px;
  57. border: 1px solid #ccc;
  58. border-radius: 5px;
  59. }
  60.  
  61. #advancedSearchFormContainer select {
  62. margin-top: 5px;
  63. padding: 5px;
  64. border: 1px solid #ccc;
  65. border-radius: 5px;
  66. }
  67.  
  68. #advancedSearchFormContainer button {
  69. border: none;
  70. border-radius: 5px;
  71. background-color: #007bff;
  72. color: #fff;
  73. font-size: 14px;
  74. font-weight: bold;
  75. margin: 20px;
  76. }
  77. `
  78. GM_addStyle(style)
  79. // 创建按钮和表单元素
  80. const toggleButton = document.createElement('button');
  81. toggleButton.className = 'nfSF8e';
  82. toggleButton.textContent = '高级搜索';
  83. toggleButton.id = 'advancedSearchToggleButton'
  84. document.querySelector('.logo').appendChild(toggleButton);
  85.  
  86. const formContainer = document.createElement('div');
  87. formContainer.id = 'advancedSearchFormContainer'
  88. formContainer.addEventListener('click', function (event) {
  89. // Hide the form container when clicking outside of it
  90. if (!form.contains(event.target)) {
  91. formContainer.style.display = 'none';
  92. }
  93. });
  94. document.body.appendChild(formContainer);
  95.  
  96. // 创建表单元素
  97. const form = document.createElement('form');
  98. formContainer.appendChild(form);
  99.  
  100. const params = {
  101. 'as_q': '以下所有字词:',
  102. 'as_epq': '与以下字词完全匹配:',
  103. 'as_oq': '包含以下任意字词:',
  104. 'as_eq': '排除以下字词:',
  105. 'as_nlo': "包含的数字范围:从",
  106. 'as_nhi': "到:",
  107. // 'lr': '语言:',
  108. // 'cr': '地区:',
  109. // 'as_qdr': '最后更新时间:',
  110. 'as_sitesearch': '网站或域名:',
  111. // 'as_occt': '字词出现位置:',
  112. 'as_filetype': '文件类型:',
  113. // 'tbs': '使用权限:',
  114. };
  115.  
  116. for (const param in params) {
  117. const label = document.createElement('label');
  118. label.textContent = params[param]; // 使用 params[param] 获取参数名称对应的中文解释
  119. const input = document.createElement('input');
  120. input.name = param;
  121. input.type = 'text';
  122. form.appendChild(label);
  123. form.appendChild(input);
  124. form.appendChild(document.createElement('br'));
  125. }
  126.  
  127. // 在原有代码的这个位置插入新代码
  128. const timeOptions = {
  129. '': '请选择',
  130. 'd': '一天内',
  131. 'w': '一周内',
  132. 'm': '一月内',
  133. 'y': '一年内',
  134. };
  135.  
  136. const timeLabel = document.createElement('label');
  137. timeLabel.textContent = '最后更新时间:';
  138. const timeSelect = document.createElement('select');
  139. timeSelect.name = 'as_qdr';
  140.  
  141. for (const key in timeOptions) {
  142. const option = document.createElement('option');
  143. option.value = key;
  144. option.textContent = timeOptions[key];
  145. timeSelect.appendChild(option);
  146. }
  147.  
  148. form.appendChild(timeLabel);
  149. form.appendChild(timeSelect);
  150. form.appendChild(document.createElement('br'));
  151.  
  152. const searchButton = document.createElement('button');
  153. searchButton.textContent = '搜索';
  154. searchButton.className = 'nfSF8e';
  155. form.appendChild(searchButton);
  156.  
  157. // Add a clear button to reset the form
  158. const clearButton = document.createElement('button');
  159. clearButton.textContent = '清空';
  160. clearButton.className = 'nfSF8e';
  161. clearButton.addEventListener('click', function (event) {
  162. event.preventDefault();
  163. form.reset();
  164. });
  165. form.appendChild(clearButton);
  166.  
  167. // Load saved data and fill the form when opening a new page
  168. window.addEventListener('load', function () {
  169. for (const param in params) {
  170. const savedValue = GM_getValue(param);
  171. if (savedValue) {
  172. form[param].value = savedValue;
  173. }
  174. }
  175. });
  176.  
  177. // Save form data to Greasemonkey storage
  178. form.addEventListener('input', function () {
  179. for (const param in params) {
  180. GM_setValue(param, form[param].value);
  181. }
  182. });
  183.  
  184. // 在按钮点击时切换表单可见性
  185. toggleButton.addEventListener('click', function (event) {
  186. event.preventDefault();
  187. let status = formContainer.style.display;
  188. status = status === 'none' || status === '' ? 'block' : 'none';
  189. formContainer.style.display = status;
  190. });
  191.  
  192. // 在表单提交时进行高级搜索
  193. form.addEventListener('submit', function (event) {
  194. event.preventDefault();
  195. const searchParams = new URLSearchParams();
  196. for (const param in params) {
  197. const value = form[param].value;
  198. if (value) {
  199. searchParams.set(param, value);
  200. }
  201. }
  202. const searchUrl = 'https://www.google.com/search?' + searchParams.toString();
  203. window.location.href = searchUrl;
  204. });
  205. })();