百度谷歌必应链接缩短

将 Baidu、Google、Bing 搜索引擎的冗长链接缩短,变干净。

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

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