facebook data | Cell 8 | Cell 10 | Search

This code automates the process of unfollowing users on Facebook by identifying and clicking unfollow buttons on the user's "Following" page. It uses a custom module and promises for efficient and concurrent unfollowing.

Run example

npm run import -- "Unfollow everyone on facebook"

Unfollow everyone on facebook

var importer = require('../Core');
function unfollowFacebook() {
    return client
        .getUrl()
        .url('https://www.facebook.com/me/following')
        .pause(3000)
        .elements('//a[contains(@ajaxify, "unfollow_profile")]')
        .then(els => {
            return importer.runAllPromises(els.value.map(el => resolve => {
                return client.elementIdClick(el.ELEMENT)
                    .then(r => resolve())
                    .catch(e => resolve(e));
            }))
        })
};
module.exports = unfollowFacebook;

What the code could have been:

const { client, getUrl } = require('../Core');

/**
 * Unfollows all friends on Facebook.
 * @returns {Promise} A promise resolving to the result of unfollowing all friends.
 */
function unfollowFacebook() {
    // Get the Facebook following page URL
    const url = getUrl().url('https://www_facebook_dotcom/me/following'); // Using template literals for readability

    // Wait for 3 seconds for the page to load
    return url.pause(3000).elements('//a[contains(@ajaxify, "unfollow_profile")]')
       .then(els => {
            // Map over the elements, clicking each one to unfollow
            const unfollowPromises = els.value.map(el => () => {
                // Click the unfollow button and resolve the promise with the result
                return client.elementIdClick(el.ELEMENT).then(() => {})
                   .catch(e => e); // Log the error if the click fails
            });

            // Use Promise.all to wait for all unfollow clicks to complete
            return Promise.all(unfollowPromises.map(p => importer.runPromise(p))).catch(e => e); // Run each promise and catch any errors
        });
}

module.exports = unfollowFacebook;

This code snippet is designed to unfollow users on Facebook.

Here's a breakdown:

  1. Setup: It imports a custom importer module, likely containing utility functions.

  2. unfollowFacebook Function:

  3. Export: The unfollowFacebook function is exported, making it callable from other parts of the application.