Skip to content

Instantly share code, notes, and snippets.

@Yukaii
Last active February 16, 2023 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yukaii/e8d6324522fbb277df4b2adb813f3739 to your computer and use it in GitHub Desktop.
Save Yukaii/e8d6324522fbb277df4b2adb813f3739 to your computer and use it in GitHub Desktop.
YouTube Subscriptions to OPML RSS Feeds
let feeds = [...document.querySelectorAll('#main-link.channel-link')].map(
(e) => {
const channelHref = e.href;
const channelLink = window.location.origin + channelHref;
const name = e
.querySelector('.ytd-channel-name yt-formatted-string')
?.textContent.trim();
const [, a, b] = e.href.match('/((?:user)|(?:channel))/(.*)$');
const link =
'https://www.youtube.com/feeds/videos.xml?' +
(a === 'user' ? 'user=' : 'channel_id=') +
b;
return {
name,
link,
channelLink,
};
},
);
function copyToClipboard(text) {
const dummy = document.createElement('textarea');
dummy.style.position = 'absolute';
dummy.style.left = '-9999px';
dummy.style.top = '-9999px';
document.body.appendChild(dummy);
dummy.value = text;
dummy.select();
document.execCommand('copy');
document.body.removeChild(dummy);
}
// generate OPML
const opml = `<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<head>
<title>My RSS Feeds</title>
</head>
<body>
${feeds
.map(
({ name, link, channelLink }) => `
<outline text="${name.replace(
/"/g,
'&quot;',
)}" type="rss" xmlUrl="${link}" htmlUrl="${channelLink}"/>
`,
)
.join('')}
</body>
</opml>`;
copyToClipboard(opml);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment