2022-03-24 12:51:12 +00:00
|
|
|
import { getEnvAwareBrowserInstance } from './smartpuppeteer.classes.smartpuppeteer.js';
|
|
|
|
import * as plugins from './smartpuppeteer.plugins.js';
|
2021-01-08 21:14:01 +00:00
|
|
|
|
|
|
|
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-08-17 15:51:50 +00:00
|
|
|
plugins.treeKill(this.browser.process().pid, 'SIGKILL');
|
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
|
|
|
|
*/
|
2021-11-07 14:58:45 +00:00
|
|
|
public async rotateBrowser() {
|
2021-01-12 14:24:21 +00:00
|
|
|
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
|
|
|
}
|