左上角下载当前播放歌曲
右上角下载播放列表全部音乐
// ==UserScript==
// @name Get Song Info and Download All
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Get current playing song and artist and download all songs in the playlist
// @author Eadny
// @match *://y.qq.com/n/ryqq/player*
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
var allDownloadButton = document.createElement("button");
allDownloadButton.innerHTML = "全部下载";
allDownloadButton.id = "allDownloadButton";
document.body.appendChild(allDownloadButton);
GM_addStyle(`
#allDownloadButton {
position: fixed;
right: 0;
top: 0;
z-index: 9999;
}
`);
var isDownloading = false;
allDownloadButton.addEventListener("click", function() {
if (!isDownloading) {
isDownloading = true;
downloadAllSongs();
}
});
function downloadAllSongs() {
var songInfoList = [];
function downloadNextSong() {
var songNameElement = document.querySelector('.song_info__name a');
var artistElement = document.querySelector('.song_info__singer a');
var songName = songNameElement ? songNameElement.innerText : "未找到歌曲名字";
var artist = artistElement ? artistElement.innerText : "未找到歌手名字";
var audioElement = document.querySelector('audio');
var songUrl = audioElement ? audioElement.src : "未找到歌曲链接";
var fileName = songName + '-' + artist + '.mp3';
songInfoList.push({ songName, artist, songUrl });
GM_xmlhttpRequest({
method: "GET",
url: songUrl,
responseType: "blob",
onload: function(response) {
var blob = response.response;
var link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
var nextButton = document.querySelector('.btn_big_next');
if (nextButton) {
nextButton.click();
if (isDuplicate(songInfoList[songInfoList.length - 1], songInfoList.slice(0, -1))) {
isDownloading = false;
alert('全部歌曲下载完成');
} else {
downloadNextSong();
}
}
},
});
}
downloadNextSong();
}
function isDuplicate(currentSong, songList) {
return songList.some(song => song.songName === currentSong.songName && song.artist === currentSong.artist && song.songUrl === currentSong.songUrl);
}
var button = document.createElement("button");
button.innerHTML = "下载";
button.id = "getSongInfoButton";
document.body.appendChild(button);
GM_addStyle(`
#getSongInfoButton {
position: fixed;
left: 0;
top: 0;
z-index: 9999;
}
`);
button.addEventListener("click", function() {
var songNameElement = document.querySelector('.song_info__name a');
var artistElement = document.querySelector('.song_info__singer a');
var songName = songNameElement ? songNameElement.innerText : "未找到歌曲名字";
var artist = artistElement ? artistElement.innerText : "未找到歌手名字";
var audioElement = document.querySelector('audio');
var songUrl = audioElement ? audioElement.src : "未找到歌曲链接";
var fileName = songName + '-' + artist + '.mp3';
GM_xmlhttpRequest({
method: "GET",
url: songUrl,
responseType: "blob",
onload: function(response) {
var blob = response.response;
var link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
},
});
});
})();