Make BiliBili Grate Again

useful tweaks for bilibili.com

目前为 2022-04-13 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Make BiliBili Grate Again
  3. // @namespace https://www.kookxiang.com/
  4. // @version 1.3.2
  5. // @description useful tweaks for bilibili.com
  6. // @author kookxiang
  7. // @match https://*.bilibili.com/*
  8. // @grant unsafeWindow
  9. // ==/UserScript==
  10.  
  11. let customStyle = ``
  12.  
  13. // 动态页面优化
  14. if (location.host === "t.bilibili.com") {
  15. customStyle += `
  16. body[wide] #app {
  17. display: flex;
  18. }
  19.  
  20. body[wide] .bili-dyn-home--member {
  21. box-sizing: border-box;
  22. padding: 0 10px;
  23. width: 100%;
  24. flex: 1;
  25. }
  26.  
  27. body[wide] main {
  28. margin: 0 8px;
  29. flex: 1;
  30. overflow: auto;
  31. width: initial;
  32. }
  33.  
  34. #wide-mode-switch {
  35. margin-left: 0;
  36. margin-right: 20px;
  37. }
  38. `
  39. if (!localStorage.WIDE_OPT_OUT) {
  40. document.body.setAttribute('wide', 'wide')
  41. }
  42. window.addEventListener('load', function(){
  43. const tabContainer = document.querySelector('.bili-dyn-list-tabs__list')
  44. const placeHolder = document.createElement('div')
  45. placeHolder.style.flex = 1
  46. const switchButton = document.createElement('a')
  47. switchButton.id = 'wide-mode-switch'
  48. switchButton.className = 'bili-dyn-list-tabs__item'
  49. switchButton.textContent = '宽屏模式'
  50. switchButton.addEventListener('click', function(e) {
  51. e.preventDefault()
  52. if (localStorage.WIDE_OPT_OUT) {
  53. localStorage.removeItem('WIDE_OPT_OUT')
  54. document.body.setAttribute('wide', 'wide')
  55. } else {
  56. localStorage.setItem('WIDE_OPT_OUT', '1')
  57. document.body.removeAttribute('wide')
  58. }
  59. })
  60. tabContainer.appendChild(placeHolder)
  61. tabContainer.appendChild(switchButton)
  62. })
  63. }
  64.  
  65. // 去广告
  66. if (unsafeWindow.__INITIAL_STATE__ && unsafeWindow.__INITIAL_STATE__.locsData) {
  67. for (const key in unsafeWindow.__INITIAL_STATE__.locsData) {
  68. if (!Array.isArray(unsafeWindow.__INITIAL_STATE__.locsData[key])) {
  69. continue;
  70. }
  71. unsafeWindow.__INITIAL_STATE__.locsData[key] = unsafeWindow.__INITIAL_STATE__.locsData[key].filter(x => !x.is_ad)
  72. }
  73. }
  74.  
  75. // 修复文章区复制
  76. if (location.href.startsWith('https://www.bilibili.com/read/cv')) {
  77. unsafeWindow.original.reprint = "1"
  78. document.querySelector('.article-holder').classList.remove("unable-reprint")
  79. document.querySelector('.article-holder').addEventListener('copy', e => e.stopImmediatePropagation(), true)
  80. }
  81.  
  82. // 去 P2P CDN
  83. if (location.href.startsWith('https://www.bilibili.com/video/') || location.href.startsWith('https://www.bilibili.com/bangumi/play/')) {
  84. let cdnDomain
  85.  
  86. try {
  87. [ cdnDomain ] = document.head.innerHTML.match(/up[\w-]+\.bilivideo\.com/)
  88. } catch(e) {}
  89.  
  90. (function(open) {
  91. unsafeWindow.XMLHttpRequest.prototype.open = function() {
  92. try {
  93. const urlObj = new URL(arguments[1]);
  94. if (urlObj.hostname.endsWith(".mcdn.bilivideo.cn")) {
  95. urlObj.host = cdnDomain || 'upos-sz-mirrorcoso1.bilivideo.com'
  96. urlObj.port = 443
  97. console.warn(`更换视频源: ${urlObj.host}`);
  98. arguments[1] = urlObj.toString()
  99. } else if (urlObj.hostname.endsWith(".szbdyd.com")) {
  100. urlObj.host = urlObj.searchParams.get('xy_usource');
  101. urlObj.port = 443;
  102. console.warn(`更换视频源: ${urlObj.host}`);
  103. arguments[1] = urlObj.toString();
  104. }
  105. } finally {
  106. return open.apply(this, arguments)
  107. }
  108. };
  109. })(unsafeWindow.XMLHttpRequest.prototype.open);
  110.  
  111. }
  112.  
  113. // 视频裁切
  114. if (location.href.startsWith('https://www.bilibili.com/video/')) {
  115. customStyle += `
  116. body[video-fit] .bilibili-player-video video,
  117. body[video-fit] .bilibili-player-video bwp-video {
  118. object-fit: cover;
  119. }
  120.  
  121. .bilibili-player-video-btn-setting-left-fit-mode {
  122. display: flex;
  123. width: 100%;
  124. height: 32px;
  125. line-height: 32px;
  126. }
  127.  
  128. .bilibili-player-video-btn-setting-box.bui-panel .bui-panel-wrap,
  129. .bilibili-player-video-btn-setting-box.bui-panel .bui-panel-item {
  130. min-height: 172px !important;
  131. }
  132. `
  133. let timer;
  134. function toggleMode(enabled) {
  135. if (enabled) {
  136. document.body.setAttribute('video-fit', '')
  137. } else {
  138. document.body.removeAttribute('video-fit')
  139. }
  140. }
  141. function injectButton() {
  142. if (!document.querySelector('.bilibili-player-video-btn-setting-left')) {
  143. return
  144. }
  145. clearInterval(timer)
  146. const parent = document.querySelector('.bilibili-player-video-btn-setting-left')
  147. const item = document.createElement('div')
  148. item.className = 'bilibili-player-video-btn-setting-left-fit-mode bui bui-switch'
  149. item.innerHTML = '<input class="bui-switch-input" type="checkbox"><label class="bui-switch-label"><span class="bui-switch-name">裁切模式</span><span class="bui-switch-body"><span class="bui-switch-dot"><span></span></span></span></label>'
  150. parent.insertBefore(item, document.querySelector('.bilibili-player-video-btn-setting-left-more'))
  151. document.querySelector('.bilibili-player-video-btn-setting-left-fit-mode input').addEventListener('change', e => toggleMode(e.target.checked))
  152. document.querySelector('.bilibili-player-video-btn-setting-box .bui-panel-item').style.height = ''
  153. }
  154. timer = setInterval(injectButton, 200)
  155. }
  156.  
  157. const style = document.createElement('style')
  158. style.textContent = customStyle
  159. document.head.appendChild(style)

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址