Created
August 31, 2024 05:34
-
-
Save huaminghuangtw/6d691b0314738be2b37f5317082558c3 to your computer and use it in GitHub Desktop.
A JavaScript script to automatically extract and download YouTube video URLs from a playlist page. This can be useful for exporting videos from the Watch Later playlist. For removing all videos from the Watch Later playlist please visit: https://gist.github.com/astamicu/eb351ce10451f1a51b71a1287d36880f
This file contains hidden or 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
function getVideoURLs() { | |
const videoElements = document.querySelectorAll('a#video-title'); | |
const videoURLs = Array.from(videoElements).map(video => { | |
const url = new URL(video.href); | |
const videoId = url.searchParams.get('v'); | |
return `https://www.youtube.com/watch?v=${videoId}`; | |
}).filter(url => url.includes('v=')); | |
return videoURLs; | |
} | |
function scrollToBottom() { | |
return new Promise((resolve) => { | |
let lastHeight = document.documentElement.scrollHeight; | |
const interval = setInterval(() => { | |
window.scrollTo(0, document.documentElement.scrollHeight); | |
if (document.documentElement.scrollHeight > lastHeight) { | |
lastHeight = document.documentElement.scrollHeight; | |
} else { | |
clearInterval(interval); | |
resolve(); | |
} | |
}, 1000); | |
}); | |
} | |
function downloadAsTextFile(data, filename) { | |
const blob = new Blob([data], { type: 'text/plain' }); | |
const url = URL.createObjectURL(blob); | |
const a = document.createElement('a'); | |
a.href = url; | |
a.download = filename; | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
URL.revokeObjectURL(url); | |
} | |
async function extractAllVideoURLsFromPlaylist() { | |
await scrollToBottom(); | |
const videoURLs = getVideoURLs(); | |
const videoURLsText = videoURLs.join('\n'); | |
downloadAsTextFile(videoURLsText, 'youtube_playlist_video_urls.txt'); | |
} | |
extractAllVideoURLsFromPlaylist(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment