Skip to content

Instantly share code, notes, and snippets.

@ShetlandJ
Last active October 30, 2023 19:11
Show Gist options
  • Save ShetlandJ/abd6fcab93d91002b945ba63960d3359 to your computer and use it in GitHub Desktop.
Save ShetlandJ/abd6fcab93d91002b945ba63960d3359 to your computer and use it in GitHub Desktop.
Primus Toasterland song download script
// This is a script to download all songs in a playlist from Toasterland. In order to accomplish this, you can either paste this script into the console tab of the Inspector, or you can run a minifier on this and add it as a bookmark, prefixing it with javascript: ...
// Full code with comments:
var scripts = document.querySelectorAll("script[type='text/javascript']");
// get the last script tag, which contains the player, and get the inner content.
var playerScript = null;
if (scripts) {
playerScript = scripts[scripts.length - 1];
playerScript = playerScript.textContent || playerScript.innerText
}
if (!playerScript) {
console.log("No player script found");
}
// Use a regular expression to match and extract encoded URLs
var urlRegex = /encodeURI\("([^"]+)"\)/g;
var urls = [];
var match;
// Match the URLs in the string
while ((match = urlRegex.exec(playerScript)) !== null) {
var encodedUrl = match[1];
var decodedUrl = decodeURI(encodedUrl);
urls.push(decodedUrl);
}
// Download file function
async function downloadFile(url, filename) {
const response = await fetch(url);
const blob = await response.blob();
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = filename || url.split('/').pop();
link.click();
}
// Adjust the URLs by adding "s" to "http"
const modifiedURLs = urls.map(url => {
if (url.startsWith('http://')) {
return url.replace('http://', 'https://');
} else if (url.startsWith('https://')) {
return url; // Already HTTPS
} else {
return url; // Other protocols, leave unchanged
}
});
// Download each modified URL with a delay
for (let i = 0; i < modifiedURLs.length; i++) {
setTimeout(function (url) {
downloadFile(url);
}, i * 1000, modifiedURLs[i]); // Adjust the delay (1000 milliseconds = 1 second)
}
// minified bookmarklet:
javascript:var match,scripts=document.querySelectorAll("script[type='text/javascript']"),playerScript=null;scripts&&(playerScript=(playerScript=scripts[scripts.length-1]).textContent||playerScript.innerText),playerScript||console.log("No player script found");for(var urlRegex=/encodeURI\("([^"]+)"\)/g,urls=[];null!==(match=urlRegex.exec(playerScript));){var t,e=decodeURI(match[1]);urls.push(e)}async function downloadFile(t,e){let r=await fetch(t),l=await r.blob(),p=document.createElement("a");p.href=window.URL.createObjectURL(l),p.download=e||t.split("/").pop(),p.click()}const modifiedURLs=urls.map(t=>t.startsWith("http://")?t.replace("http://","https://"):(t.startsWith("https://"),t));for(let i=0;i<modifiedURLs.length;i++)setTimeout(function(t){downloadFile(t)},1e3*i,modifiedURLs[i]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment