百度谷歌必应链接缩短

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

  1. // ==UserScript==
  2. // @name Baidu Google Bing URL Shorten
  3. // @name:zh-CN 百度谷歌必应链接缩短
  4. // @namespace https://www.runningcheese.com
  5. // @version 0.4
  6. // @description Mark Baidu、Google、Bing URL Shortest.
  7. // @description:zh-CN 将 Baidu、Google、Bing 搜索引擎的冗长链接缩短,变干净。
  8. // @author RunningCheese
  9. // @match *://*.google.com/*
  10. // @match *://*.baidu.com/*
  11. // @match *://*.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','lq','ghsh','ghacc','ghpl','ghc'
  44.  
  45. ];
  46. // query string need to be removed if equal to something
  47. var qseq = [['start', '0']];
  48.  
  49. // remove not necessary query string
  50. nurl = rmqs(nurl, qs);
  51. // remove not necessary query string if equal to something
  52. nurl = rmqseq(nurl, qseq);
  53.  
  54. // do nothing if new url is the same as url
  55. if (url == nurl){
  56. return false;
  57. }
  58.  
  59. // update url in address bar to new url
  60. window.history.replaceState(null, null, nurl);
  61.  
  62. // update url in address bar to new url(deprecated)
  63. //window.location.replace(nurl)
  64. }
  65.  
  66. // remove not necessary query string
  67. function rmqs(url, qs) {
  68. url = new URL(url);
  69. qs.forEach(function(i){
  70. url.searchParams.delete(i);
  71. });
  72. return url.toString();
  73. }
  74.  
  75. // remove not necessary query string if equal to something
  76. function rmqseq(url, qseq) {
  77. url = new URL(url);
  78. qseq.forEach(function(i){
  79. if (url.searchParams.get(i[0]) == i[1]){
  80. url.searchParams.delete(i[0]);
  81. }
  82. });
  83. return url.toString();
  84. }
  85.  
  86. /*----force listen to locationchange work start----*/
  87. history.pushState = ( f => function pushState(){
  88. var ret = f.apply(this, arguments);
  89. window.dispatchEvent(new Event('pushstate'));
  90. window.dispatchEvent(new Event('locationchange'));
  91. return ret;
  92. })(history.pushState);
  93.  
  94. history.replaceState = ( f => function replaceState(){
  95. var ret = f.apply(this, arguments);
  96. window.dispatchEvent(new Event('replacestate'));
  97. window.dispatchEvent(new Event('locationchange'));
  98. return ret;
  99. })(history.replaceState);
  100.  
  101. window.addEventListener('popstate',()=>{
  102. window.dispatchEvent(new Event('locationchange'))
  103. });
  104. /*----force listen to locationchange work end----*/
  105.  
  106.  
  107.