Greasy Fork is available in English.

B站URL清理(掩耳盗铃)

清理B站 URL 中多余的内容——这种清理只是将 URL 中多余的部分简单隐藏起来,不会过分阻止其完成自身的使命

ติดตั้งสคริปต์นี้?
สคริปต์ที่แนะนำของผู้เขียน

คุณอาจชื่นชอบ B站稍后再看功能增强

ติดตั้งสคริปต์นี้
  1. // ==UserScript==
  2. // @name B站URL清理(掩耳盗铃)
  3. // @version 1.1.1.20230314
  4. // @namespace laster2800
  5. // @author Laster2800
  6. // @description 清理B站 URL 中多余的内容——这种清理只是将 URL 中多余的部分简单隐藏起来,不会过分阻止其完成自身的使命
  7. // @note 提供真实清理的脚本有很多,但本人认为不能以最坏的恶意来推测每一种设计(尽管 vd_source 确实已经被证实是会暴露分享者信息的邪恶设计),只不过在 URL 上添加各种奇奇怪怪的内容实在是太难看了,让 URL 在显示上更简洁才是该脚本的本意。
  8. // @icon https://www.bilibili.com/favicon.ico
  9. // @homepageURL https://greasyfork.org/zh-CN/scripts/447604
  10. // @supportURL https://greasyfork.org/zh-CN/scripts/447604/feedback
  11. // @license LGPL-3.0
  12. // @noframes
  13. // @include *://*.bilibili.com/*
  14. // @grant none
  15. // @run-at document-start
  16. // ==/UserScript==
  17.  
  18. (function() {
  19. 'use strict'
  20.  
  21. let busy = false
  22. const rm = ['vd_source', 'spm_id_from']
  23. initUrlchangeEvent()
  24.  
  25. clean()
  26. window.addEventListener('urlchange', clean)
  27.  
  28. function clean() {
  29. if (busy) return
  30. busy = true
  31. let r = false
  32. const url = new URL(location.href)
  33. for (const k of rm) {
  34. r |= tryClean(url, k) // |= 不会短路
  35. }
  36. r && history.replaceState({}, null, url.href)
  37. busy = false
  38. }
  39.  
  40. function tryClean(url, k) {
  41. const m = new RegExp(`^\\?((.*)&)?((?<=[?&])${k}(=[^&]*)?)(&(.+))?$`).exec(url.search)
  42. if (!m) return false
  43. let a = m[2] ?? ''
  44. const b = m[6] ?? ''
  45. if (a && b) {
  46. a += `&${b}`
  47. } else if (a || b) {
  48. a += b
  49. }
  50. url.search = a ? `?${a}` : ''
  51. return true
  52. }
  53.  
  54. /**
  55. * @see UserscriptAPI.base.initUrlchangeEvent
  56. */
  57. function initUrlchangeEvent() {
  58. if (window[Symbol.for('onurlchange')] === undefined) {
  59. let url = new URL(location.href)
  60. const dispatchEvent = () => {
  61. const event = new CustomEvent('urlchange', {
  62. detail: { prev: url, curr: new URL(location.href) },
  63. bubbles: true,
  64. })
  65. url = event.detail.curr
  66. if (typeof window.onurlchange === 'function') {
  67. window.addEventListener('urlchange', window.onurlchange, { once: true })
  68. }
  69. document.dispatchEvent(event)
  70. }
  71.  
  72. history.pushState = (f => (...args) => {
  73. const ret = Reflect.apply(f, history, args)
  74. dispatchEvent()
  75. return ret
  76. })(history.pushState)
  77. history.replaceState = (f => (...args) => {
  78. const ret = Reflect.apply(f, history, args)
  79. dispatchEvent()
  80. return ret
  81. })(history.replaceState)
  82. window.addEventListener('popstate', () => {
  83. dispatchEvent()
  84. })
  85. window[Symbol.for('onurlchange')] = true
  86. }
  87. }
  88. })()
  89.