您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
点击左上角按钮,将获取到的B站字幕以提示框的方式展示出来
当前为
// ==UserScript== // @name 获取B站字幕 // @namespace http://tampermonkey.net/ // @version 0.2 // @description 点击左上角按钮,将获取到的B站字幕以提示框的方式展示出来 // @author 贺墨于 // @match https://www.bilibili.com/* // @require https://unpkg.com/[email protected]/dist/jquery.min.js // @grant none // @license MIT // ==/UserScript== function getQueryVariable(variable){ var query = window.location.search.substring(1); var vars = query.split("&"); for (var i=0;i<vars.length;i++) { var pair = vars[i].split("="); if(pair[0] == variable){return pair[1];} } return(false); } function getSubtitle(json) { let content = []; let len = json.length; for (let i = 0; i < len; i++) { let pair = json[i].content.split('\n')[0] content.push(pair) } return content.join(','); } function getJsonURL(aid, cid) { return new Promise((resovle, reject) => { $.ajax({ url: `https://api.bilibili.com/x/player/v2?cid=${cid}&aid=${aid}`, success: function(res){ let subtitles = res.data.subtitle.subtitles; if (subtitles.length > 0) { resovle(subtitles[0].subtitle_url); } else { console.log('-- 该视频无字幕! ---') reject() } }, error: function() { reject() } }) }) } function getJson(url) { return new Promise((resovle, reject) => { $.ajax({ url, success: function(res){ resovle(res.body) }, error: function() { reject() } }) }) } window.outSubtitle = function() { Promise.all([ new Promise((resovle, reject) => { $.ajax({ url: `https://api.bilibili.com/x/web-interface/view?bvid=${bvid}`, success: function(res){ resovle(res.data.aid) }, error: function() { reject() } }) }), new Promise((resovle, reject) => { $.ajax({ url: `https://api.bilibili.com/x/player/pagelist?bvid=${bvid}&jsonp=jsonp`, success: function(res){ let pvList = res.data let p = +getQueryVariable('p') if (p && p > 0) { resovle(pvList[p-1].cid) } else { reject() } }, error: function() { reject() } }) }) ]).then(resp => { let aid = resp[0]; let cid = resp[1]; return getJsonURL(aid, cid) }).then(resp => { return getJson(resp) }).then(resp => { let content = getSubtitle(resp) // console.log(content) if (!content) { alert('该视频无字幕!') } else { alert(content) } }).catch(() => { alert('-- 获取字幕失败! --') }) } var bvid_s = window.location.pathname.lastIndexOf('/') + 1 window.bvid = window.location.pathname.substring(bvid_s) $(function() { $('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>') })
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址