Skip to content

Instantly share code, notes, and snippets.

@amarilindra
Created April 28, 2023 07:27
Show Gist options
  • Save amarilindra/d0fec5c6a9a9173f41bdf9ac82183e8a to your computer and use it in GitHub Desktop.
Save amarilindra/d0fec5c6a9a9173f41bdf9ac82183e8a to your computer and use it in GitHub Desktop.
This is a JavaScript script for unsubscribing from YouTube channels. The script automatically clicks the unsubscribe button for each channel on the page and removes it from the DOM. The script uses the YouTube DOM elements to perform the unsubscribe action, so it should work regardless of changes to the YouTube website design. To use the script,…
// Get the total number of channels to unsubscribe
var channelCount = document.querySelectorAll("ytd-channel-renderer:not(.ytd-item-section-renderer)").length;
// Call the timer function
unsubscribeTimer();
function unsubscribeTimer() {
// Return if there are no channels left to unsubscribe
if (channelCount === 0) {
return;
}
// Get the element with the subscribe button and click it
var subscribeBtn = document.querySelector('.ytd-subscribe-button-renderer');
subscribeBtn.click();
setTimeout(function() {
// Get the unsubscribe button and click it
var unsubscribeBtn = document.getElementById("confirm-button");
unsubscribeBtn.click();
// Update the channel count and log the result
channelCount--;
console.log(channelCount + " remaining");
// Remove the channel from the DOM
var channelToRemove = document.querySelector("ytd-channel-renderer");
channelToRemove.parentNode.removeChild(channelToRemove);
// Call the timer function again
unsubscribeTimer();
}, 250);
}
@WrayOfSunshine
Copy link

WrayOfSunshine commented Jan 11, 2024

Doesn't seem to be working (at least in Firefox) - the unsubscribe button -looks- like it gets clicked, the objects get removed, but if I refresh then suddenly all my subscriptions are back.

EDIT:

If it helps, I get this when I try to refresh the screen (which shows all the buttons/channels for me again, visually) and then re-run the script:

Uncaught TypeError: unsubscribeBtn is null
unsubscribeTimer debugger eval code:20
setTimeout handler*unsubscribeTimer debugger eval code:17
debugger eval code:5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment