1.购买服务器阿里云:服务器购买地址https://t.aliyun.com/U/Bg6shY若失效,可用地址
阿里云:
服务器购买地址
https://t.aliyun.com/U/Bg6shY若失效,可用地址
https://www.aliyun.com/daily-act/ecs/activity_selection?source=5176.29345612&userCode=49hts92d腾讯云:
https://curl.qcloud.com/wJpWmSfU若失效,可用地址
https://cloud.tencent.com/act/cps/redirect?redirect=2446&cps_key=ad201ee2ef3b771157f72ee5464b1fea&from=console华为云
https://activity.huaweicloud.com/cps.html?fromacct=64b5cf7cc11b4840bb4ed2ea0b2f4468&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=2019052.部署教程
3.代码如下
(function() {'use strict';// 创建UI按钮function createDownloadButton() {const button = document.createElement('div');button.id = 'netease-music-downloader';button.innerHTML = '获取MP3链接';button.style.cssText = `position: fixed;top: 20px;right: 20px;z-index: 9999;background-color: #C20C0C;color: white;padding: 10px 15px;border-radius: 5px;cursor: pointer;font-size: 14px;box-shadow: 0 2px 5px rgba(0,0,0,0.2);transition: all 0.3s ease;`;button.addEventListener('mouseover', function() {button.style.backgroundColor = '#A00A0A';button.style.transform = 'scale(1.05)';});button.addEventListener('mouseout', function() {button.style.backgroundColor = '#C20C0C';button.style.transform = 'scale(1)';});button.addEventListener('click', getMusicUrl);document.body.appendChild(button);// 添加提示信息const tooltip = document.createElement('div');tooltip.id = 'netease-tooltip';tooltip.style.cssText = `position: fixed;top: 70px;right: 20px;z-index: 9999;background-color: rgba(0,0,0,0.8);color: white;padding: 8px 12px;border-radius: 4px;font-size: 12px;max-width: 200px;display: none;`;document.body.appendChild(tooltip);return { button, tooltip };}// 获取当前页面的音乐IDfunction getMusicId() {// 从URL中获取音乐IDconst urlMatch = window.location.href.match(/song\?id=(\d+)/);if (urlMatch) {return urlMatch[1];}// 尝试从页面元素中获取音乐ID// 歌曲详情页const songDetailElement = document.querySelector('.song-detail-abstract .name') ||document.querySelector('.song-name') ||document.querySelector('.f-ff2');if (songDetailElement) {const href = songDetailElement.getAttribute('href') ||songDetailElement.parentElement.getAttribute('href');if (href) {const idMatch = href.match(/id=(\d+)/);if (idMatch) {return idMatch[1];}}}// 播放器中的当前歌曲const playerSong = document.querySelector('.player .name') ||document.querySelector('.j-flag');if (playerSong) {const href = playerSong.getAttribute('href') ||playerSong.parentElement.getAttribute('href');if (href) {const idMatch = href.match(/id=(\d+)/);if (idMatch) {return idMatch[1];}}}return null;}// 获取音乐信息function getMusicInfo() {const title = document.querySelector('.song-name-abstract .name') ||document.querySelector('.song-name') ||document.querySelector('.f-ff2') ||document.querySelector('.player .name');const artist = document.querySelector('.song-detail-abstract .singer') ||document.querySelector('.descrip.s-fc4') ||document.querySelector('.player .by');return {title: title ? title.textContent.trim() : '未知歌曲',artist: artist ? artist.textContent.trim() : '未知歌手'};}// 显示提示信息function showTooltip(message, duration = 3000) {const tooltip = document.getElementById('netease-tooltip');if (tooltip) {tooltip.textContent = message;tooltip.style.display = 'block';setTimeout(() => {tooltip.style.display = 'none';}, duration);}}// 通过API获取音乐链接function getMusicUrl() {const musicId = getMusicId();if (!musicId) {showTooltip('无法获取音乐ID,请确保您在音乐播放页面');return;}const musicInfo = getMusicInfo();showTooltip(`正在获取《${musicInfo.title}》的MP3链接...`);// 解灰有点问题?const apiUrl = `https://music-api.top/song/url/v1?id=${musicId}&level=lossless&randomCNIP=true&unblock=false`;GM_xmlhttpRequest({method: 'GET',url: apiUrl,headers: {'Content-Type': 'application/json'},onload: function(response) {try {const data = JSON.parse(response.responseText);if (data && data.data && data.data.length > 0) {const musicUrl = data.data[0].url;if (musicUrl) {// 复制到剪贴板GM_setClipboard(musicUrl);showTooltip(`MP3链接已复制到剪贴板!`);} else {showTooltip('获取音乐链接失败,可能需要VIP权限或者无版权');}} else {showTooltip('API返回数据格式错误');}} catch (error) {console.error('解析API响应失败:', error);showTooltip('解析API响应失败,请查看控制台');}},onerror: function(error) {console.error('API请求失败:', error);showTooltip('API请求失败,请检查网络连接');}});}// 初始化function init() {// 等待页面加载完成if (document.readyState === 'loading') {document.addEventListener('DOMContentLoaded', createDownloadButton);} else {createDownloadButton();}}// 启动脚本init();})();
该脚本为网易云音乐 MP3 链接获取脚本。
主要作用
在
music.163.com页面右上角插入按钮"获取MP3链接"。自动解析当前页面或播放器里的歌曲 ID,请求一个第三方 API,得到该歌曲的 MP3播放地址。
将获取到的 MP3 链接复制到剪贴板并在页面右上角气泡提示。
生效范围与权限
@match:https://music.163.com/*(网易云音乐网页端所有路径)Grants:
GM_xmlhttpRequest:跨域请求第三方 APIGM_setClipboard:把链接写入剪贴板@connect *:允许请求任意域(脚本里实际请求的是music-api.xiaohan-kaka.top)
核心流程
创建 UI:
createDownloadButton()右上角固定悬浮红色按钮 + 一个用于消息提示的 tooltip。
hover 有轻微放大、变色,点击触发
getMusicUrl()。定位当前歌曲:
getMusicId()先从 URL 里找
song?id=xxxx;如果没有,再从页面元素(歌曲详情 / 播放器 DOM)中找包含
id=xxx的链接。展示歌曲信息(可用于提示):
getMusicInfo()尝试从常见位置取歌名、歌手(取不到则回退"未知歌曲/歌手")。
调用 API 获取直链:
getMusicUrl()若拿到
musicId,拼接:https://music-api.top/song/url/v1?id=<ID>&level=lossless&randomCNIP=true&unblock=false用
GM_xmlhttpRequest发起 GET 请求;成功后解析 JSON,取data[0].url。如果拿到
url:GM_setClipboard(url)并提示"已复制"。
失败则在 tooltip 给出错误文案。提示系统:
showTooltip(message)右上角黑底提示,默认 3 秒后消失。
关键方法
createDownloadButton():插入"获取MP3链接"按钮与提示气泡 UI,并绑定点击事件。getMusicId():多策略提取当前歌曲 ID(URL 优先,其次 DOM)。getMusicInfo():从页面尝试读取歌名 & 歌手,仅用于友好提示。getMusicUrl():整合调用链——获取 ID → 调用第三方 API → 复制结果到剪贴板 → 提示状态。showTooltip():统一出口的轻量提示 UI。
注意:
本文部分变量已做脱敏处理,仅用于测试和学习研究,禁止用于商业用途,不能保证其合法性,准确性,完整性和有效性,请根据情况自行判断。技术层面需要提供帮助,可以通过打赏的方式进行探讨。
没有评论:
发表评论