This JavaScript snippet selects all "Like" buttons on a webpage that meet specific attribute criteria and clicks them at 2-seconds intervals. It ensures that only buttons with the visible text "Like"
are clicked.
var elements = document.querySelectorAll('div[aria-label="Like"][role="button"] span[data-ad-rendering-role="like_button"]');
var likeButtons = [];
elements.forEach(function(el) {
if (el.innerText.toLowerCase().trim() === 'like') {
var parent = el.closest('div[aria-label="Like"][role="button"]');
if (parent) {
likeButtons.push(parent);
}
}
});
likeButtons.forEach(function(button, index) {
setTimeout(function() {
button.click();
console.log('Clicked element ' + (index + 1));
// Show an alert when all posts are clicked
if (index === likeButtons.length - 1) {
setTimeout(function() {
alert('All posts have been liked!');
}, 1000); // Slight delay after the last click for better UX
}
}, index * 2000); // Delay each click by 2 seconds
});
- Selects all potential "Like" buttons using
querySelectorAll()
. - Filters elements that actually contain the text
"Like"
(case insensitive). - Finds the closest parent div with
aria-label="Like"
androle="button"
. - Clicks each button one by one at 2-second intervals using
setTimeout()
. - Logs each click action for debugging.