Skip to content

Instantly share code, notes, and snippets.

@GrzegorzManiak
Created October 13, 2023 14:49
Show Gist options
  • Save GrzegorzManiak/19976c68d3ba590e48b59e6fa2b388db to your computer and use it in GitHub Desktop.
Save GrzegorzManiak/19976c68d3ba590e48b59e6fa2b388db to your computer and use it in GitHub Desktop.
Delete all your youtube comments automatically
//
// ENSURE that you are on your own page
// https://myactivity.google.com/page?hl=en&pli=1&page=youtube_comments
//
// Wait for the page to fully load, and than copy / paste this script
// into the console.
//
// There are two variables that you can change, SLEEP_FOR and CLICK_DELAY
// --> SLEEP_FOR: Min,Max timeout before scrolling down
// --> CLICK_DELAY: Min,Max timeout between clicking the element
// --> COMMENTS_TIMEOUT: Ammount of times 0 comments can be found before we stop
//
(async() => {
const SLEEP_FOR = [1000, 2000];
const CLICK_DELAY = [350, 1000];
const COMMENTS_TIMEOUT = 3; // -- Ammount of times 0 comments can be found before we stop
// -- Map to store the comments that we've already deleted
const comments = new Map();
let zero_comments_count = 0;
// -- Function used to return all the coments that you've made
// without returning duplicates
const get_comments = () => {
// -- Get all the comments on the page
const new_comments = Array.from(document.querySelectorAll('[data-show-delete-individual="true"'));
// -- Ensure that we arent returning duplicates
const unique = new_comments.filter((item) => !comments.has(item));
// -- If we have 0 comments, increment the zero comments count
if (unique.length === 0) zero_comments_count++;
else zero_comments_count = 0;
// -- Add the reply content to the map
unique.forEach((item) => comments.set(item, false));
// -- Return the unique reply content
return unique;
};
// -- Simple sleep function
const sleep = (min_max) => new Promise((resolve) => {
const min = min_max[0], max = min_max[1];
const time = Math.floor(Math.random() * (max - min + 1)) + min;
setTimeout(resolve, time);
});
// -- Progresses down the page and deletes all the content
let deleted = 0;
const step = async() => {
// -- Get comments
const new_comments = get_comments();
// -- Delete all the content
for (const item of new_comments) {
// -- Click the x button: button
const delete_btn = item.querySelector('button');
if (!delete_btn) continue;
// -- Click the more button
delete_btn.click();
deleted++;
await sleep(CLICK_DELAY);
}
// -- Scrolls down the page
window.scrollTo(0, document.body.scrollHeight);
}
// -- Run the step function every 2 seconds
while (true) {
await step();
await sleep(SLEEP_FOR);
// -- If there are no more comments left, exit the loop
if (zero_comments_count === COMMENTS_TIMEOUT) break;
}
console.log(`Delted ${deleted} comments`);
})();
@GrzegorzManiak
Copy link
Author

Note: This also works with Community Posts and Live chats, make sure you dont close out of the page till you see the 'No activity.' graphic.
image

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