Greasy Fork镜像 支持简体中文。

bilibili 显示 av 号

在视频播放页面显示视频的 av 号

  1. // ==UserScript==
  2. // @name bilibili 显示 av 号
  3. // @namespace https://github.com/8qwe24657913
  4. // @version 0.3
  5. // @description 在视频播放页面显示视频的 av 号
  6. // @author 8q
  7. // @match http://www.bilibili.com/*
  8. // @match https://www.bilibili.com/*
  9. // @run-at document-end
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. // eslint-disable-next-line no-extra-semi
  14. ;(function () {
  15. 'use strict'
  16. let aid, link, anchor
  17. const setData = () => {
  18. link.href = `//www.bilibili.com/video/av${aid}/`
  19. link.textContent = `av${aid}`
  20. }
  21. const run = () => {
  22. // aid 应为全数字
  23. if (!/^\d+$/.test(aid)) {
  24. console.error('bilibili 显示 av 号: aid 格式错误', 'aid=', aid)
  25. return
  26. }
  27. // 已插入链接且未被 vue 删除,常见于通过 pushState + ajax 实现的无刷新跳页
  28. if (link && anchor && document.contains(anchor)) {
  29. setData()
  30. return
  31. }
  32. anchor = document.querySelector('.video-data span:not([title]):not([class]), .pub-info')
  33. if (!anchor) {
  34. console.error('bilibili 显示 av 号: 未能找到元素插入点', 'aid=', aid)
  35. return
  36. }
  37. link = document.createElement('a')
  38. link.target = '_blank'
  39. link.className = 'show-bili-aid'
  40. setData()
  41. // vue 你赢了,我用 shadow dom 你总碰不着了吧?
  42. if (!anchor.shadowRoot) {
  43. const clone = anchor.cloneNode(true)
  44. anchor.attachShadow({ mode: 'open' })
  45. anchor.shadowRoot.appendChild(clone)
  46. }
  47. const style = document.createElement('style')
  48. if (!anchor.classList.contains('pub-info')) {
  49. // 普通视频
  50. style.textContent = `
  51. a.show-bili-aid {
  52. margin-left: 16px;
  53. color: #999;
  54. text-decoration: none;
  55. }
  56. `
  57. } else {
  58. // 番剧,使用 .av-link 的样式
  59. style.textContent = `
  60. .pub-info {
  61. display: block;
  62. float: left;
  63. height: 16px;
  64. line-height: 16px;
  65. margin-right: 10px;
  66. }
  67. a.show-bili-aid {
  68. display: block;
  69. float: left;
  70. height: 16px;
  71. line-height: 16px;
  72. color: #212121;
  73. text-decoration: none;
  74. }
  75. a.show-bili-aid:hover {
  76. color: #03a0d6;
  77. }
  78. `
  79. }
  80. anchor.shadowRoot.appendChild(style)
  81. anchor.shadowRoot.appendChild(link)
  82. }
  83. const target = window.__INITIAL_STATE__ && 'aid' in window.__INITIAL_STATE__ ? window.__INITIAL_STATE__ : window
  84. if (target === window && !('aid' in window)) window.aid = ''
  85. aid = target.aid
  86. // 我可能比 vue hook 得早,也可能比它晚
  87. // 如果我比 vue 早,vue 的 hook 触发时会自动触发我的 hook
  88. // 如果我比 vue 晚,我的 hook 触发时就需要触发 vue 的 hook
  89. const desc = Object.getOwnPropertyDescriptor(target, 'aid')
  90. const vueHook = desc.set
  91. Object.defineProperty(target, 'aid', {
  92. get: desc.get || (() => aid),
  93. set(id) {
  94. aid = id
  95. setTimeout(run)
  96. if (vueHook) vueHook.call(this, id)
  97. },
  98. enumerable: true,
  99. configurable: true,
  100. })
  101. // 如果已经有 aid 了就开始第一次运行
  102. if (aid) run()
  103. })()

QingJ © 2025

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