Google網址縮短

Google搜尋結果的網址很冗長,將其縮短,方便複製、分享、儲存成乾淨的書籤

  1. // ==UserScript==
  2. // @name Google網址縮短
  3. // @namespace https://gf.qytechs.cn/scripts/438711
  4. // @version 3.5
  5. // @description Google搜尋結果的網址很冗長,將其縮短,方便複製、分享、儲存成乾淨的書籤
  6. // @author fmnijk
  7. // @include /^https?://www\.google\.com.*$/
  8. // @icon https://www.google.com/s2/favicons?domain=google.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. // main function
  14. (function() {
  15. 'use strict';
  16. sturl();
  17. window.addEventListener('locationchange', function (){
  18. sturl();
  19. })
  20. })();
  21.  
  22. // shorten url
  23. function sturl() {
  24. // url
  25. var url = window.location.href;
  26. // new url
  27. var nurl = window.location.href;
  28. // query string need to be removed
  29. var qs = ['ved', 'uact', 'ei', 'ie', 'oq', 'sclient', 'cshid', 'dpr', 'iflsig', 'aqs', 'gs_lp', 'gs_lcp', 'gs_lcrp', 'sca_upv', 'source', 'sourceid', '{google:searchboxStats}sourceid', 'sxsrf', 'pccc', 'sa', 'biw', 'bih', 'client', 'gws_rd', 'prmd', 'sca_esv', 'bshm', 'sstk', 'fbs'];
  30. qs = qs.concat(['newwindow']);
  31. //qs = qs.concat(['gl', 'hl']);
  32.  
  33. // query string need to be removed if equal to something
  34. var qseq = [['start', '0']];
  35.  
  36. // remove not necessary query string
  37. nurl = rmqs(nurl, qs);
  38. // remove not necessary query string if equal to something
  39. nurl = rmqseq(nurl, qseq);
  40.  
  41. // do nothing if new url is the same as url
  42. if (url == nurl){
  43. return false;
  44. }
  45.  
  46. // update url in address bar to new url
  47. window.history.replaceState(null, null, nurl);
  48.  
  49. // update url in address bar to new url(deprecated)
  50. //window.location.replace(nurl)
  51. }
  52.  
  53. // remove not necessary query string
  54. function rmqs(url, qs) {
  55. url = new URL(url);
  56. qs.forEach(function(i){
  57. url.searchParams.delete(i);
  58. });
  59. return url.toString();
  60. }
  61.  
  62. // remove not necessary query string if equal to something
  63. function rmqseq(url, qseq) {
  64. url = new URL(url);
  65. qseq.forEach(function(i){
  66. if (url.searchParams.get(i[0]) == i[1]){
  67. url.searchParams.delete(i[0]);
  68. }
  69. });
  70. return url.toString();
  71. }
  72.  
  73. /*----force listen to locationchange work start----*/
  74. history.pushState = ( f => function pushState(){
  75. var ret = f.apply(this, arguments);
  76. window.dispatchEvent(new Event('pushstate'));
  77. window.dispatchEvent(new Event('locationchange'));
  78. return ret;
  79. })(history.pushState);
  80.  
  81. history.replaceState = ( f => function replaceState(){
  82. var ret = f.apply(this, arguments);
  83. window.dispatchEvent(new Event('replacestate'));
  84. window.dispatchEvent(new Event('locationchange'));
  85. return ret;
  86. })(history.replaceState);
  87.  
  88. window.addEventListener('popstate',()=>{
  89. window.dispatchEvent(new Event('locationchange'))
  90. });
  91. /*----force listen to locationchange work end----*/
  92.  
  93. // Ref
  94. // https://gist.github.com/sshay77/4b1f6616a7afabc1ce2a
  95. // https://moz.com/blog/the-ultimate-guide-to-the-google-search-parameters
  96. // https://developer.chrome.com/docs/extensions/mv3/match_patterns
  97. // https://www.google.com/supported_domains
  98.  

QingJ © 2025

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