Google Search URL Fixup Fork

Sets the links in the JavaScript-free Google Basic Variant (gbv=1) search results to their original domains, which circumvents click routing through Google's query parameters, fixes browser history mismatch, and strips tracking query parameters.

当前为 2024-08-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Google Search URL Fixup Fork
  3. // @namespace https://gf.qytechs.cn/users/581142
  4. // @namespace https://github.com/brian6932/gsearch-urlfix
  5. // @license MPL-2.0
  6. // @include /^https?:\/{2}w{3}\.google\.(?:a[delmstz]|b[aefgijsty]|c(?:o(?:m(?:\.(?:a[fgru]|b[dhnorz]|c[ouy]|do|e[cgt]|fj|g[hit]|hk|jm|k[hw]|l[by]|m[mtxy]|n[agip]|om|p[aeghkry]|qa|s[abglv]|t[jrw]|u[ay]|v[cn]))?|\.(?:ao|bw|c[kr]|i[dln]|jp|k[er]|ls|m[az]|nz|t[hz]|u[gkz]|v[ei]|z[amw]))|[adfghilmnvz])|d[ejkmz]|e[es]|f[imr]|g[aeglmry]|h[nrtu]|i[emqst]|j[eo]|k[giz]|l[aiktuv]|m[degklnuvw]|n[eloru]|p[lnst]|r[osuw]|s[cehikmnort]|t[dglmnot]|vu|ws)\/search\?/
  7. // @grant none
  8. // @version 0.87
  9. // @author brian6932
  10. // @description Sets the links in the JavaScript-free Google Basic Variant (gbv=1) search results to their original domains, which circumvents click routing through Google's query parameters, fixes browser history mismatch, and strips tracking query parameters.
  11. // ==/UserScript==
  12. // jshint esversion: 11
  13.  
  14. let i = -1
  15. const
  16. pathToParam = {
  17. __proto__: null,
  18. "/url": "q",
  19. "/imgres": "imgurl"
  20. },
  21. strippableParams = [
  22. "ei",
  23. "fbs",
  24. "sa",
  25. "sca_esv",
  26. "sca_upv",
  27. "source",
  28. "sxsrf",
  29. "ved",
  30. ],
  31. links = document.links,
  32. fixLinks = () => {
  33. while (++i < links.length) {
  34. if (links[i].search === "" && links[i].host !== "www.google.com")
  35. continue
  36.  
  37. const mappedParam = pathToParam[links[i].pathname]
  38. if (mappedParam !== undefined) {
  39. // Extract the search without the leading '?'.
  40. const encodedLink = new URLSearchParams(links[i].search.slice(1)).get(mappedParam)
  41. // note to self: must change href attribute, not the entire
  42. // thing. Doh. Read the docs, sort of.
  43. if (encodedLink === null)
  44. continue
  45.  
  46. if (!(links[i].href = decodeURIComponent(encodedLink)).startsWith("https://www.google.com/"))
  47. continue
  48. }
  49.  
  50. /*
  51. * Should hit the following paths
  52. /
  53. /maps
  54. /preferences
  55. /search
  56. /setprefs
  57. /travel
  58. /webhp
  59. */
  60. const link = new URL(links[i].href)
  61.  
  62. for (const param of strippableParams)
  63. link.searchParams.delete(param)
  64.  
  65. links[i].href = link.toString()
  66. }
  67. }
  68.  
  69. // Invoke on load.
  70. fixLinks()
  71.  
  72. // Reinvoke on body change.
  73. new MutationObserver(mutations => {
  74. for (const mutation of mutations)
  75. if (mutation.type === "childList" && mutation.addedNodes[1].id === "main")
  76. return fixLinks()
  77. }).observe(document.body, { __proto__: null, childList: true, subtree: true })

QingJ © 2025

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