2021-01-08 21:14:01 +00:00
|
|
|
import { getEnvAwareBrowserInstance } from './smartpuppeteer.classes.smartpuppeteer';
|
|
|
|
import * as plugins from './smartpuppeteer.plugins';
|
|
|
|
|
|
|
|
export class IncognitoBrowser {
|
|
|
|
public status: 'started' | 'stopped' = 'stopped';
|
|
|
|
public browser: plugins.puppeteer.Browser;
|
|
|
|
|
2021-01-08 21:16:25 +00:00
|
|
|
constructor() {}
|
2021-01-08 21:14:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* starts the IncognitoBrowser
|
|
|
|
*/
|
|
|
|
public async start() {
|
|
|
|
this.status = 'started';
|
|
|
|
this.browser = await getEnvAwareBrowserInstance();
|
2021-08-17 00:53:34 +00:00
|
|
|
this.browser.on('disconnected', async (eventArg) => {
|
2021-01-08 21:14:01 +00:00
|
|
|
try {
|
|
|
|
this.browser.removeAllListeners();
|
2021-01-08 21:16:25 +00:00
|
|
|
} catch (err) {}
|
2021-01-08 21:14:01 +00:00
|
|
|
if (this.status === 'started') {
|
|
|
|
this.browser = await getEnvAwareBrowserInstance();
|
|
|
|
}
|
2021-01-08 21:16:25 +00:00
|
|
|
});
|
2021-01-08 21:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stops the IncognitoBrowser
|
|
|
|
*/
|
|
|
|
public async stop() {
|
|
|
|
this.status = 'stopped';
|
2021-01-08 21:53:17 +00:00
|
|
|
await this.browser.close();
|
2021-01-08 21:14:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:24:21 +00:00
|
|
|
/**
|
|
|
|
* rotate
|
|
|
|
*/
|
|
|
|
public async rotateBrowser () {
|
|
|
|
this.browser.close().catch();
|
|
|
|
this.browser = await getEnvAwareBrowserInstance();
|
|
|
|
}
|
|
|
|
|
2021-01-08 21:14:01 +00:00
|
|
|
public async getNewIncognitoContext(): Promise<plugins.puppeteer.BrowserContext> {
|
|
|
|
if (this.browser) {
|
|
|
|
return this.browser.createIncognitoBrowserContext();
|
|
|
|
} else {
|
|
|
|
throw new Error('you need to start the IncognitoBrowser instance first');
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 21:16:25 +00:00
|
|
|
}
|