Skip to content

Instantly share code, notes, and snippets.

@Horopter
Created March 15, 2024 00:33
Show Gist options
  • Save Horopter/76a2e785de3a933d97e0f2bb343a6ecc to your computer and use it in GitHub Desktop.
Save Horopter/76a2e785de3a933d97e0f2bb343a6ecc to your computer and use it in GitHub Desktop.
Youtube Comments JS Files
// Global constants for XPaths
const COMMON_XPATH = '/html/body/c-wiz/div/div[2]/c-wiz/c-wiz/div/div';
const END_MESSAGE_XPATH = `${COMMON_XPATH}[2]/div[1]/div`;
const COMMENT_XPATH_SUFFIX = 'div/div/div[2]/div[1]/div[1]';
const LINK_XPATH_SUFFIX = 'div/div/div[2]/div[1]/div[2]/a';
// Array to store collected comments and links
let collectedData = [];
function autoScroll(counter) {
window.scrollTo(0, document.body.scrollHeight);
setTimeout(function() {
counter -= 20; // Adjust to ensure no comments are skipped while scrolling
collectComments(counter);
}, 1000);
}
function collectComments(counter) {
try {
var endMessage = document.evaluate(END_MESSAGE_XPATH, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (endMessage && endMessage.textContent.includes("Looks like you've reached the end")) {
console.log("Collection complete");
console.log(JSON.stringify(collectedData));
return;
}
for (let i = counter; i < counter + 20; i++) {
let commentXPath = `${COMMON_XPATH}[1]/c-wiz[${i}]/${COMMENT_XPATH_SUFFIX}`;
let linkXPath = `${COMMON_XPATH}[1]/c-wiz[${i}]/${LINK_XPATH_SUFFIX}`;
let commentElement = document.evaluate(commentXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
let linkElement = document.evaluate(linkXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (commentElement && linkElement) {
let comment = commentElement.textContent;
let link = linkElement.getAttribute('href');
collectedData.push({ comment, link });
}
}
setTimeout(function() {
collectComments(counter + 20);
}, 2000); // Wait for two seconds before processing the next batch of comments
} catch (e) {
console.error('An error occurred:', e);
}
}
collectComments(1);
// Global constants for XPaths
const COMMON_XPATH = '/html/body/c-wiz/div/div[2]/c-wiz/c-wiz/div/div';
const END_MESSAGE_XPATH = `${COMMON_XPATH}[2]/div[1]/div`;
// Function to format the delete button XPath with the counter value
function getDeleteButtonXPath(counter) {
return `${COMMON_XPATH}[1]/c-wiz[${counter}]/div/div/div[1]/div[2]/div/button`;
}
function autoScroll(counter) {
window.scrollTo(0, document.body.scrollHeight);
setTimeout(function() {
counter -= 20; // Adjust to ensure no comments are skipped while scrolling
deleteComment(counter);
}, 1000);
}
function deleteComment(counter) {
try {
var endMessage = document.evaluate(END_MESSAGE_XPATH, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (endMessage && endMessage.textContent.includes("Looks like you've reached the end")) {
console.log("Wait for a couple of hours until the grey notifications disappeear");
return;
}
console.log('Deleting comment: ' + counter);
var deleteButtonXPath = getDeleteButtonXPath(counter);
var deleteButton = document.evaluate(deleteButtonXPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (deleteButton) {
deleteButton.click();
setTimeout(function() {
deleteComment(++counter);
}, 2000); // Wait for two seconds before deleting the next comment. Increase according to your convenience.
} else {
autoScroll(counter);
}
} catch (e) {
console.error('An error occurred:', e);
}
}
deleteComment(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment