QQ音乐网页版下载 tampermonkey(油猴)下载脚本

文章描述了一个用户脚本,允许用户在Y.qq音乐网站上下载当前播放的歌曲和播放列表中的所有音乐。它使用JavaScript和GM_xmlhttpRequest进行网络请求,获取音频文件并下载。
摘要由CSDN通过智能技术生成

左上角下载当前播放歌曲

右上角下载播放列表全部音乐

// ==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);
            },
        });
    });

})();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值