Skip to content

Commit

Permalink
fixed issue #7
Browse files Browse the repository at this point in the history
  • Loading branch information
oviirup committed May 14, 2021
1 parent 9a00f3c commit 572fa4d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "JioSaavn Downloader ᴺᴱᵂ",
"version": "0.6",
"version": "0.6.1",
"manifest_version": 2,
"description": "__MSG_description__",
"icons": {
Expand Down
40 changes: 22 additions & 18 deletions src/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ const getSongsData = async (type, token, callback) => {
type,
}
console.log('songs =>', result);
callback(result, true)
callback(result)
},
error: (res) => callback(res, false)
error: () => callback()
});
}
//#endregion
Expand All @@ -91,7 +91,6 @@ const getURLArrayBuffer = (url, onload, onprogress, onerror) => {
if (xhr.status === 200) {
onload(xhr.response)
} else {
console.warn("Requested URL Forbiden !")
onprogress && onprogress(false);
onerror && onerror();
}
Expand Down Expand Up @@ -152,31 +151,36 @@ const downloadWithData = (song, onSuccess, onError) => {
//#endregion

//#region //* Download Set of Songs as a Zip
const downloadSongsAsZip = function (list, onSuccess) {
const downloadSongsAsZip = function (list, onSuccess = () => { }, onError = () => { }) {
const { title, songs, image } = list
// create a zip
var zip = new JSZip();
var num = 0
var zip = new JSZip()
var a = 0, b = 0, c = 0
// Download cover image for albums
if (list.type == 'playlist') {
var cover = image.replace('c.saavncdn.com', 'corsdisabledimage.tuhinwin.workers.dev');
getURLArrayBuffer(cover, (image) => {
zip.file(`_cover_.jpg`, image);
})
}
songs.forEach((song, i) => {
songs.forEach((song) => {
// get a single song blob
getSongBlob(song, (blob) => {
zip.file(`${song.title}.mp3`, blob);
num++;
if (num === songs.length) setTimeout(() => {
// toast("Compressing & Zipping the Downloads");
downloadZip(zip, title);
onSuccess && onSuccess()
}, 1000);
}, () => num++);
});
};
getSongBlob(
song,
(blob) => {
zip.file(`${song.title}.mp3`, blob)
++a
if ((a + b) === songs.length && a !== 0) setTimeout(() => {
downloadZip(zip, title)
onSuccess()
if (b !== 0) toast('Some of the songs are not downloaded')
}, 1000)
},
() => {
if (++b === songs.length) onError()
})
})
}
//#endregion

//#region //* Download a Zip blob as File via FileSaver
Expand Down
50 changes: 35 additions & 15 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,26 @@ const add_album_download_btn = () => {
const token = window.location.href.match(/.*\/(.*)/)[1]
icon.removeClass('o-icon-download').addClass('o-icon-download-progress');
// Get album data
getSongsData('album', token, (result, status) => {
if (status) {
// Display Toast
toast(`Now Downloading Album : ${result.title}`)
// Download zip file
downloadSongsAsZip(result, () => {
getSongsData('album', token, (result) => {
if (!result) {
toast('Sorry! Cannot download this Album');
icon.removeClass('o-icon-download-progress').addClass('o-icon-download')
return
}
// Display Toast
toast(`Now Downloading Album : ${result.title}`)
// Download zip file
downloadSongsAsZip(
result,
() => {
icon.addClass('o-icon-download').removeClass('o-icon-download-progress');
toast("Compressing & Zipping the Downloads");
})
} else toast('Cannot download this Album')
},
() => {
icon.addClass('o-icon-download').removeClass('o-icon-download-progress');
toast("Unable to download the album !");
}
)
})
});
if (firstBtn.parent().find($('.album_download_btn')).length == 0)
Expand All @@ -72,16 +82,25 @@ const add_playlist_download_btn = () => {
const token = window.location.href.match(/.*\/(.*)/)[1]
icon.removeClass('o-icon-download').addClass('o-icon-download-progress')
// Get album data
getSongsData('playlist', token, (result, status) => {
if (status) {
// Display Toast
toast(`Now Downloading Playlist : ${result.title}`)
// Download Zip
downloadSongsAsZip(result, () => {
getSongsData('playlist', token, (result) => {
if (!result) {
toast('Sorry! Cannot download this Playlist');
icon.removeClass('o-icon-download-progress').addClass('o-icon-download')
return
}
// Display Toast
toast(`Now Downloading Playlist : ${result.title}`)
// Download Zip
downloadSongsAsZip(
result,
() => {
icon.addClass('o-icon-download').removeClass('o-icon-download-progress');
toast("Compressing & Zipping the Downloads");
},
() => {
icon.addClass('o-icon-download').removeClass('o-icon-download-progress');
toast("Unable to download the playlist !");
})
} else toast('Cannot download this Playlist')
})
});
if (firstBtn.parent().find($('.playlist_download_btn')).length == 0)
Expand Down Expand Up @@ -159,6 +178,7 @@ const toast = (message) => {

//#region //? Run on Plugin Initialization
var initPlugin = () => {
localStorage.download_bitrate = localStorage.download_bitrate || 320
hideAds();
add_song_download_btn();
add_album_download_btn();
Expand Down

0 comments on commit 572fa4d

Please sign in to comment.