移除 Bilibili 链接垃圾参数

移除Bilibili地址栏链接中的垃圾参数,如spm_id_from、from_sourse、from等

질문, 리뷰하거나, 이 스크립트를 신고하세요.
  1. // ==UserScript==
  2. // @name 移除 Bilibili 链接垃圾参数
  3. // @namespace https://github.com/s0urcelab/userscripts
  4. // @match https://www.bilibili.com/video/*
  5. // @match https://live.bilibili.com/*
  6. // @grant none
  7. // @version 1.3
  8. // @author s0urce
  9. // @description 移除Bilibili地址栏链接中的垃圾参数,如spm_id_from、from_sourse、from等
  10. // @icon https://www.bilibili.com/favicon.ico
  11. // @run-at document-start
  12. // @license MIT
  13. // ==/UserScript==
  14. // 垃圾参数列表
  15. // Thanks to [Bilibili 干净链接](https://greasyfork.org/zh-CN/scripts/393995)
  16. const SPAM_PARAMS = new Set([
  17. 'spm_id_from',
  18. 'from_source',
  19. 'msource',
  20. 'bsource',
  21. 'seid',
  22. 'source',
  23. 'session_id',
  24. 'visit_id',
  25. 'sourceFrom',
  26. 'from_spmid',
  27. 'share_source',
  28. 'share_medium',
  29. 'share_plat',
  30. 'share_session_id',
  31. 'share_tag',
  32. 'unique_k',
  33. 'csource',
  34. 'vd_source',
  35. 'tab',
  36. 'is_story_h5',
  37. 'share_from',
  38. 'plat_id',
  39. '-Arouter',
  40. 'launch_id',
  41. 'live_from',
  42. 'hotRank',
  43. 'broadcast_type',
  44. ])
  45. const removeSpam = function (url) {
  46. const [withoutHash, hash] = url.split('#')
  47. const [pathname, qs] = withoutHash.split('?')
  48. const params = new URLSearchParams(qs)
  49. const newParams = new URLSearchParams()
  50. for (const [key, val] of params) {
  51. if (!SPAM_PARAMS.has(key)) {
  52. newParams.append(key, val)
  53. }
  54. }
  55. const newQs = newParams.toString()
  56. return `${pathname}${newQs ? `?${newQs}` : ''}${hash ? `#${hash}` : ''}`
  57. }
  58. // 劫持history原生方法
  59. function hijackHistoryNative(name) {
  60. const historyFunc = history[name]
  61. return function () {
  62. // 修改url参数
  63. const { 2: url } = arguments
  64. arguments[2] = removeSpam(url)
  65. // console.error(name, arguments[2])
  66. return historyFunc.apply(this, arguments)
  67. }
  68. }
  69. history.pushState = hijackHistoryNative('pushState')
  70. history.replaceState = hijackHistoryNative('replaceState')
  71. // 劫持window.open原生方法(live直播跳转)
  72. function hijackOpenNative() {
  73. const openFunc = window.open
  74. return function () {
  75. // 修改url参数
  76. const { 0: url } = arguments
  77. arguments[0] = removeSpam(url)
  78. return openFunc.apply(this, arguments)
  79. }
  80. }
  81. window.open = hijackOpenNative()