[JavaScript] 纯文本查看 复制代码 // ==UserScript==
// [url=home.php?mod=space&uid=170990]@name[/url] 音乐磁场-下载音乐
// [url=home.php?mod=space&uid=467642]@namespace[/url] http://tampermonkey.net/
// [url=home.php?mod=space&uid=1248337]@version[/url] 1.1
// @description 在音乐播放器下方添加下载按钮,点击即可下载音乐
// [url=home.php?mod=space&uid=686208]@AuThor[/url] Your Name
// @license MIT
// [url=home.php?mod=space&uid=195849]@match[/url] *://*.hifini.com/**
// [url=home.php?mod=space&uid=609072]@grant[/url] none
// ==/UserScript==
(function() {
'use strict';
// 创建下载按钮
function createDownloadButton(url) {
var downloadButton = document.createElement('div');
downloadButton.onclick=download;
downloadButton.innerText = '下载';
downloadButton.style.display = 'block';
downloadButton.style.marginTop = '10px';
return downloadButton;
}
function download(){
fetch(getMusicDownloadUrl(), {
method: 'GET',
redirect: 'follow',
})
.then(response => response.blob())
.then(blob => {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = window.ap4.music.title+'-'+window.ap4.music.author+'.m4a';
a.style.display = 'none';
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(url);
})
}
// 获取音乐下载地址
function getMusicDownloadUrl() {
var ap4 = window.ap4;
if (ap4 && ap4.music) {
var music = ap4.music;
return music.url;
}
return null;
}
// 在页面中插入下载按钮
function insertDownloadButton() {
var downloadUrl = getMusicDownloadUrl();
if (downloadUrl) {
var playerElement = document.getElementById('player4');
var downloadButton = createDownloadButton(downloadUrl);
playerElement.parentNode.insertBefore(downloadButton, playerElement.nextSibling);
}
}
// 监听页面加载完成事件,在页面中插入下载按钮
window.addEventListener('load', function() {
insertDownloadButton();
});
})();
|