Skip to content

Instantly share code, notes, and snippets.

@cadadr
Last active June 27, 2022 19:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cadadr/43d574cc35ba37823b1adff07b9a6a3c to your computer and use it in GitHub Desktop.
Save cadadr/43d574cc35ba37823b1adff07b9a6a3c to your computer and use it in GitHub Desktop.
// This script will generate an OPML from your YouTube subscriptions
// and write it to clipboard, or failing that, to the browser console.
// In order to use it, navigate to <https://www.youtube.com/feed/channels>,
// open the browser developer console, and copy-paste the following
// javascript into the prompt.
//
// In order to grab the resulting script in Firefox, if copying
// to clipboard fails, right-click on the output message in the
// console and select "Copy object" from the menu.
//
// This can be made into a bookmarklet easily. First, copy the script below.
// Then, open the bookmark manager, create a bookmark somewhere appropriate.
// Into the "Location" input type "javascript:" and then paste the script
// you copied. Should work fine.
//
// It'll alert to tell you if the resulting OPML was copied to clipboard
// or written to the console.
(async function(){
var rssbase = 'http://www.youtube.com/feeds/videos.xml?channel_id=';
var opml = document.implementation.createDocument("", 'opml', null);
var head = opml.createElement('head');
opml.documentElement.appendChild(head);
var title = opml.createElement('title');
title.innerHTML = 'YouTube Subscriptions OPML';
head.appendChild(title);
var dateCreat = opml.createElement('dateCreated');
var dateMod = opml.createElement('dateModified');
var date = new Date().toString();
dateCreat.innerHTML = date;
dateMod.innerHTML = date;
head.appendChild(dateCreat);
head.appendChild(dateMod);
var body = opml.createElement('body');
opml.documentElement.appendChild(body);
Array.from(
document.querySelectorAll(
'#grid-container div#info-section a.channel-link'
)
).map(
function (c) {
var e = opml.createElement('outline');
e.setAttribute('text', c.querySelector('.ytd-channel-name').innerText);
e.setAttribute('title', c.querySelector('.ytd-channel-name').innerText);
e.setAttribute('htmlUrl', c.href);
e.setAttribute('type', 'rss');
e.setAttribute('version', 'RSS2');
e.setAttribute('xmlUrl', rssbase + c.href.split('/').reverse()[0]);
body.appendChild(e);
}
);
var opmlText = new XMLSerializer().serializeToString(opml.documentElement);
try {
await navigator.clipboard.writeText(opmlText);
alert("Wrote OPML to clipboard!");
} catch (err) {
var node = document.createElement('textarea');
var selection = document.getSelection();
node.textContent = opmlText;
document.body.appendChild(node);
selection.removeAllRanges();
node.select();
if(document.execCommand('copy')){
alert("Wrote OPML to clipboard! (via shitty method)");
} else {
console.log(opmlText);
alert("Wrote OPML to console because f*** the web...")
}
selection.removeAllRanges();
document.body.removeChild(node);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment