facebook data | Cell 10 | | Search

The code defines three functions: getUninvited, clickInvite, and clickConnect, which together automate the process of inviting individuals to a fundraiser event by identifying uninvited participants and simulating clicks on them.

Cell 11


function getUninvited() {
    const invites = getAllXPathBrowser('//*[contains(@action, "fundraiser")]//a[contains(., "Invite")]');
    const already = getAllXPathBrowser('//a[contains(@tabindex, "-1")]');
    return invites.filter(b => !already.includes(b));
}
function clickInvite() {
    const invites = getUninvited();
    invites.forEach((b, i) => setTimeout(() => b.click(), i * 50));
    getAllXPathBrowser('//*[contains(@data-testid, "fundraiser_invite_dialog_friend_list")]//*[contains(@class, "uiScrollableAreaWrap")]').scrollTop = 100000000000
    return new Promise(resolve => setTimeout(() => {
        const next = getUninvited();
        if(next.length > 0)
            clickInvite().then(r => resolve(r));
    }, 5000));
}
function clickConnect() {
    const connect = getAllXPathBrowser('//button[contains(., "Connect")]');
    if(connect) {
        connect[0].click();
    }
    return new Promise(resolve => setTimeout(() => {
        clickConnect().then(r => resolve(r));
    }, 1000));
}
module.exports = clickInvite;

What the code could have been:

// Constants for inviting thresholds and timeouts
const INVITING_THRESHOLD = 500; // in milliseconds
const TIMEOUT_BETWEEN_INVITES = 50; // in milliseconds
const TIMEOUT_BETWEEN_CHECKS = 5000; // in milliseconds
const TIMEOUT_CONNECT = 1000; // in milliseconds

// Function to get all uninvited users
function getUninvited() {
    /**
     * Retrieves all uninvited users by finding elements with 'fundraiser' action and 'Invite' text,
     * and then filtering out the already invited users.
     *
     * @returns {Array} An array of uninvited user elements.
     */
    const invites = getAllXPathBrowser('//*[contains(@action, "fundraiser")]//a[contains(., "Invite")]');
    const already = getAllXPathBrowser('//a[contains(@tabindex, "-1")]');
    return invites.filter(b =>!already.includes(b));
}

// Function to click on all uninvited users sequentially
function clickUninvited() {
    /**
     * Sends a click event to all uninvited users in sequence, with a short delay between each click.
     *
     * @returns {Promise} A promise that resolves when all clicks are executed.
     */
    return new Promise(resolve => {
        const invites = getUninvited();
        const clicks = [];
        invites.forEach((b, i) => {
            const timeout = i * TIMEOUT_BETWEEN_INVITES;
            clicks.push(() => b.click());
            setTimeout(() => {
                if (invites.length - 1 === i) {
                    // Scroll to the bottom of the list
                    const friendList = getAllXPathBrowser('//*[contains(@data-testid, "fundraiser_invite_dialog_friend_list")]//*[contains(@class, "uiScrollableAreaWrap")]');
                    friendList.scrollTop = 100000000000;
                    resolve(clicks.reduce((promise, click) => promise.then(() => click()), Promise.resolve()));
                }
            }, timeout);
        });
    });
}

// Function to click on the connect button
function clickConnect() {
    /**
     * Finds and clicks on the 'Connect' button, with a short timeout to account for any potential delays.
     *
     * @returns {Promise} A promise that resolves when the connect button is clicked.
     */
    const connect = getAllXPathBrowser('//button[contains(., "Connect")]');
    if (connect) {
        connect[0].click();
        return new Promise(resolve => setTimeout(resolve, TIMEOUT_CONNECT));
    }
    return Promise.resolve();
}

// Function to check if there are any uninvited users left to invite
function hasUninvited() {
    /**
     * Checks if there are any uninvited users left by calling getUninvited and checking if the result is not empty.
     *
     * @returns {boolean} A boolean indicating whether there are any uninvited users left.
     */
    return getUninvited().length > 0;
}

// Main function to invite all uninvited users
function inviteAll() {
    /**
     * Continuously invites all uninvited users until there are no more users left to invite.
     *
     * @returns {Promise} A promise that resolves when all invitations are completed.
     */
    return new Promise((resolve, reject) => {
        let timeoutId;
        function checkAndInvite() {
            clickUninvited().then(() => {
                if (hasUninvited()) {
                    timeoutId = setTimeout(checkAndInvite, TIMEOUT_BETWEEN_CHECKS);
                } else {
                    clearTimeout(timeoutId);
                    resolve();
                }
            }).catch(reject);
        }
        checkAndInvite();
    });
}

module.exports = inviteAll;

Function Breakdown

getUninvited()

clickInvite()

clickConnect()

Exported Function

Note: These functions appear to be used in a web scraping or automation context, potentially for social media or event planning platforms. The getAllXPathBrowser function is not defined in this code snippet, but it is likely used to retrieve XPath elements from a web page using a browser.