/
km-downlad.js
37 lines (28 loc) · 1.18 KB
/
km-downlad.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function downloadBlob(blob, name = "km-download.mp3") {
// Convert your blob into a Blob URL (a special url that points to an object in the browser's memory)
const blobUrl = URL.createObjectURL(blob);
// Create a link element
const link = document.createElement("a");
// Set link's href to point to the Blob URL
link.href = blobUrl;
link.download = name;
// Append link to the body
document.body.appendChild(link);
// Dispatch click event on the link
// This is necessary as link.click() does not work on the latest firefox
link.dispatchEvent(
new MouseEvent("click", {
bubbles: true,
cancelable: true,
view: window,
})
);
// Remove link from body
document.body.removeChild(link);
}
const pathName = location.pathname.split("/");
const downloadId = pathName[pathName.length - 1];
const download = document.querySelector(`.new-player${downloadId}`);
const songName = document.querySelector(`#song-title${downloadId}`).parentElement.querySelector("p").textContent
const downloadUrl = download.dataset.songUrl;
fetch(downloadUrl).then((res) => res.blob().then((res) => downloadBlob(res,songName +'.mp3')));