Skip to content

Instantly share code, notes, and snippets.

@johnsnook
Last active March 15, 2019 15:44
Show Gist options
  • Select an option

  • Save johnsnook/55986e23a606b6d73abd4619f481c071 to your computer and use it in GitHub Desktop.

Select an option

Save johnsnook/55986e23a606b6d73abd4619f481c071 to your computer and use it in GitHub Desktop.
Put this in a chrome bookmark url field and click on while on a youtube playlist to download a vlc playlist.
javascript:(function () {
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
vars[key] = value;
});
return vars;
}
function getUrlParam(parameter, defaultvalue) {
var urlparameter = defaultvalue;
if (window.location.href.indexOf(parameter) > -1) {
urlparameter = getUrlVars()[parameter];
}
return urlparameter;
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
var eTitle = document.getElementById('title');
var sTitle = eTitle.getElementsByTagName('a').length > 0 ? eTitle.getElementsByTagName('a')[0].innerText : 'Playlist';
var listId = getUrlParam('list', false);
var list = [];
var endPoints = document.getElementsByClassName('yt-simple-endpoint');
for (i = 0; i < endPoints.length; i++)
{
let sHref = endPoints.item(i).href;
if (sHref !== '' && sHref.indexOf('watch?') > 0)
{
if (listId !== false && sHref.indexOf('&list=' + listId) !== -1) {
if (!list.includes(sHref)) {
list.push(sHref);
}
} else if (listId === false) {
if (sHref.indexOf('&list') !== -1) {
sHref = sHref.substr(0, sHref.indexOf('&list'));
}
if (!list.includes(sHref)) {
list.push(sHref);
}
} else {
continue;
}
}
}
var xmlDoc = document.implementation.createDocument(null, "playlist");
xmlDoc.firstChild.setAttribute('xmlns', "http://xspf.org/ns/0/");
xmlDoc.firstChild.setAttribute('xmlns:vlc', "http://www.videolan.org/vlc/playlist/ns/0/");
xmlDoc.firstChild.setAttribute('version', "1");
var title = xmlDoc.createElement('title');
title.append(sTitle);
xmlDoc.firstChild.append(title);
var tracklist = xmlDoc.createElement('trackList');
var extensions = xmlDoc.createElement('extension');
var loc, trackextension, vlcid, vlcitem, tid;
extensions.setAttribute('application', 'http://www.videolan.org/vlc/playlist/0');
for (i = 0; i < list.length; i++)
{
track = xmlDoc.createElement('track');
loc = xmlDoc.createElement('location');
loc.textContent = list[i];
trackextension = xmlDoc.createElement('extension');
trackextension.setAttribute('application', 'http://www.videolan.org/vlc/playlist/0');
vlcid = xmlDoc.createElement('vlc:id');
vlcid.textContent = i;
trackextension.append(vlcid);
track.append(trackextension);
track.append(loc);
tracklist.append(track);
vlcitem = xmlDoc.createElement('vlc:item');
vlcitem.setAttribute('tid', i);
extensions.append(vlcitem);
}
xmlDoc.firstChild.append(tracklist);
xmlDoc.firstChild.append(extensions);
var xmlText = '<?xml version="1.0" encoding="UTF-8"?>' + new XMLSerializer().serializeToString(xmlDoc);
download(sTitle + '.xspf', xmlText);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment