// ==UserScript== // @name Bilibili音频下载 // @namespace https://bbs.tampermonkey.net.cn/ // @version 0.0.9 // @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' }); } ); var flag = false; function rigister_hot_key({codes, callback}) { document.addEventListener('keydown', function(event) { if (event.code !== codes && !flag) return; flag = true; callback(); }); document.addEventListener('keyup', function(event) { flag = false; }); } rigister_hot_key({ codes: 'KeyH', callback: function() { fetchDownload({ url: getAudioUrl(), download_type: 'mp3' }); } }) })();