3

Is it possible to get all the values from localStorage using Puppeteer? including values from third-party domains (with the assumption that I don't know all the third-party domains).

I am looking for something similar with this, which gets all the cookies from the browser (but for localStorage).

export const getCookies = async page => {
  const { cookies } = await page._client.send("Network.getAllCookies", {});

  return cookies;
};
Vitalie Maldur
  • 553
  • 3
  • 19
  • It seems neither [DOMStorage API](https://chromedevtools.github.io/devtools-protocol/tot/DOMStorage) nor [Storage API](https://chromedevtools.github.io/devtools-protocol/tot/Storage) provide a way to get the list of all origins for DOMStorage entries. You can [get the entries](https://chromedevtools.github.io/devtools-protocol/tot/DOMStorage#method-getDOMStorageItems) only if you know the origin. Maybe it is worth to ask or request the feature in the [devtools-protocol repository](https://github.com/ChromeDevTools/devtools-protocol)? – vsemozhebuty Jan 24 '19 at 19:02

1 Answers1

3

However, if we suppose that localStorage origins = frames, we can get the data by any of these two ways:

'use strict';

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch({ headless: false });
    const [page] = await browser.pages();

    await page.goto('https://example.org/');


    await page.evaluate(() => {
      document.body.appendChild(document.createElement('iframe')).src = 'https://example.net/';
      document.body.appendChild(document.createElement('iframe')).src = 'https://example.com/';
    });

    for (const frame of page.frames()) {
      await frame.waitForSelector('head > title');
      await frame.evaluate(() => {
        localStorage.setItem('foo', document.location.href);
        localStorage.setItem('bar', document.title);
      });
    }

    const client = await page.target().createCDPSession();
    for (const frame of page.frames()) {
      const securityOrigin = new URL(frame.url()).origin;
      const response = await client.send(
        'DOMStorage.getDOMStorageItems',
        { storageId: { isLocalStorage: true, securityOrigin } },
      );
      console.log(response.entries);
    }

    console.log('----------');

    for (const frame of page.frames()) {
      const entries = await frame.evaluate(() => {
        const data = [];
        for (var i = 0; i < localStorage.length; i++) {
          const key = localStorage.key(i);
          data[i] = [key, localStorage.getItem(key)];
        }
        return data;
      });
      console.log(entries);
    }

    await browser.close();
  } catch (err) {
    console.error(err);
  }
})();
[ [ 'foo', 'https://example.org/' ],
  [ 'bar', 'Example Domain' ] ]
[ [ 'foo', 'https://example.net/' ],
  [ 'bar', 'Example Domain' ] ]
[ [ 'foo', 'https://example.com/' ],
  [ 'bar', 'Example Domain' ] ]
----------
[ [ 'foo', 'https://example.org/' ],
  [ 'bar', 'Example Domain' ] ]
[ [ 'foo', 'https://example.net/' ],
  [ 'bar', 'Example Domain' ] ]
[ [ 'foo', 'https://example.com/' ],
  [ 'bar', 'Example Domain' ] ]
vsemozhebuty
  • 12,992
  • 1
  • 26
  • 26