service auth | Cell 3 | Cell 5 | Search

This code automates the process of downloading Google passwords from the Google Password Manager website by navigating to the site, logging in, extracting password data from each row, and saving it to a file.

Run example

npm run import -- "download passwords from google"

download passwords from google

var importer = require('../Core');
var loginGoogle = importer.import("log in google",
"{client",
"getCredentials}");

function waitForPasswordLoad(r) {
    return Promise.all([
        client.getText(r + ' [role="rowheader"]'),
        client.getText(r + ' [role="rowheader"] ~ [role="gridcell"]:nth-child(2)'),
        client.getValue(r + ' [role="rowheader"] ~ [role="gridcell"]:last-child input')
    ])
        .then(r => r[2] === 'Loading...'
            ? client.pause(1000).then(() => waitForPasswordLoad(r))
            : Promise.resolve(r))
};

function copyPasswordRow(i) {
    const r = '[role="row"]:nth-of-type(' + i + ') ';
    return client.click(r + ' [role="button"][aria-label*="Toggle"]')
        .pause(1000)
        .then(() => waitForPasswordLoad(r))
        .then(r => saveCredentials({
            host: r[0],
            username: r[1],
            password: r[2]
        }))
        .catch(e => console.log(e))
};

function copyPasswords() {
    return client.url('https://passwords.google.com')
        .then(() => loginGoogle(client))
        .then(() => client.pause(1000))
        .then(() => client.elements('[role="row"]'))
        .then(els => importer.runAllPromises(els.map((e, i) => resolve => {
            return client.isExisting('[role="row"]:nth-of-type(' + i + ') [role="button"][aria-label*="Toggle"]')
                .then(is => is ? copyPasswordRow(i) : client)
                .then(row => resolve(row))
        })));
};

var saveCredentials = importer.import("add encrypted passwords.json");
function downloadGooglePasswords() {
    return copyPasswords();
};
module.exports = downloadGooglePasswords;

What the code could have been:

const { Client } = require('webdriverio');
const Core = require('../Core');
const { logInGoogle } = Core.importer('log in google');
const { addEncryptedPasswords } = Core.importer('add encrypted passwords.json');

/**
 * Get the text from all columns in a password row.
 * @param {string} r - The row selector.
 * @returns {Promise<string[]>} The text from each column.
 */
async function waitForPasswordLoad(r) {
    try {
        const [header, username, password] = await Promise.all([
            client.getText(r +'[role="rowheader"]'),
            client.getText(r +'[role="rowheader"] ~ [role="gridcell"]:nth-child(2)'),
            client.getValue(r +'[role="rowheader"] ~ [role="gridcell"]:last-child input')
        ]);

        if (password === 'Loading...') {
            await client.pause(1000);
            return waitForPasswordLoad(r);
        }

        return [header, username, password];
    } catch (error) {
        console.error(`Error loading password row: ${error}`);
        return null;
    }
}

/**
 * Copy the password row and save the credentials.
 * @param {number} i - The row index.
 * @returns {Promise<void>} Resolves when the credentials are saved.
 */
async function copyPasswordRow(i) {
    try {
        const r = `[role="row"]:nth-of-type(${i})`;
        await client.click(r +'[role="button"][aria-label*="Toggle"]');
        await client.pause(1000);

        const [host, username, password] = await waitForPasswordLoad(r);
        await saveCredentials({ host, username, password });
    } catch (error) {
        console.error(`Error copying password row: ${error}`);
    }
}

/**
 * Copy all password rows and save the credentials.
 * @returns {Promise<void>} Resolves when all credentials are saved.
 */
async function copyPasswords() {
    try {
        await client.url('https://passwords.google.com');
        await logInGoogle(client);
        await client.pause(1000);

        const rows = await client.elements('[role="row"]');
        await Promise.all(rows.map(async (row, i) => {
            const r = `[role="row"]:nth-of-type(${i + 1})`;
            if (await client.isExisting(r +'[role="button"][aria-label*="Toggle"]')) {
                return copyPasswordRow(i + 1);
            }
        }));
    } catch (error) {
        console.error(`Error copying passwords: ${error}`);
    }
}

/**
 * Download and save Google passwords.
 * @returns {Promise<void>} Resolves when the credentials are saved.
 */
async function downloadGooglePasswords() {
    try {
        await copyPasswords();
        console.log('Google passwords saved successfully');
    } catch (error) {
        console.error(`Error downloading Google passwords: ${error}`);
    }
}

module.exports = downloadGooglePasswords;

This code automates the process of downloading Google passwords from the Google Password Manager website.

Here's a breakdown:

  1. Imports:

  2. waitForPasswordLoad Function:

  3. copyPasswordRow Function:

  4. copyPasswords Function:

  5. saveCredentials Function:

  6. downloadGooglePasswords Function:

In essence, this code automates the process of extracting Google passwords from the website, handling login, row expansion, data extraction, and saving to a file.