获取B站字幕

点击左上角按钮,将获取到的B站字幕以提示框的方式展示出来

  1. // ==UserScript==
  2. // @name 获取B站字幕
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description 点击左上角按钮,将获取到的B站字幕以提示框的方式展示出来
  6. // @author 贺墨于
  7. // @match https://www.bilibili.com/*
  8. // @require https://unpkg.com/jquery@3.6.0/dist/jquery.min.js
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. function getQueryVariable(variable){
  14. var query = window.location.search.substring(1);
  15. var vars = query.split("&");
  16. for (var i=0;i<vars.length;i++) {
  17. var pair = vars[i].split("=");
  18. if(pair[0] == variable){return pair[1];}
  19. }
  20. return(false);
  21. }
  22.  
  23. function getSubtitle(json) {
  24. let content = [];
  25. let len = json.length;
  26. for (let i = 0; i < len; i++) {
  27. let pair = json[i].content.split('\n')[0]
  28. content.push(pair)
  29. }
  30. return content.join(',');
  31. }
  32.  
  33. function getJsonURL(aid, cid) {
  34. return new Promise((resovle, reject) => {
  35. $.ajax({
  36. url: `https://api.bilibili.com/x/player/v2?cid=${cid}&aid=${aid}`,
  37. success: function(res){
  38. let subtitles = res.data.subtitle.subtitles;
  39. if (subtitles.length > 0) {
  40. resovle(subtitles[0].subtitle_url);
  41. } else {
  42. console.log('-- 该视频无字幕! ---')
  43. reject('-- 该视频无字幕! ---')
  44. }
  45. },
  46. error: function() {
  47. reject()
  48. }
  49. })
  50. })
  51. }
  52.  
  53. function getJson(url) {
  54. return new Promise((resovle, reject) => {
  55. $.ajax({
  56. url,
  57. success: function(res){
  58. resovle(res.body)
  59. },
  60. error: function() {
  61. reject()
  62. }
  63. })
  64. })
  65. }
  66.  
  67. window.showSubtitlesDialog = function(content) {
  68. document.getElementById('subtitlesDialog').style.display = 'block'
  69. document.getElementById('subtitlesContent').innerHTML = content
  70. }
  71.  
  72. window.outSubtitle = function() {
  73. Promise.all([
  74. new Promise((resovle, reject) => {
  75. $.ajax({
  76. url: `https://api.bilibili.com/x/web-interface/view?bvid=${bvid}`,
  77. success: function(res){
  78. resovle(res.data.aid)
  79. },
  80. error: function() {
  81. reject()
  82. }
  83. })
  84. }),
  85. new Promise((resovle, reject) => {
  86. $.ajax({
  87. url: `https://api.bilibili.com/x/player/pagelist?bvid=${bvid}&jsonp=jsonp`,
  88. success: function(res){
  89. let pvList = res.data
  90. let p = getQueryVariable('p')
  91. if (p) {
  92. resovle(pvList[+p-1].cid)
  93. } else {
  94. resovle(pvList[0].cid)
  95. }
  96. },
  97. error: function() {
  98. reject()
  99. }
  100. })
  101. })
  102. ]).then(resp => {
  103. let aid = resp[0];
  104. let cid = resp[1];
  105. return getJsonURL(aid, cid)
  106. }).then(resp => {
  107. return getJson(resp)
  108. }).then(resp => {
  109. let content = getSubtitle(resp)
  110. // console.log(content)
  111. if (!content) {
  112. alert('该视频无字幕!')
  113. } else {
  114. //alert(content)
  115. showSubtitlesDialog(content)
  116. console.log(content)
  117. }
  118. }).catch(e => {
  119. alert(e ? e : '-- 获取字幕失败! --')
  120. })
  121. }
  122.  
  123. window.bvid = window.location.pathname.replaceAll('/video/', '').replaceAll('/', '')
  124.  
  125. var htmlStr = '<div id="subtitlesDialog" style="z-index:10000; width: 500px; height: 500px; overflow-y: scroll; background-color: #fff;display: none; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); position: fixed; top: 0; right: 0; bottom: 0; left: 0; margin: auto; padding: 15px;">' +
  126. '<div style="text-align: center; margin-bottom: 10px;">' +
  127. '<button onclick="javascript: document.getElementById(\'subtitlesDialog\').style.display = \'none\';">关闭</button>' +
  128. '</div>' +
  129. '<div id="subtitlesContent">' +
  130. "</div>" +
  131. "</div>"
  132.  
  133.  
  134.  
  135. $(function() {
  136. $('body').append(htmlStr)
  137. $('body').append('<span id="outSubtitles" style="position: fixed; top: 15%; left: 0; background: #00aeec; color: white; padding: 8px; border-bottom-right-radius: 8px; border-top-right-radius: 8px; cursor: pointer;" onclick="outSubtitle()">获取字幕</span>')
  138. })

QingJ © 2025

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