Skip to content

Instantly share code, notes, and snippets.

@fokklz
Last active June 5, 2024 18:16
Show Gist options
  • Save fokklz/bf3e7d33971e9134136554528dc73d8e to your computer and use it in GitHub Desktop.
Save fokklz/bf3e7d33971e9134136554528dc73d8e to your computer and use it in GitHub Desktop.
remove comments the creator did not respond to YouTube
// Scroll to the bottom of the page (loading all comments)
// tip: press your mouse wheel down and move your mouse down
// Open the console and run:
// ###########################
// STEP: 1
document.querySelectorAll('ytd-comment-replies-renderer:not(:has(yt-img-shadow)), ytd-comment-thread-renderer:not(:has(ytd-comment-replies-renderer))').forEach((e) => {
let target = e;
if ( e.tagName.toLowerCase() == "ytd-comment-replies-renderer" ){
target = e.parentElement.parentElement;
}
target.remove();
})
// ###########################
// STEP: 2
// Open all Replies
// we use a delay to ensure its not triggering rate-limits
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function clickWithDelay() {
const elements = document.querySelectorAll('#more-replies');
for (let i = 0; i < elements.length; i++) {
elements[i].scrollIntoView({ behavior: 'smooth', block: 'center' });
elements[i].click();
await delay(4000);
}
}
clickWithDelay();
// ###########################
// STEP: 3 (Optional)
// Remove all replies that are not the creator
document.querySelectorAll('#header-author:not(:has(.ytd-author-comment-badge-renderer))').forEach((e) => {
let target = e.parentElement.parentElement.parentElement.parentElement;
if(target.hasAttribute('is-reply')){
target.remove();
}
});
// ###########################
// STEP: 4
// Extract the comments as JSON (will popup a download)
let comments = [];
document.querySelectorAll('ytd-comment-thread-renderer').forEach((e) => {
let author = e.querySelector('#author-text > span').textContent.trim();
let comment = e.querySelector('.yt-core-attributed-string').textContent.trim();
let replies = [];
Array.from(e.querySelectorAll('ytd-comment-view-model')).slice(1).forEach((e1) => {
let replyAuthor = e1.querySelector('#author-comment-badge yt-formatted-string, #header-author .ytd-comment-view-model').textContent.trim();
let replyContent = e1.querySelector('.yt-core-attributed-string').textContent.trim();
replyAuthor = replyAuthor.replace(/[\n\s]+/g, ' ').trim();
replyContent = replyContent.replace(/[\n\s]+/g, ' ').trim();
replies.push({
author: replyAuthor,
content: replyContent
});
});
author = author.replace(/[\n\s]+/g, ' ').trim();
comment = comment.replace(/[\n\s]+/g, ' ').trim();
comments.push({
author: author,
comment: comment,
replies: replies
});
let file = new Blob([JSON.stringify(comments, null, 2)], {type: 'application/json'});
let a = document.createElement('a');
let url = URL.createObjectURL(file);
a.href = url;
a.download = 'comments-with-replies.json';
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment