I'm trying to access a pre-signed gmail account, following the guidance provided by this answer from user wolfy. However, I noticed that it logs me out after a while or when multiple instances are opened with the same cookies, requiring me to re-enter the password.
This is how I implemented it:
const getCookies = async (page) => {
// Fetch all cookies
const cookiesArray = await page._client.send('Network.getAllCookies');
// Extract cookies from array
const cookies = await cookiesArray.cookies;
// Save cookies to file
fs.writeFile('./cookies.json', JSON.stringify(cookies, null, 4), (err) => {
if (err) console.log(err);
return;
});
}
const setCookies = async (page) => {
// Retrieve cookies from file
let cookies = JSON.parse(fs.readFileSync('./cookies.json'));
// Set page cookies
await page.setCookie(...cookies);
return
}
Sending cookies:
// Create page once browser loads
let [page] = await browser.pages();
// Enable page request interception
await page.setRequestInterception(true);
// Add event listener for requests
page.on('request', async (req) => {
// If the request URL matches my criteria, execute my function
if (req.url() === 'https://youtube.com/?authuser=0') {
await getCookies(page);
await browser.close();
}
// Otherwise, continue normal functionality
req.continue();
});
// Navigate to my desired URL once all listeners are set up
await page.goto('https://accounts.google.com/AccountChooser?service=wise&continue=https://youtube.com')