Created
December 9, 2019 18:44
-
-
Save g-rohit/ead3e7c3802a64ccdb633afc7598b794 to your computer and use it in GitHub Desktop.
add a download btn to svn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nowPlaying = document.getElementById('now-playing'); | |
if (nowPlaying) { | |
var meta = nowPlaying.getElementsByClassName('meta-wrap')[0]; | |
var a = document.createElement('a'); | |
a.setAttribute('id', 'download-song'); | |
a.setAttribute('class', 'btn x-small green'); | |
a.style.display = 'inline'; | |
a.textContent = 'Download Song'; | |
meta.appendChild(a); | |
console.log('playing'); | |
var script = document.createElement('script'); | |
script.textContent = '' | |
+ 'document.getElementById("download-song").addEventListener("click", function() {' | |
+ 'window.postMessage({' | |
+ 'url: Player.currentSongUrl,' | |
+ 'song: Player.getCurrentSong()' | |
+ '}, "*");' | |
+ '}, false);'; | |
document.body.appendChild(script); | |
} | |
window.addEventListener("message", function (event) { | |
if (event.source != window) | |
return; | |
if (event.data.url && event.data.song) { | |
console.log(event.data.url, event.data.song); | |
var title = event.data.song.title.replace(/\/\\*/, "&"); | |
var album = event.data.song.album.replace(/\/\\*/, "&"); | |
console.log(title, album); | |
var options = { | |
url: event.data.url, | |
filename: title + " - " + album + ".mp3" | |
}; | |
chrome.runtime.sendMessage({download: options}, function (res) { | |
console.log(res.msg); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment