百度谷歌必应链接缩短

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

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

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