Last active
January 31, 2022 18:04
-
-
Save umnikos/8c0d6d27bd56e11cb15e5bac87136d2e to your computer and use it in GitHub Desktop.
Download button for bandcamp.com
This file contains 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
// ==UserScript== | |
// @name Bandcamp downloader | |
// @namespace Bandcamp | |
// @version 1.0 | |
// @description Adds a download button next to songs on bandcamp.com | |
// @author umnikos | |
// @match http*://*.bandcamp.com/* | |
// @license GPL3 | |
// @grant GM_download | |
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@1 | |
// ==/UserScript== | |
// I have no clue how web development works. I just know there's an <audio> object in the html | |
// that magically gets updated with the link to the audio file right after the user clicks on the play button | |
// so this code clicks on that button instead of the user and then downloads from that link | |
// code snippets scavenged from https://greasyfork.org/en/scripts/20719-bandcamp-download-mp3s/code | |
function download_song() { | |
var playbutton = document.getElementsByClassName("playbutton")[0]; | |
playbutton.click(); | |
setTimeout((function() { | |
playbutton.click(); | |
}), 0); | |
var audioelement = document.getElementsByTagName("audio")[0]; | |
//var trackname = TralbumData.trackinfo[0].title; | |
var trackname = document.getElementsByClassName("inline_player")[0].getElementsByClassName("title")[0].innerHTML; | |
trackname = trackname.replace(/[^a-z0-9]/gi, ' ').replace(/\s+/g, ' ').toLowerCase(); | |
document.getElementById("downloadButton").innerHTML = "Downloading..."; | |
GM_download({ | |
url: audioelement.src, | |
name: trackname + ".mp3", | |
onerror: (() => {document.getElementById("downloadButton").innerHTML = "Error while downloading.";}), | |
ontimeout: (() => {document.getElementById("downloadButton").innerHTML = "Download timed out.";}) | |
}); | |
} | |
(function() { | |
VM.observe(document.body, () => { | |
if (TralbumData) { | |
if (TralbumData.hasAudio && !TralbumData.freeDownloadPage && TralbumData.trackinfo) { | |
var mydiv = document.getElementsByClassName("tralbumCommands")[0]; | |
var mybutton = document.createElement("a"); | |
mybutton.id = "downloadButton"; | |
mybutton.onclick = download_song; | |
mybutton.classList = ["download-link", "buy-link"]; | |
mybutton.innerHTML = "Download for free!"; | |
mydiv.insertBefore(document.createElement("br"),mydiv.firstChild); | |
mydiv.insertBefore(mybutton, mydiv.firstChild); | |
} | |
return true; | |
} | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment