获取B站字幕

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

目前为 2022-02-12 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 获取B站字幕
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  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.outSubtitle = function() {
  68. Promise.all([
  69. new Promise((resovle, reject) => {
  70. $.ajax({
  71. url: `https://api.bilibili.com/x/web-interface/view?bvid=${bvid}`,
  72. success: function(res){
  73. resovle(res.data.aid)
  74. },
  75. error: function() {
  76. reject()
  77. }
  78. })
  79. }),
  80. new Promise((resovle, reject) => {
  81. $.ajax({
  82. url: `https://api.bilibili.com/x/player/pagelist?bvid=${bvid}&jsonp=jsonp`,
  83. success: function(res){
  84. let pvList = res.data
  85. let p = +getQueryVariable('p')
  86. if (p && p > 0) {
  87. resovle(pvList[p-1].cid)
  88. } else {
  89. reject()
  90. }
  91. },
  92. error: function() {
  93. reject()
  94. }
  95. })
  96. })
  97. ]).then(resp => {
  98. let aid = resp[0];
  99. let cid = resp[1];
  100. return getJsonURL(aid, cid)
  101. }).then(resp => {
  102. return getJson(resp)
  103. }).then(resp => {
  104. let content = getSubtitle(resp)
  105. // console.log(content)
  106. if (!content) {
  107. alert('该视频无字幕!')
  108. } else {
  109. alert(content)
  110. }
  111. }).catch(() => {
  112. alert('-- 获取字幕失败! --')
  113. })
  114. }
  115.  
  116. var bvid_s = window.location.pathname.lastIndexOf('/') + 1
  117. window.bvid = window.location.pathname.substring(bvid_s)
  118.  
  119. $(function() {
  120. $('html').append('<span id="outSubtitles" style="position: fixed; top: 20%; left: 0; background: #00aeec; color: white; padding: 8px; border-bottom-right-radius: 8px; border-top-right-radius: 8px; cursor: pointer;" onclick="outSubtitle()">获取字幕</span>')
  121. })

QingJ © 2025

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