使用 Puppeteer 从 localStorage 获取所有值
Get all values from localStorage using Puppeteer
是否可以使用 Puppeteer 从 localStorage
获取所有值?包括来自第三方域的值(假设我不知道所有第三方域)。
我正在寻找与此类似的东西,它从浏览器获取所有 cookie(但 localStorage
)。
export const getCookies = async page => {
const { cookies } = await page._client.send("Network.getAllCookies", {});
return cookies;
};
但是,如果我们假设localStorage origins = frames,我们可以通过以下两种方式之一获取数据:
'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' ] ]
是否可以使用 Puppeteer 从 localStorage
获取所有值?包括来自第三方域的值(假设我不知道所有第三方域)。
我正在寻找与此类似的东西,它从浏览器获取所有 cookie(但 localStorage
)。
export const getCookies = async page => {
const { cookies } = await page._client.send("Network.getAllCookies", {});
return cookies;
};
但是,如果我们假设localStorage origins = frames,我们可以通过以下两种方式之一获取数据:
'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' ] ]