Skip to content

Instantly share code, notes, and snippets.

@vijayhardaha
Last active March 12, 2025 02:18
Show Gist options
  • Save vijayhardaha/1528a12d5ac0ae1457ee0fba3a59b49a to your computer and use it in GitHub Desktop.
Save vijayhardaha/1528a12d5ac0ae1457ee0fba3a59b49a to your computer and use it in GitHub Desktop.
Auto Click "Like" Buttons at Intervals

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.

Code:

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
});

How It Works:

  1. Selects all potential "Like" buttons using querySelectorAll().
  2. Filters elements that actually contain the text "Like" (case insensitive).
  3. Finds the closest parent div with aria-label="Like" and role="button".
  4. Clicks each button one by one at 2-second intervals using setTimeout().
  5. Logs each click action for debugging.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment