puppeteer-pro
Version:
A simple puppeteer wrapper to enable useful plugins with ease
136 lines • 5.28 kB
JavaScript
"use strict";
// https://gist.github.com/jeroenvisser101/636030fe66ea929b63a33f5cb3a711ad
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManageLocalStoragePlugin = void 0;
const tslib_1 = require("tslib");
const crypto = tslib_1.__importStar(require("crypto"));
const fs = tslib_1.__importStar(require("fs/promises"));
const __1 = require("..");
const sleep = (time) => { return new Promise(resolve => { setTimeout(resolve, time); }); };
class ManageLocalStoragePlugin extends __1.Plugin {
constructor(opts) {
super();
this.saveLocation = '';
this.mode = '';
this.stringify = (localStorage) => JSON.stringify(localStorage);
this.parse = (localStorage) => JSON.parse(localStorage);
this.disableWarning = false;
this.profile = 'default';
this.allLocalStorage = {};
// Need to find a better typescript way of doing this
this.saveLocation = opts.saveLocation || this.saveLocation;
this.mode = opts.mode || this.mode;
this.stringify = opts.stringify || this.stringify;
this.parse = opts.parse || this.parse;
this.disableWarning = opts.disableWarning || this.disableWarning;
this.profile = opts.profile || this.profile;
if (this.disableWarning !== true) {
console.warn('Warning: Exposing local storage in an unprotected manner can compromise your security. Add the `disableWarning` flag to remove this message.');
}
}
async afterLaunch() {
try {
await fs.access(this.saveLocation);
this.allLocalStorage = this.parse((await fs.readFile(this.saveLocation)).toString() || '{}');
}
catch (_a) {
null;
}
void this.watchLocalStorage();
}
async afterRestart() {
void this.watchLocalStorage();
}
async switchToProfile(profile) {
if (this.isStopped)
return;
this.profile = profile;
await this.loadProfileLocalStorage();
}
async save() {
if (this.isStopped)
return;
if (this.mode !== 'manual')
return;
await this.saveProfileLocalStorage();
}
async load() {
if (this.isStopped)
return;
if (this.mode !== 'manual')
return;
await this.loadProfileLocalStorage();
}
async clear() {
if (this.isStopped)
return;
await this.clearProfileLocalStorage();
}
async watchLocalStorage() {
if (this.isStopped)
return;
if (this.mode !== 'monitor')
return;
const hash = (x) => crypto.createHash('md5').update(x).digest('hex');
let oldProfile = '';
let oldHash = '';
while (!this.isStopped) {
const localStorage = { [this.profile]: await this.getLocalStorage() };
const localStorageString = this.stringify(localStorage);
const newHash = hash(localStorageString);
if (oldProfile !== this.profile) {
oldProfile = this.profile;
}
else if (oldHash !== newHash) {
oldHash = newHash;
await this.saveProfileLocalStorage();
}
else {
await sleep(300);
}
}
}
async saveProfileLocalStorage() {
this.allLocalStorage[this.profile] = await this.getLocalStorage();
const localStorageString = this.stringify(this.allLocalStorage);
await fs.writeFile(this.saveLocation, localStorageString);
}
async loadProfileLocalStorage() {
await this.setLocalStorage(this.allLocalStorage[this.profile] || {});
}
async clearProfileLocalStorage() {
delete this.allLocalStorage[this.profile];
const localStorageString = this.stringify(this.allLocalStorage);
await fs.writeFile(this.saveLocation, localStorageString);
}
async setLocalStorage(allLocalStorage) {
if (!this.browser)
return;
const pages = await this.browser.pages();
const activePages = pages.filter(x => x.url() !== 'about:blank');
for (const page of activePages) {
const origin = await page.evaluate(() => window.origin);
await page.evaluate(originLocalStorage => {
const keys = Object.keys(originLocalStorage);
localStorage.clear();
for (let i = 0; i < Object.keys(localStorage).length; i++) {
localStorage.setItem(keys[i], originLocalStorage[keys[i]]);
}
}, allLocalStorage[origin] || {});
}
}
async getLocalStorage() {
if (!this.browser)
return {};
const allLocalStorage = {};
const pages = await this.browser.pages();
const activePages = pages.filter(x => x.url() !== 'about:blank');
for (const page of activePages) {
const originLocalStorage = await page.evaluate(() => ({ [window.origin]: { ...localStorage } }));
Object.assign(allLocalStorage, originLocalStorage);
}
return allLocalStorage;
}
}
exports.ManageLocalStoragePlugin = ManageLocalStoragePlugin;
//# sourceMappingURL=index.js.map