Greasy Fork is available in English.
从塞壬唱片(https://monster-siren.hypergryph.com/)下载音乐,可以下载单曲或整张专辑。
当前为
// ==UserScript==
// @name 塞壬唱片下载器(Monster-Siren Music Downloader)
// @namespace monster-siren.hypergryph.com
// @match https://monster-siren.hypergryph.com/*
// @grant none
// @version 1.1
// @author sodawatter
// @description 从塞壬唱片(https://monster-siren.hypergryph.com/)下载音乐,可以下载单曲或整张专辑。
// @license MIT
// ==/UserScript==
(function () {
'use strict';
function downloadMusicWithCid(musicId, albumName) {
console.log("Download " + musicId);
var apiUrl = 'https://monster-siren.hypergryph.com/api/song/' + musicId;
// Fetch the JSON data from the API URL
fetch(apiUrl)
.then(function (response) {
return response.json();
})
.then(function (data) {
var sourceUrl = data.data.sourceUrl;
var musicName = data.data.name;
// Create a new XMLHttpRequest object
var xhr = new XMLHttpRequest();
xhr.open('GET', sourceUrl, true);
xhr.responseType = 'blob'; // Set the response type to blob
xhr.onload = function () {
if (xhr.status === 200) {
// Create a new Blob object from the response
var blob = new Blob([xhr.response], { type: 'audio/wav' });
// Create a temporary URL for the Blob object
var url = URL.createObjectURL(blob);
// Create a link element and set the href and download attributes
var link = document.createElement('a');
link.href = url;
link.download = '[' + albumName + '] ' + musicName + '.wav';
link.click(); // Simulate a click on the link to start the download
}
};
xhr.send();
})
.catch(function (error) {
console.error('An error occurred while fetching the music data:', error);
});
}
function getJSON(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.responseText));
} else {
reject(new Error('Error while fetching JSON data'));
}
}
};
xhr.send();
});
}
async function getAlbumId(musicId) {
var apiUrl = 'https://monster-siren.hypergryph.com/api/song/' + musicId;
var albumId = '';
var json_data = await getJSON(apiUrl);
albumId = json_data.data.albumCid;
return albumId;
}
async function getAlbumName(albumId) {
var resName = '';
var json_data = await getJSON('https://monster-siren.hypergryph.com/api/albums');
for (var i = 0; i < json_data.data.length; i++) {
if (json_data.data[i].cid == albumId) {
resName = json_data.data[i].name;
}
}
return resName;
}
async function getMusicIdListOfAlbum(albumId) {
var resList = [];
var json_data = await getJSON('https://monster-siren.hypergryph.com/api/songs');
for (var i = 0; i < json_data.data.list.length; i++) {
if (albumId == json_data.data.list[i].albumCid) {
resList.push(json_data.data.list[i].cid);
}
}
return resList;
}
async function downloadMusic(btn) {
var currentUrl = window.location.href;
var musicId = currentUrl.split('/').pop();
if (!isNaN(musicId) && musicId != '') {
var albumId = await getAlbumId(musicId);
var albumName = await getAlbumName(albumId);
downloadMusicWithCid(musicId, albumName);
btn.textContent = 'Downloading';
setTimeout(function() {
btn.textContent = 'Download This Music';
}, 5000);
} else {
btn.textContent = 'Not a music page';
setTimeout(function() {
btn.textContent = 'Download This Music';
}, 2000);
}
}
async function downloadAlbum(btn) {
var currentUrl = window.location.href;
var musicId = currentUrl.split('/').pop();
if (!isNaN(musicId) && musicId != '') {
var albumId = await getAlbumId(musicId);
var albumName = await getAlbumName(albumId);
var musicList = await getMusicIdListOfAlbum(albumId);
var delay = 1000; // Delay in milliseconds
function downloadMusicWithDelay(musicList, albumName, index) {
if (index >= musicList.length) {
return; // Exit the function when all music has been downloaded
}
downloadMusicWithCid(musicList[index], albumName);
setTimeout(function () {
downloadMusicWithDelay(musicList, albumName, index + 1); // Call the function recursively with the next index
}, delay);
}
downloadMusicWithDelay(musicList, albumName, 0);
btn.textContent = 'Downloading';
setTimeout(function() {
btn.textContent = 'Download This Album';
}, 5000 * musicList.length);
} else {
btn.textContent = 'Not a music page';
setTimeout(function() {
btn.textContent = 'Download This Music';
}, 2000);
}
}
function createButton() {
var downloadMusicButton = document.createElement('button');
downloadMusicButton.textContent = 'Download This Music';
// Style the download button
downloadMusicButton.style.position = 'fixed';
downloadMusicButton.style.zIndex = '9999';
downloadMusicButton.style.top = '10px';
downloadMusicButton.style.left = '10px';
downloadMusicButton.style.padding = '10px';
downloadMusicButton.style.backgroundColor = '#f44336';
downloadMusicButton.style.color = 'white';
downloadMusicButton.style.border = 'none';
downloadMusicButton.style.borderRadius = '5px';
downloadMusicButton.style.cursor = 'pointer';
downloadMusicButton.style.fontFamily = 'Arial, sans-serif';
downloadMusicButton.style.fontSize = '16px';
// Create a button to navigate to the play page
var dowloadAlbumButton = document.createElement('button');
dowloadAlbumButton.textContent = 'Download This Album';
// Style the play button
dowloadAlbumButton.style.position = 'fixed';
dowloadAlbumButton.style.zIndex = '9999';
dowloadAlbumButton.style.top = '50px';
dowloadAlbumButton.style.left = '10px';
dowloadAlbumButton.style.padding = '10px';
dowloadAlbumButton.style.backgroundColor = '#2196f3';
dowloadAlbumButton.style.color = 'white';
dowloadAlbumButton.style.border = 'none';
dowloadAlbumButton.style.borderRadius = '5px';
dowloadAlbumButton.style.cursor = 'pointer';
dowloadAlbumButton.style.fontFamily = 'Arial, sans-serif';
dowloadAlbumButton.style.fontSize = '16px';
// Append the button to the document body
document.body.appendChild(downloadMusicButton);
document.body.appendChild(dowloadAlbumButton);
downloadMusicButton.addEventListener('click', function (e) {
downloadMusic(downloadMusicButton); // Prevent the default click behavior
});
dowloadAlbumButton.addEventListener('click', function (e) {
downloadAlbum(dowloadAlbumButton); // Prevent the default click behavior
});
}
// Wait for the page to load, then download the background music
window.addEventListener('load', function () {
setTimeout(createButton, 1000); // Add a delay to ensure the music file is loaded
});
})();