Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer: save sessionStorage and localStorage

Tags:

Can Puppeteer store values from sessionStorage and localStorage on a disk and use them next time?

like image 497
Kertis van Kertis Avatar asked Sep 08 '17 15:09

Kertis van Kertis


People also ask

How do you save a session in puppeteer?

To keep your connection alive, is as easy as adding keepalive=<MILLISECONDS> to the query string on your connection. Assuming you're self-hosting: const innit = async () => { const browser = await puppeteer. connect({ browserWSEndpoint: 'ws://localhost:3000?keepalive=300000' }); const page = await browser.

Is IndexedDB better than localStorage?

If you want to store structured data on the client side, IndexedDB is the better choice, especially since localStorage isn't built to store sensitive information. But if you're storing a simple, small amount of key-value pair data, use localStorage.

Is sessionStorage shared between tabs?

Right, sessionStorage is not shared across tabs. The way I solved it is by using localStorage events. When a user opens a new tab, we first ask any other tab that is opened if he already have the sessionStorage for us.


1 Answers

I was looking for the same thing. I used node-persist together with Puppeteer as a solution. You can use it like the following.

const storage = require('node-persist');

// Set Local Storage
storage.initSync();
var data = {
    "token": storage.getItemSync("token")
};
page.evaluate((data) => {
    localStorage.setItem("token", data.token);
}, data);

// Get Local Storage
page.evaluate(() => {
    return {
        "token": localStorage.getItem("token")
    };
}).then((data) => {
    storage.initSync();
    storage.setItemSync("token", data.token);
});
like image 147
Ömer Avatar answered Oct 11 '22 13:10

Ömer