Last active
October 17, 2024 06:32
-
-
Save deansimcox/20c247fc5b7feb82f6c91967fb331e23 to your computer and use it in GitHub Desktop.
Some JS that will go through your Watch Later playlist on Youtube, and remove items sequentially
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
/** | |
* Works on your watch later playlist `/playlist?list=WL` | |
* Update NUMBER_OF_LATEST_VIDEOS_TO_KEEP if you want to keep more/less than the 20 most recent videos | |
*/ | |
const NUMBER_OF_LATEST_VIDEOS_TO_KEEP = 60; | |
async function doThing(vidArray) { | |
for (const vid of vidArray) { | |
vid.click(); | |
await Promise.allSettled([ | |
poll(() => { | |
const removeBtn = document.querySelector( | |
"ytd-popup-container ytd-menu-service-item-renderer:nth-child(3)" | |
); | |
if (!removeBtn) { | |
return null; | |
} | |
console.log("removeBtn", removeBtn); | |
removeBtn.click(); | |
}), | |
wait(1200), | |
]); | |
} | |
} | |
function wait(milliseconds) { | |
return new Promise((resolve) => { | |
setTimeout(resolve, milliseconds); | |
}); | |
} | |
function poll(fn, timeout, interval) { | |
var endTime = Number(new Date()) + (timeout || 1200); | |
interval = interval || 600; | |
return new Promise(function (resolve, reject) { | |
(function p() { | |
// If the condition is met, we're done! | |
if (fn()) { | |
resolve(); | |
} | |
// If the condition isn't met but the timeout hasn't elapsed, go again | |
else if (Number(new Date()) < endTime) { | |
setTimeout(p, interval); | |
} | |
// Didn't match and too much time, reject! | |
else { | |
reject("timed out for " + fn + ": " + arguments); | |
} | |
})(); | |
}); | |
} | |
const vidBtns = Array.from( | |
document.querySelectorAll( | |
`#contents ytd-playlist-video-renderer:nth-child(n+${NUMBER_OF_LATEST_VIDEOS_TO_KEEP + 1}) yt-icon-button button` | |
) | |
).reverse(); | |
doThing(vidBtns); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment