黑猫警长Bilibili音频下载
// ==UserScript==
// @name 黑猫警长Bilibili音频下载
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.0.10
// @description try to take over the world!
// @author 作者静水流深 黑猫警长搬运
// @match https://www.bilibili.com/video/*
// @grant unsafeWindow
// @grant GM_registerMenuCommand
// @grant GM_notification
// ==/UserScript==
(function() {
'use strict'
function getAudioUrl() {
let audio_list = unsafeWindow.__playinfo__.data.dash.audio;
if (audio_list.length > 0) {
return unsafeWindow.__playinfo__.data.dash.audio[0].baseUrl;
}
else {
GM_notification("音频提取失败", "提示");
return null;
}
}
function fetchDownload({url, download_type}) {
if (!url) return;
let download_file_name;
let title = document.querySelector('li.on > a')
if (!title) {
download_file_name = `${document.getElementsByTagName('h1')[0].innerText}.${download_type}`
} else {
download_file_name = `${title.getAttribute('title')}.${download_type}`
}
fetch(url)
.then(res => res.blob())
.then(blob => {
const link = document.createElement('a')
link.style.display = 'none'
document.body.appendChild(link)
const download_url = window.URL.createObjectURL(blob)
link.href = download_url;
link.download = download_file_name
link.click()
document.body.removeChild(link)
window.URL.revokeObjectURL(download_url)
})
}
GM_registerMenuCommand(
"音频下载",
function () {
fetchDownload({
url: getAudioUrl(),
download_type: 'mp3'
});
}
)
})();