Skip to content

Instantly share code, notes, and snippets.

@jorilallo
Created May 7, 2013 22:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorilallo/5536679 to your computer and use it in GitHub Desktop.
Save jorilallo/5536679 to your computer and use it in GitHub Desktop.
var downloads = [];
var downloadSong = function(url, tabid){
if( chrome.webRequest.onBeforeRequest.hasListeners() )
chrome.webRequest.onBeforeRequest.removeListener(findmp3);
chrome.tabs.sendMessage( tabid , {action: "getSong", url : url} );
setTimeout(function(){ startListener(); },500)
}
var findmp3 = function(details){
if( details.url.indexOf("cloudfront.net/mp3/") === -1 && downloads.indexOf(details.url) ) return;
downloadSong(details.url, details.tabId)
};
var startListener = function(){
chrome.webRequest.onBeforeRequest.addListener(
findmp3,
{urls: ["*://*.cloudfront.net/mp3/*"]},
['blocking']
);
};
startListener();
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action == "getSong"){
var title = document.title;
if( title === 'Spotify Web Player' ) return;
title = title.replace("- Spotify","");
var a = document.createElement('a');
a.download = title + '.mp3';
a.href = request.url;
a.dataset.downloadurl = ['audio/mpeg', a.download, a.href].join(':');
a.click();
}else{
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment