Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How Pick a Random YouTube Comment
A recursive async function for picking a random YouTube comment
Run it by adding your GCP project API key and video ID, then open the HTML page in a browser.
<!DOCTYPE html>
<html lang="en">
<script src=""></script>
body {
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
hr {
width: 100%;
border: 1px solid gainsboro;
<img width="50px">
<button onclick="pickWinner()">Request Winner</button>
<script src="pick-winner.js"></script>
const getComments = async(pageToken = '') => {
const key = 'API_KEY'
const videoId = 'VIDEO_ID'
const params = new URLSearchParams({
part: 'snippet',
const api = ''
const url = new URL(`${api}/commentThreads?${params.toString()}`)
const res = await fetch(url);
const { items, nextPageToken } = await res.json();
const snippets = => v.snippet.topLevelComment.snippet);
comments = [...snippets, ...comments]
if (nextPageToken) {
await getComments(nextPageToken)
let comments = [];
const pickWinner = () => {
const main = document.querySelector('h1');
const info = document.querySelector('p');
main.innerHTML = '🙏 Awaiting the Swag Gods Decision 🙏';
// Add a timeout just to add to the suspense
setTimeout(() => {
// Using Lodash here to keep it simple
const uniq = _.uniqBy(comments, obj => obj.authorChannelId.value)
const choice = Math.floor(Math.random() * (uniq.length - 1))
const winner = uniq[choice];
main.innerHTML = `🎉 ${winner.authorDisplayName} 🎉`;
info.innerHTML = `You are the chosen one. Index ${choice} of ${uniq.length} souls. May this shirt bring you many years of warmth, fashion, and comfort.`
document.querySelector('img').src = winner.authorProfileImageUrl
document.querySelector('em').innerHTML = winner.textDisplay
}, 5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment