百度谷歌必应链接缩短

将 Baidu、Google、Bing 搜索引擎的冗长链接缩短,变干净。(Fork from https://greasyfork.org/scripts/438711)

当前为 2022-04-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 百度谷歌必应链接缩短
  3. // @namespace https://greasyfork.org/scripts/443491
  4. // @version 0.2
  5. // @description 将 Baidu、Google、Bing 搜索引擎的冗长链接缩短,变干净。(Fork from https://greasyfork.org/scripts/438711)
  6. // @author RunningCheese
  7. // @include /^https?://www\.baidu\.com.*$/
  8. // @include /^https?://www\.google\.com.*$/
  9. // @include /^https?://www\.bing\.com.*$/
  10. // @include /^https?://www2\.bing\.com.*$/
  11. // @icon https://t1.gstatic.cn/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://www.google.com
  12. // @grant none
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16. // main function
  17. (function() {
  18. 'use strict';
  19. sturl();
  20. window.addEventListener('locationchange', function (){
  21. sturl();
  22. })
  23. })();
  24.  
  25.  
  26.  
  27.  
  28.  
  29. // shorten url
  30. function sturl() {
  31. // url
  32. var url = window.location.href;
  33. // new url
  34. var nurl = window.location.href;
  35. // query string need to be removed
  36. var qs = [
  37. //Baidu
  38. 'rsp','prefixsug','fr','bsst','f','inputT','usm','rsv_page','rqlang','rsv_t','oq','rsv_pq','rsv_spt', 'ie', 'rsv_enter','rsv_sug1', 'rsv_sug7','rsv_sug2','rsv_sug3','rsv_iqid', 'rsv_bp', 'rsv_btype', 'rsv_idx', 'rsv_dl', 'issp', 'cshid', 'tn','rsv_sug4',
  39. //Google
  40. 'tbas','ved', 'uact', 'ei', 'ie', 'oq', 'sclient', 'cshid', 'dpr','iflsig', 'aqs', 'gs_lcp', 'source', 'sourceid', 'sxsrf', 'pccc', 'sa', 'biw', 'bih', 'hl', 'newwindow',
  41. //Bing
  42. 'tsc','sp','FORM','form','pq','sc','qs','sk','cvid'
  43. ];
  44. // query string need to be removed if equal to something
  45. var qseq = [['start', '0']];
  46.  
  47. // remove not necessary query string
  48. nurl = rmqs(nurl, qs);
  49. // remove not necessary query string if equal to something
  50. nurl = rmqseq(nurl, qseq);
  51.  
  52. // do nothing if new url is the same as url
  53. if (url == nurl){
  54. return false;
  55. }
  56.  
  57. // update url in address bar to new url
  58. window.history.replaceState(null, null, nurl);
  59.  
  60. // update url in address bar to new url(deprecated)
  61. //window.location.replace(nurl)
  62. }
  63.  
  64. // remove not necessary query string
  65. function rmqs(url, qs) {
  66. url = new URL(url);
  67. qs.forEach(function(i){
  68. url.searchParams.delete(i);
  69. });
  70. return url.toString();
  71. }
  72.  
  73. // remove not necessary query string if equal to something
  74. function rmqseq(url, qseq) {
  75. url = new URL(url);
  76. qseq.forEach(function(i){
  77. if (url.searchParams.get(i[0]) == i[1]){
  78. url.searchParams.delete(i[0]);
  79. }
  80. });
  81. return url.toString();
  82. }
  83.  
  84. /*----force listen to locationchange work start----*/
  85. history.pushState = ( f => function pushState(){
  86. var ret = f.apply(this, arguments);
  87. window.dispatchEvent(new Event('pushstate'));
  88. window.dispatchEvent(new Event('locationchange'));
  89. return ret;
  90. })(history.pushState);
  91.  
  92. history.replaceState = ( f => function replaceState(){
  93. var ret = f.apply(this, arguments);
  94. window.dispatchEvent(new Event('replacestate'));
  95. window.dispatchEvent(new Event('locationchange'));
  96. return ret;
  97. })(history.replaceState);
  98.  
  99. window.addEventListener('popstate',()=>{
  100. window.dispatchEvent(new Event('locationchange'))
  101. });
  102. /*----force listen to locationchange work end----*/
  103.  
  104.  
  105.