puppeteer-pro
Version:
A simple puppeteer wrapper to enable useful plugins with ease
144 lines • 5.13 kB
JavaScript
"use strict";
// https://gist.github.com/jeroenvisser101/636030fe66ea929b63a33f5cb3a711ad
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManageCookiesPlugin = void 0;
const crypto = require("crypto");
const fs = require("fs/promises");
const __1 = require("../..");
const sleep = (time) => { return new Promise(resolve => { setTimeout(resolve, time); }); };
class ManageCookiesPlugin extends __1.Plugin {
constructor(opts) {
super();
this.saveLocation = '';
this.mode = '';
this.stringify = (cookies) => JSON.stringify(cookies);
this.parse = (cookies) => JSON.parse(cookies);
this.disableWarning = false;
this.profile = 'default';
this.allCookies = {};
// 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 cookies 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.allCookies = this.parse((await fs.readFile(this.saveLocation)).toString() || '{}');
}
catch (_a) {
null;
}
void this.watchCookies();
}
async afterRestart() {
void this.watchCookies();
}
async switchToProfile(profile) {
if (this.isStopped)
return;
this.profile = profile;
await this.loadProfileCookies();
}
async save() {
if (this.isStopped)
return;
if (this.mode !== 'manual')
return;
await this.saveProfileCookies();
}
async load() {
if (this.isStopped)
return;
if (this.mode !== 'manual')
return;
await this.loadProfileCookies();
}
async clear() {
if (this.isStopped)
return;
await this.clearProfileCookies();
}
async watchCookies() {
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 cookies = { [this.profile]: await this.getCookies() };
const cookiesString = this.stringify(cookies);
const newHash = hash(cookiesString);
if (oldProfile !== this.profile) {
oldProfile = this.profile;
}
else if (oldHash !== newHash) {
oldHash = newHash;
await this.saveProfileCookies();
}
else {
await sleep(300);
}
}
}
async saveProfileCookies() {
this.allCookies[this.profile] = await this.getCookies();
const cookiesString = this.stringify(this.allCookies);
await fs.writeFile(this.saveLocation, cookiesString);
}
async loadProfileCookies() {
var _a, _b;
const page = await this.getFirstPage();
if (!page)
return;
const requiresRealPage = page.url() === 'about:blank';
if (requiresRealPage) {
await page.goto('http://www.google.com');
}
await ((_a = this.browser) === null || _a === void 0 ? void 0 : _a.deleteCookie(...await this.getCookies()));
await ((_b = this.browser) === null || _b === void 0 ? void 0 : _b.setCookie(...this.allCookies[this.profile] || []));
if (requiresRealPage) {
await page.goBack();
}
}
async clearProfileCookies() {
var _a;
const page = await this.getFirstPage();
if (!page)
return;
const requiresRealPage = page.url() === 'about:blank';
if (requiresRealPage) {
await page.goto('http://www.google.com');
}
await ((_a = this.browser) === null || _a === void 0 ? void 0 : _a.deleteCookie(...this.allCookies[this.profile] || []));
delete this.allCookies[this.profile];
const cookiesString = this.stringify(this.allCookies);
await fs.writeFile(this.saveLocation, cookiesString);
if (requiresRealPage) {
await page.goBack();
}
}
async getCookies() {
var _a;
const page = await this.getFirstPage();
if (!page)
return [];
try {
const cookies = await ((_a = this.browser) === null || _a === void 0 ? void 0 : _a.cookies()) || [];
return cookies;
}
catch (_b) {
return [];
}
}
}
exports.ManageCookiesPlugin = ManageCookiesPlugin;
//# sourceMappingURL=index.js.map