Greasy Fork 还支持 简体中文。

Advanced Search Assistant for Google

Add an advanced search form to the top of the page

目前為 2024-08-09 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Advanced Search Assistant for Google
  3. // @name:zh-CN Google 高级搜索助手
  4. // @namespace https://github.com/ChinaGodMan/UserScripts
  5. // @version 0.1.9.5
  6. // @description Add an advanced search form to the top of the page
  7. // @description:zh-CN 在谷歌搜索页面顶部添加一个高级搜索表单
  8. // @author shiquda &人民的勤务员 <toniaiwanowskiskr47@gmail.com>
  9. // @namespace https://github.com/shiquda/shiquda_UserScript
  10. // @supportURL https://github.com/shiquda/shiquda_UserScript/issues
  11. // @match *://www.google.com/search*
  12. // @include *://*google*/search*
  13. // @grant GM_addStyle
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @license MIT
  17. // @icon https://github.com/ChinaGodMan/UserScripts/raw/main/docs/icon/Scripts%20Icons/Google-advance-search.png
  18.  
  19. // ==/UserScript==
  20.  
  21. (function () {
  22. "use strict"
  23. let isMobile = false
  24. if (
  25. navigator.userAgent.match(/Android/i) ||
  26. navigator.userAgent.match(/webOS/i) ||
  27. navigator.userAgent.match(/iPhone/i) ||
  28. navigator.userAgent.match(/iPad/i) ||
  29. navigator.userAgent.match(/iPod/i) ||
  30. navigator.userAgent.match(/BlackBerry/i) ||
  31. navigator.userAgent.match(/Windows Phone/i)
  32. ) {
  33. // On mobile device
  34. isMobile = true
  35. }
  36.  
  37. let isDarkMode = false
  38.  
  39. try {
  40. if (
  41. window.matchMedia &&
  42. window.matchMedia("(prefers-color-scheme: dark)").matches
  43. ) {
  44. // Dark mode is enabled
  45. isDarkMode = true
  46. console.log("Dark mode is enabled.")
  47. }
  48. } catch (error) {
  49. console.log("Failed to determine the color mode.", error)
  50. }
  51.  
  52. const userLanguage = "" // You can set your language config here manually. 'zh-CN' & 'en' are supported now.
  53.  
  54. const supportedLanguages = ["zh-CN", "en"]
  55.  
  56. const translation = {
  57. as_q: {
  58. "zh-CN": "搜索字词:",
  59. en: "Search word:",
  60. },
  61. as_epq: {
  62. "zh-CN": "与以下字词完全匹配:",
  63. en: "Match the following words exactly:",
  64. },
  65. as_oq: {
  66. "zh-CN": "包含以下任意字词:",
  67. en: "Contains any of the following words:",
  68. },
  69. as_eq: {
  70. "zh-CN": "排除以下字词:",
  71. en: "Exclude the following words:",
  72. },
  73. as_nlo: {
  74. "zh-CN": "包含的数字范围:从",
  75. en: "Number range: from",
  76. },
  77. as_nhi: {
  78. "zh-CN": "到:",
  79. en: "to:",
  80. },
  81. lr: {
  82. "zh-CN": "语言:",
  83. en: "Language:",
  84. },
  85. cr: {
  86. "zh-CN": "地区:",
  87. en: "Region:",
  88. },
  89. as_qdr: {
  90. "zh-CN": "最后更新时间:",
  91. en: "Last update time:",
  92. },
  93. as_sitesearch: {
  94. "zh-CN": "网站或域名:",
  95. en: "Website or domain:",
  96. },
  97. as_occt: {
  98. "zh-CN": "字词出现位置:",
  99. en: "Word position:",
  100. },
  101. as_filetype: {
  102. "zh-CN": "文件类型:",
  103. en: "File type:",
  104. },
  105. tbs: {
  106. "zh-CN": "使用权限:",
  107. en: "Usage rights:",
  108. },
  109. advancedSearch: {
  110. "zh-CN": "高级搜索",
  111. en: "Advanced Search",
  112. },
  113. search: {
  114. "zh-CN": "搜索",
  115. en: "Search",
  116. },
  117. clear: {
  118. "zh-CN": "清空",
  119. en: "Clear",
  120. },
  121. as_qdr_select: {
  122. "": {
  123. "zh-CN": "请选择",
  124. en: "Please select",
  125. },
  126. d: {
  127. "zh-CN": "一天内",
  128. en: "Past 24 hours",
  129. },
  130. w: {
  131. "zh-CN": "一周内",
  132. en: "Past week",
  133. },
  134. m: {
  135. "zh-CN": "一月内",
  136. en: "Past month",
  137. },
  138. y: {
  139. "zh-CN": "一年内",
  140. en: "Past year",
  141. },
  142. },
  143. as_occt_select: {
  144. "": {
  145. "zh-CN": "请选择",
  146. en: "Please select",
  147. },
  148. title: {
  149. "zh-CN": "网页标题中",
  150. en: "In the title of the web page",
  151. },
  152. body: {
  153. "zh-CN": "网页正文中",
  154. en: "In the body of the web page",
  155. },
  156. url: {
  157. "zh-CN": "网页网址中",
  158. en: "In the URL of the web page",
  159. },
  160. links: {
  161. "zh-CN": "指向网页的链接中",
  162. en: "In the links to the web page",
  163. },
  164. },
  165. }
  166. const style = `
  167. #advancedSearchToggleButton {
  168. margin-right: 10px;
  169. border: none;
  170. border-radius: 5px;
  171. background-color: #007bff;
  172. color: #fff;
  173. font-size: 14px;
  174. font-weight: bold;
  175. margin: 10px;
  176. }
  177.  
  178.  
  179. #advancedSearchFormContainer {
  180. position: fixed;
  181. ${isMobile ? "top: 150px;" : "top: 130px;"}
  182. ${isMobile ? "left: 15px;" : "left: 30px;"}
  183. display: none;
  184. padding: 10px;
  185. border: 1px solid #ccc;
  186. border-radius: 5px;
  187. font-size: 14px;
  188. font-weight: bold;
  189. ${isDarkMode
  190. ? "background-color: rgba(0, 0, 0, 1);"
  191. : "background-color: rgba(255, 255, 255, 1);"
  192. }
  193. ${isMobile ? "column-count: 2;" : ""} /* 在移动设备上分为两列 */
  194. z-index: 1000; // Make sure the button is on top of the search bar
  195. }
  196.  
  197.  
  198. #advancedSearchFormContainer label {
  199. display: block;
  200. margin-top: 5px;
  201. }
  202.  
  203.  
  204. #advancedSearchFormContainer input[type="text"] {
  205. margin-top: 5px;
  206. padding: 5px;
  207. border: 1px solid #ccc;
  208. border-radius: 5px;
  209. }
  210.  
  211. #advancedSearchFormContainer select {
  212. margin-top: 5px;
  213. padding: 5px;
  214. border-radius: 5px;
  215. }
  216.  
  217. #advancedSearchFormContainer button {
  218. border: none;
  219. border-radius: 5px;
  220. background-color: #007bff;
  221. color: #fff;
  222. font-size: 14px;
  223. font-weight: bold;
  224. margin: 20px;
  225. }
  226. `
  227. GM_addStyle(style)
  228.  
  229. let language = "en"
  230. if (userLanguage.length > 0) { // userLanguage is set manually
  231. if (supportedLanguages.includes(userLanguage)) {
  232. language = userLanguage
  233. } else {
  234. console.log(`Unsupported language: ${userLanguage}`)
  235. }
  236. } else {
  237. // Check if any of the user's preferred languages are supported
  238. language =
  239. navigator.languages
  240. .map((lang) => lang.split("-")[0]) // Consider only the language part, not the region
  241. .map((lang) => supportedLanguages.find((supportedLang) => supportedLang.split("-")[0] === lang)) // Match with the supported languages
  242. .filter(Boolean) // Remove undefined values
  243. .shift() // Take the first matched language
  244. || "en" // Default to 'en' if no match found
  245. console.log(`Here is the language: ${language}`)
  246. }
  247.  
  248. // Create user interface
  249. const toggleButton = document.createElement("button")
  250. toggleButton.className = "nfSF8e"
  251. toggleButton.textContent = translation["advancedSearch"][language]
  252. toggleButton.id = "advancedSearchToggleButton"
  253. if (isMobile) {
  254. document.querySelector(".Fh5muf").appendChild(toggleButton)
  255. } else {
  256. document.querySelector(".logo").appendChild(toggleButton)
  257. const searchContainer = document.querySelector(".RNNXgb")
  258. searchContainer.appendChild(toggleButton)
  259. }
  260.  
  261.  
  262.  
  263. // Add minimal style for positioning
  264. toggleButton.style.marginTop = "5px" // Add some space above the button
  265. toggleButton.style.marginLeft = "5px" // Add some space to the left of the button
  266. // Add any additional styles to match the search bar's height or other styling
  267.  
  268. const formContainer = document.createElement("div")
  269. formContainer.id = "advancedSearchFormContainer"
  270. document.body.appendChild(formContainer)
  271.  
  272. //
  273. const form = document.createElement("form")
  274. formContainer.appendChild(form)
  275.  
  276. const params = {
  277. as_q: translation["as_q"][language],
  278. as_epq: translation["as_epq"][language],
  279. as_oq: translation["as_oq"][language],
  280. as_eq: translation["as_eq"][language],
  281. as_nlo: translation["as_nlo"][language],
  282. as_nhi: translation["as_nhi"][language],
  283. // 'lr': translation['lr'][language],
  284. // 'cr': translation['cr'][language],
  285. as_qdr: {
  286. name: translation["as_qdr"][language],
  287. options: {
  288. "": translation["as_qdr_select"][""][language],
  289. d: translation["as_qdr_select"]["d"][language],
  290. w: translation["as_qdr_select"]["w"][language],
  291. m: translation["as_qdr_select"]["m"][language],
  292. y: translation["as_qdr_select"]["y"][language],
  293. },
  294. },
  295. as_sitesearch: translation["as_sitesearch"][language],
  296. as_occt: {
  297. name: translation["as_occt"][language],
  298. options: {
  299. "": translation["as_occt_select"][""][language],
  300. title: translation["as_occt_select"]["title"][language],
  301. body: translation["as_occt_select"]["body"][language],
  302. url: translation["as_occt_select"]["url"][language],
  303. links: translation["as_occt_select"]["links"][language],
  304. },
  305. },
  306. as_filetype: translation["as_filetype"][language],
  307. // 'tbs': translation['tbs'][language],
  308. }
  309.  
  310. for (const param in params) {
  311. if (typeof params[param] === "object") {
  312. const label = document.createElement("label")
  313. label.textContent = params[param].name
  314. const select = document.createElement("select")
  315. select.name = param
  316.  
  317. Object.keys(params[param]["options"]).forEach((option) => {
  318. const optionElement = document.createElement("option")
  319. optionElement.value = option
  320. optionElement.textContent = params[param]["options"][option]
  321. select.appendChild(optionElement)
  322. })
  323.  
  324. form.appendChild(label)
  325. form.appendChild(select)
  326. form.appendChild(document.createElement("br"))
  327. continue
  328. }
  329. const label = document.createElement("label")
  330. label.textContent = params[param]
  331. const input = document.createElement("input")
  332. input.name = param
  333. input.type = "text"
  334. form.appendChild(label)
  335. form.appendChild(input)
  336. form.appendChild(document.createElement("br"))
  337. }
  338.  
  339. const searchButton = document.createElement("button")
  340. searchButton.textContent = translation["search"][language]
  341. form.appendChild(searchButton)
  342.  
  343. // Add a clear button to reset the form
  344. const clearButton = document.createElement("button")
  345. clearButton.textContent = translation["clear"][language]
  346. clearButton.addEventListener("click", function (event) {
  347. event.preventDefault()
  348. form.reset()
  349. })
  350. form.appendChild(clearButton)
  351.  
  352. // Load saved data and fill the form when opening a new page
  353. window.addEventListener("load", function () {
  354. for (const param in params) {
  355. const savedValue = GM_getValue(param)
  356. if (savedValue) {
  357. form[param].value = savedValue
  358. }
  359. }
  360. })
  361.  
  362. // Save form data to Greasemonkey storage
  363. form.addEventListener("input", function () {
  364. for (const param in params) {
  365. GM_setValue(param, form[param].value)
  366. }
  367. })
  368.  
  369. // Toggle the form display
  370. toggleButton.addEventListener("click", function (event) {
  371. event.preventDefault()
  372. let status = formContainer.style.display
  373. status = status === "none" || status === "" ? "block" : "none"
  374. formContainer.style.display = status
  375. })
  376.  
  377. // Submit the form
  378. form.addEventListener("submit", function (event) {
  379. event.preventDefault()
  380. const searchParams = new URLSearchParams()
  381. for (const param in params) {
  382. const value = form[param].value
  383. if (value) {
  384. searchParams.set(param, value)
  385. }
  386. }
  387. const searchUrl =
  388. "https://www.google.com/search?" + searchParams.toString()
  389. window.location.href = searchUrl
  390. })
  391. })()