@push.rocks/smartpuppeteer 2.0.2 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ import puppeteer from 'puppeteer';
2
+ export declare const getEnvAwareBrowserInstance: () => Promise<puppeteer.Browser>;
3
+ export { puppeteer };
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const puppeteer_1 = __importDefault(require("puppeteer"));
7
+ exports.puppeteer = puppeteer_1.default;
8
+ exports.getEnvAwareBrowserInstance = async () => {
9
+ let chromeArgs = [];
10
+ if (process.env.CI) {
11
+ chromeArgs = chromeArgs.concat(['--no-sandbox', '--disable-setuid-sandbox']);
12
+ }
13
+ const headlessBrowser = await puppeteer_1.default.launch({
14
+ args: chromeArgs
15
+ });
16
+ return headlessBrowser;
17
+ };
18
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLDBEQUFrQztBQWF6QixvQkFiRixtQkFBUyxDQWFFO0FBWEwsUUFBQSwwQkFBMEIsR0FBRyxLQUFLLElBQWdDLEVBQUU7SUFDL0UsSUFBSSxVQUFVLEdBQWEsRUFBRSxDQUFDO0lBQzlCLElBQUksT0FBTyxDQUFDLEdBQUcsQ0FBQyxFQUFFLEVBQUU7UUFDbEIsVUFBVSxHQUFHLFVBQVUsQ0FBQyxNQUFNLENBQUMsQ0FBQyxjQUFjLEVBQUUsMEJBQTBCLENBQUMsQ0FBQyxDQUFDO0tBQzlFO0lBQ0QsTUFBTSxlQUFlLEdBQUcsTUFBTSxtQkFBUyxDQUFDLE1BQU0sQ0FBQztRQUM3QyxJQUFJLEVBQUUsVUFBVTtLQUNqQixDQUFDLENBQUM7SUFDSCxPQUFPLGVBQWUsQ0FBQztBQUN6QixDQUFDLENBQUMifQ==
@@ -1,5 +1,5 @@
1
1
  /**
2
- * autocreated commitinfo by @pushrocks/commitinfo
2
+ * autocreated commitinfo by @push.rocks/commitinfo
3
3
  */
4
4
  export declare const commitinfo: {
5
5
  name: string;
@@ -1,9 +1,9 @@
1
1
  /**
2
- * autocreated commitinfo by @pushrocks/commitinfo
2
+ * autocreated commitinfo by @push.rocks/commitinfo
3
3
  */
4
4
  export const commitinfo = {
5
- name: '@pushrocks/smartpuppeteer',
6
- version: '2.0.2',
7
- description: 'simplified access to puppeteer'
5
+ name: '@push.rocks/smartpuppeteer',
6
+ version: '2.0.4',
7
+ description: 'Provides simplified access to Puppeteer for automation and testing purposes.'
8
8
  };
9
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSwyQkFBMkI7SUFDakMsT0FBTyxFQUFFLE9BQU87SUFDaEIsV0FBVyxFQUFFLGdDQUFnQztDQUM5QyxDQUFBIn0=
9
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSw0QkFBNEI7SUFDbEMsT0FBTyxFQUFFLE9BQU87SUFDaEIsV0FBVyxFQUFFLDhFQUE4RTtDQUM1RixDQUFBIn0=
@@ -4,16 +4,25 @@ export declare class IncognitoBrowser {
4
4
  browser: plugins.puppeteer.Browser;
5
5
  constructor();
6
6
  /**
7
- * starts the IncognitoBrowser
7
+ * Starts the IncognitoBrowser instance.
8
+ * It launches the browser using environment-aware options and sets up a listener
9
+ * to automatically re-launch if the browser disconnects while the status is 'started'.
8
10
  */
9
11
  start(): Promise<void>;
10
12
  /**
11
- * stops the IncognitoBrowser
13
+ * Stops the IncognitoBrowser instance.
14
+ * It forcefully kills the browser process (if needed) and then closes the browser.
12
15
  */
13
16
  stop(): Promise<void>;
14
17
  /**
15
- * rotate
18
+ * Rotates the browser instance.
19
+ * It closes the current browser and launches a new one.
16
20
  */
17
21
  rotateBrowser(): Promise<void>;
22
+ /**
23
+ * Returns a new incognito browser context.
24
+ * This uses Puppeteer's createIncognitoBrowserContext() API, which is the
25
+ * correct method for creating isolated sessions.
26
+ */
18
27
  getNewIncognitoContext(): Promise<plugins.puppeteer.BrowserContext>;
19
28
  }
@@ -5,43 +5,61 @@ export class IncognitoBrowser {
5
5
  this.status = 'stopped';
6
6
  }
7
7
  /**
8
- * starts the IncognitoBrowser
8
+ * Starts the IncognitoBrowser instance.
9
+ * It launches the browser using environment-aware options and sets up a listener
10
+ * to automatically re-launch if the browser disconnects while the status is 'started'.
9
11
  */
10
12
  async start() {
11
13
  this.status = 'started';
12
14
  this.browser = await getEnvAwareBrowserInstance();
13
- this.browser.on('disconnected', async (eventArg) => {
15
+ this.browser.on('disconnected', async () => {
14
16
  try {
15
17
  this.browser.removeAllListeners();
16
18
  }
17
- catch (err) { }
19
+ catch (err) {
20
+ // Optionally handle the error.
21
+ }
18
22
  if (this.status === 'started') {
19
23
  this.browser = await getEnvAwareBrowserInstance();
20
24
  }
21
25
  });
22
26
  }
23
27
  /**
24
- * stops the IncognitoBrowser
28
+ * Stops the IncognitoBrowser instance.
29
+ * It forcefully kills the browser process (if needed) and then closes the browser.
25
30
  */
26
31
  async stop() {
27
32
  this.status = 'stopped';
28
- plugins.treeKill(this.browser.process()?.pid, 'SIGKILL');
33
+ const pid = this.browser.process()?.pid;
34
+ if (pid) {
35
+ plugins.treeKill(pid, 'SIGKILL');
36
+ }
29
37
  await this.browser.close();
30
38
  }
31
39
  /**
32
- * rotate
40
+ * Rotates the browser instance.
41
+ * It closes the current browser and launches a new one.
33
42
  */
34
43
  async rotateBrowser() {
35
- this.browser.close().catch();
44
+ try {
45
+ await this.browser.close();
46
+ }
47
+ catch (err) {
48
+ // Ignore errors if the browser is already closed.
49
+ }
36
50
  this.browser = await getEnvAwareBrowserInstance();
37
51
  }
52
+ /**
53
+ * Returns a new incognito browser context.
54
+ * This uses Puppeteer's createIncognitoBrowserContext() API, which is the
55
+ * correct method for creating isolated sessions.
56
+ */
38
57
  async getNewIncognitoContext() {
39
- if (this.browser) {
40
- return this.browser.createIncognitoBrowserContext();
41
- }
42
- else {
43
- throw new Error('you need to start the IncognitoBrowser instance first');
58
+ if (!this.browser) {
59
+ throw new Error('You need to start the IncognitoBrowser instance first');
44
60
  }
61
+ // @ts-ignore
62
+ return this.browser.createIncognitoBrowserContext();
45
63
  }
46
64
  }
47
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRwdXBwZXRlZXIuY2xhc3Nlcy5pbmNvZ25pdG9icm93c2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRwdXBwZXRlZXIuY2xhc3Nlcy5pbmNvZ25pdG9icm93c2VyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSwwQkFBMEIsRUFBRSxNQUFNLDRDQUE0QyxDQUFDO0FBQ3hGLE9B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UFPLEVBQUUsQ0FBQztZQUNsQixNQUFNLElBQUksS0FBSyxDQUFDLHVEQUF1RCxDQUFDLENBQUM7UUFDM0UsQ0FBQztRQUNELGFBQWE7UUFDYixPQUFPLElBQUksQ0FBQyxPQUFPLENBQUMsNkJBQTZCLEVBQUUsQ0FBQztJQUN0RCxDQUFDO0NBQ0YifQ==
@@ -1,31 +1,37 @@
1
1
  import * as plugins from './smartpuppeteer.plugins.js';
2
2
  export const getEnvAwareBrowserInstance = async (optionsArg = {}) => {
3
3
  const options = {
4
- ...{
5
- forceNoSandbox: false,
6
- },
4
+ forceNoSandbox: false,
7
5
  ...optionsArg,
8
6
  };
9
7
  let chromeArgs = [];
10
- if (process.env.CI || options.forceNoSandbox || plugins.os.userInfo().username === 'root') {
8
+ if (process.env.CI ||
9
+ options.forceNoSandbox ||
10
+ plugins.os.userInfo().username === 'root') {
11
11
  chromeArgs = chromeArgs.concat(['--no-sandbox', '--disable-setuid-sandbox']);
12
+ console.warn('********************************************************');
13
+ console.warn('WARNING: Launching browser without sandbox. This can be insecure!');
14
+ console.warn('********************************************************');
12
15
  }
13
- let headlessBrowser;
14
- console.log('launching puppeteer bundled chrome with arguments:');
16
+ // Automatically choose an executable if available: prefer google-chrome, then chromium, then chromium-browser.
17
+ const execPath = plugins.smartshell.which.sync('google-chrome') ||
18
+ plugins.smartshell.which.sync('chromium') ||
19
+ plugins.smartshell.which.sync('chromium-browser');
20
+ const executablePathOptions = execPath ? { executablePath: execPath } : {};
21
+ console.log('Launching puppeteer browser with arguments:');
15
22
  console.log(chromeArgs);
16
- headlessBrowser = await plugins.puppeteer.launch({
23
+ if (execPath) {
24
+ console.log(`Using executable: ${execPath}`);
25
+ }
26
+ else {
27
+ console.log('No specific browser executable found; falling back to Puppeteer default.');
28
+ }
29
+ const headlessBrowser = await plugins.puppeteer.launch({
17
30
  args: chromeArgs,
18
31
  pipe: options.usePipe !== undefined ? options.usePipe : true,
19
32
  headless: true,
20
- ...(() => {
21
- const returnObject = {};
22
- const googleChrome = plugins.smartshell.which.sync('google-chrome');
23
- if (googleChrome) {
24
- returnObject.executablePath = googleChrome;
25
- }
26
- return returnObject;
27
- })(),
33
+ ...executablePathOptions,
28
34
  });
29
35
  return headlessBrowser;
30
36
  };
31
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRwdXBwZXRlZXIuY2xhc3Nlcy5zbWFydHB1cHBldGVlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0cHVwcGV0ZWVyLmNsYXNzZXMuc21hcnRwdXBwZXRlZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLE9BQU8sTUFBTSw2QkFBNkIsQ0FBQztBQU92RCxNQUFNLENBQUMsTUFBTSwwQkFBMEIsR0FBRyxLQUFLLEVBQzdDLGFBQStCLEVBQUUsRUFDRyxFQUFFO0lBQ3RDLE1BQU0sT0FBTyxHQUFxQjtRQUNoQyxHQUFHO1lBQ0QsY0FBYyxFQUFFLEtBQUs7U0FDdEI7UUFDRCxHQUFHLFVBQVU7S0FDZCxDQUFDO0lBRUYsSUFBSSxVQUFVLEdBQWEsRUFBRSxDQUFDO0lBQzlCLElBQUksT0FBTyxDQUFDLEdBQUcsQ0FBQyxFQUFFLElBQUksT0FBTyxDQUFDLGNBQWMsSUFBSSxPQUFPLENBQUMsRUFBRSxDQUFDLFFBQVEsRUFBRSxDQUFDLFFBQVEsS0FBSyxNQUFNLEVBQUU7UUFDekYsVUFBVSxHQUFHLFVBQVUsQ0FBQyxNQUFNLENBQUMsQ0FBQyxjQUFjLEVBQUUsMEJBQTBCLENBQUMsQ0FBQyxDQUFDO0tBQzlFO0lBRUQsSUFBSSxlQUEwQyxDQUFDO0lBQy9DLE9BQU8sQ0FBQyxHQUFHLENBQUMsb0RBQW9ELENBQUMsQ0FBQztJQUNsRSxPQUFPLENBQUMsR0FBRyxD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PQUFPLENBQUMsR0FBRyxDQUFDLFVBQVUsQ0FBQyxDQUFDO0lBQ3hCLElBQUksUUFBUSxFQUFFLENBQUM7UUFDYixPQUFPLENBQUMsR0FBRyxDQUFDLHFCQUFxQixRQUFRLEVBQUUsQ0FBQyxDQUFDO0lBQy9DLENBQUM7U0FBTSxDQUFDO1FBQ04sT0FBTyxDQUFDLEdBQUcsQ0FBQywwRUFBMEUsQ0FBQyxDQUFDO0lBQzFGLENBQUM7SUFFRCxNQUFNLGVBQWUsR0FBRyxNQUFNLE9BQU8sQ0FBQyxTQUFTLENBQUMsTUFBTSxDQUFDO1FBQ3JELElBQUksRUFBRSxVQUFVO1FBQ2hCLElBQUksRUFBRSxPQUFPLENBQUMsT0FBTyxLQUFLLFNBQVMsQ0FBQyxDQUFDLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxDQUFDLENBQUMsSUFBSTtRQUM1RCxRQUFRLEVBQUUsSUFBSTtRQUNkLEdBQUcscUJBQXFCO0tBQ3pCLENBQUMsQ0FBQztJQUVILE9BQU8sZUFBZSxDQUFDO0FBQ3pCLENBQUMsQ0FBQyJ9
@@ -1,7 +1,7 @@
1
1
  import * as os from 'os';
2
2
  export { os };
3
- import * as smartdelay from '@pushrocks/smartdelay';
4
- import * as smartshell from '@pushrocks/smartshell';
3
+ import * as smartdelay from '@push.rocks/smartdelay';
4
+ import * as smartshell from '@push.rocks/smartshell';
5
5
  export { smartdelay, smartshell };
6
6
  import puppeteer from 'puppeteer';
7
7
  import treeKill from 'tree-kill';
@@ -2,11 +2,11 @@
2
2
  import * as os from 'os';
3
3
  export { os };
4
4
  // @pushrocks scope
5
- import * as smartdelay from '@pushrocks/smartdelay';
6
- import * as smartshell from '@pushrocks/smartshell';
5
+ import * as smartdelay from '@push.rocks/smartdelay';
6
+ import * as smartshell from '@push.rocks/smartshell';
7
7
  export { smartdelay, smartshell };
8
8
  // third party scope
9
9
  import puppeteer from 'puppeteer';
10
10
  import treeKill from 'tree-kill';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ELE9BQU8sS0FBSyxVQUFVLE1BQU0sd0JBQXdCLENBQUM7QUFFckQsT0FBTyxFQUFFLFVBQVUsRUFBRSxVQUFVLEVBQUUsQ0FBQztBQUVsQyxvQkFBb0I7QUFDcEIsT0FBTyxTQUFTLE1BQU0sV0FBVyxDQUFDO0FBQ2xDLE9BQU8sUUFBUSxNQUFNLFdBQVcsQ0FBQztBQUVqQyxPQUFPLEVBQUUsU0FBUyxFQUFFLFFBQVEsRUFBRSxDQUFDIn0=
package/npmextra.json CHANGED
@@ -2,17 +2,30 @@
2
2
  "gitzone": {
3
3
  "projectType": "npm",
4
4
  "module": {
5
- "githost": "gitlab.com",
6
- "gitscope": "pushrocks",
5
+ "githost": "code.foss.global",
6
+ "gitscope": "push.rocks",
7
7
  "gitrepo": "smartpuppeteer",
8
8
  "shortDescription": "simplified access to puppeteer",
9
- "npmPackagename": "@pushrocks/smartpuppeteer",
9
+ "npmPackagename": "@push.rocks/smartpuppeteer",
10
10
  "license": "MIT",
11
- "projectDomain": "push.rocks"
11
+ "projectDomain": "push.rocks",
12
+ "description": "Provides simplified access to Puppeteer for automation and testing purposes.",
13
+ "keywords": [
14
+ "puppeteer",
15
+ "automation",
16
+ "browser automation",
17
+ "web scraping",
18
+ "testing",
19
+ "headless browsing",
20
+ "incognito browsing"
21
+ ]
12
22
  }
13
23
  },
14
24
  "npmci": {
15
25
  "npmGlobalTools": [],
16
26
  "npmAccessLevel": "public"
27
+ },
28
+ "tsdoc": {
29
+ "legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
17
30
  }
18
31
  }
package/package.json CHANGED
@@ -1,27 +1,24 @@
1
1
  {
2
2
  "name": "@push.rocks/smartpuppeteer",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "private": false,
5
- "description": "simplified access to puppeteer",
5
+ "description": "Provides simplified access to Puppeteer for automation and testing purposes.",
6
6
  "main": "dist_ts/index.js",
7
7
  "typings": "dist_ts/index.d.ts",
8
8
  "type": "module",
9
9
  "author": "Lossless GmbH",
10
10
  "license": "MIT",
11
- "scripts": {
12
- "test": "tstest test/",
13
- "build": "tsbuild --web --allowimplicitany"
14
- },
15
11
  "devDependencies": {
16
- "@gitzone/tsbuild": "^2.1.63",
17
- "@gitzone/tstest": "^1.0.71",
18
- "@pushrocks/tapbundle": "^5.0.3",
19
- "@types/node": "^18.0.6"
12
+ "@git.zone/tsbuild": "^2.1.63",
13
+ "@git.zone/tsrun": "^1.2.46",
14
+ "@git.zone/tstest": "^1.0.71",
15
+ "@push.rocks/tapbundle": "^5.0.8",
16
+ "@types/node": "^20.12.7"
20
17
  },
21
18
  "dependencies": {
22
- "@pushrocks/smartdelay": "^2.0.13",
23
- "@pushrocks/smartshell": "^2.0.30",
24
- "puppeteer": "^15.4.0",
19
+ "@push.rocks/smartdelay": "^3.0.1",
20
+ "@push.rocks/smartshell": "^3.0.3",
21
+ "puppeteer": "^22.6.4",
25
22
  "tree-kill": "^1.2.2"
26
23
  },
27
24
  "files": [
@@ -38,5 +35,23 @@
38
35
  ],
39
36
  "browserslist": [
40
37
  "last 1 chrome versions"
41
- ]
38
+ ],
39
+ "keywords": [
40
+ "puppeteer",
41
+ "automation",
42
+ "browser automation",
43
+ "web scraping",
44
+ "testing",
45
+ "headless browsing",
46
+ "incognito browsing"
47
+ ],
48
+ "homepage": "https://code.foss.global/push.rocks/smartpuppeteer",
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "https://code.foss.global/push.rocks/smartpuppeteer.git"
52
+ },
53
+ "scripts": {
54
+ "test": "tstest test/",
55
+ "build": "tsbuild --web --allowimplicitany"
56
+ }
42
57
  }
@@ -0,0 +1 @@
1
+
package/readme.md CHANGED
@@ -1,46 +1,133 @@
1
- # @pushrocks/smartpuppeteer
1
+ # @push.rocks/smartpuppeteer
2
2
  simplified access to puppeteer
3
3
 
4
- ## Availabililty and Links
5
- * [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpuppeteer)
6
- * [gitlab.com (source)](https://gitlab.com/pushrocks/smartpuppeteer)
7
- * [github.com (source mirror)](https://github.com/pushrocks/smartpuppeteer)
8
- * [docs (typedoc)](https://pushrocks.gitlab.io/smartpuppeteer/)
9
-
10
- ## Status for master
11
-
12
- Status Category | Status Badge
13
- -- | --
14
- GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartpuppeteer/badges/master/pipeline.svg)](https://lossless.cloud)
15
- GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartpuppeteer/badges/master/coverage.svg)](https://lossless.cloud)
16
- npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartpuppeteer)](https://lossless.cloud)
17
- Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartpuppeteer)](https://lossless.cloud)
18
- TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
19
- node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
20
- Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
21
- PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartpuppeteer)](https://lossless.cloud)
22
- PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartpuppeteer)](https://lossless.cloud)
23
- BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartpuppeteer)](https://lossless.cloud)
24
- Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)
4
+ ## Install
5
+ To install `@push.rocks/smartpuppeteer` in your project, run the following command using npm:
6
+
7
+ ```sh
8
+ npm install @push.rocks/smartpuppeteer --save
9
+ ```
10
+
11
+ Or using yarn:
12
+
13
+ ```sh
14
+ yarn add @push.rocks/smartpuppeteer
15
+ ```
25
16
 
26
17
  ## Usage
18
+ `@push.rocks/smartpuppeteer` simplifies interaction with Puppeteer, providing easier ways to launch Puppeteer instances considering environment constraints, such as running in a CI pipeline or as root, which necessitates certain flags for Chrome.
27
19
 
28
- Use TypeScript for best in class intellisense.
20
+ Here, we give a comprehensive guide to using `@push.rocks/smartpuppeteer` in various scenarios, using ESM syntax and TypeScript.
21
+
22
+ ### Basic Setup
23
+ Firstly, let’s set up the basic environment for using `@push.rocks/smartpuppeteer`:
29
24
 
30
25
  ```typescript
31
- const headlessBrowser = await smartpuppeteer.getEnvAwareBrowserInstance({
32
- forceNoSandbox: true, // if you really want no sandbox, you can do this. Otherwise its starting things as necessary
33
- });
34
- await headlessBrowser.close();
26
+ import { getEnvAwareBrowserInstance, IncognitoBrowser, puppeteer } from '@push.rocks/smartpuppeteer';
27
+
28
+ // Usually, you would initialize the browser instance at the start of your script or application logic
29
+ const initializeBrowser = async () => {
30
+ const browser = await getEnvAwareBrowserInstance({
31
+ forceNoSandbox: true, // A flag useful for certain environments; use it with caution
32
+ });
33
+ return browser;
34
+ };
35
+ ```
36
+
37
+ ### Opening a Page and Navigating
38
+ After obtaining a browser instance, you commonly want to open a page and navigate to a URL:
39
+
40
+ ```typescript
41
+ const openPage = async (browser: puppeteer.Browser) => {
42
+ const page = await browser.newPage();
43
+ await page.goto('https://www.example.com');
44
+ const pageTitle = await page.title();
45
+ console.log(`Page title: ${pageTitle}`);
46
+ // Always close the browser after you are done to free resources
47
+ await browser.close();
48
+ };
49
+
50
+ // Utilize the async function
51
+ initializeBrowser()
52
+ .then(openPage)
53
+ .catch(console.error);
54
+ ```
55
+
56
+ ### Using Incognito Mode for Isolated Sessions
57
+ `@push.rocks/smartpuppeteer` offers easy management of incognito sessions, allowing isolated environments within the same browser instance:
58
+
59
+ ```typescript
60
+ const useIncognitoBrowser = async () => {
61
+ const incognitoBrowser = new IncognitoBrowser();
62
+ await incognitoBrowser.start(); // Initializes a new incognito browser instance
63
+ const context = await incognitoBrowser.getNewIncognitoContext();
64
+ const page = await context.newPage();
65
+ await page.goto('https://www.privacyfocusedsite.com');
66
+ // Perform actions in the isolated session
67
+ // Tidy up
68
+ await incognitoBrowser.stop(); // Stops the incognito browser and closes all its pages and contexts
69
+ };
70
+
71
+ useIncognitoBrowser()
72
+ .then(() => console.log('Incognito session used successfully'))
73
+ .catch(console.error);
35
74
  ```
36
75
 
37
- ## Contribution
76
+ ### Advanced Configuration
77
+ `@push.rocks/smartpuppeteer` allows further customization for launching the Puppeteer browser, such as disabling the sandbox environment (not recommended for production).
78
+
79
+ ### Handling Browser Events
80
+ It's important to handle browser events, such as disconnections, which might occur due to various reasons:
81
+
82
+ ```typescript
83
+ const browserWithEventHandling = async () => {
84
+ const browser = await getEnvAwareBrowserInstance();
85
+ browser.on('disconnected', () => {
86
+ console.log('Browser disconnected. Handling reconnection...');
87
+ // Implement reconnection logic here
88
+ });
89
+ // Utilize the browser for tasks
90
+ };
91
+
92
+ browserWithEventHandling()
93
+ .then(() => console.log('Handled browser events successfully'))
94
+ .catch(console.error);
95
+ ```
96
+
97
+ ### Rotation of Browsers and Pages
98
+ In scenarios such as web scraping or automated testing, you might want to rotate between browser instances or pages to manage memory usage or simulate new sessions:
99
+
100
+ ```typescript
101
+ const rotateBrowserInstances = async (incognitoBrowser: IncognitoBrowser) => {
102
+ // Assuming incognitoBrowser is already initialized and started
103
+ await incognitoBrowser.rotateBrowser(); // Closes the current browser and starts a new instance
104
+ // Now you have a fresh browser instance
105
+ };
106
+
107
+ // Example usage
108
+ const incognitoBrowser = new IncognitoBrowser();
109
+ incognitoBrowser.start()
110
+ .then(() => rotateBrowserInstances(incognitoBrowser))
111
+ .catch(console.error);
112
+ ```
113
+
114
+ `@push.rocks/smartpuppeteer` with its encapsulated features and simplified API provides an efficient way to harness the power of Puppeteer without getting bogged down by its complexities. Whether you are handling web scraping, automated testing, or any task requiring browser automation, `@push.rocks/smartpuppeteer` streamlines the process, making it more accessible and manageable even for those new to Puppeteer.
115
+
116
+ ## License and Legal Information
117
+
118
+ This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
119
+
120
+ **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
121
+
122
+ ### Trademarks
123
+
124
+ This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
38
125
 
39
- We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
126
+ ### Company Information
40
127
 
41
- For further information read the linked docs at the top of this readme.
128
+ Task Venture Capital GmbH
129
+ Registered at District court Bremen HRB 35230 HB, Germany
42
130
 
43
- > MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
44
- | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
131
+ For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
45
132
 
46
- [![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)
133
+ By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
@@ -1,8 +1,8 @@
1
1
  /**
2
- * autocreated commitinfo by @pushrocks/commitinfo
2
+ * autocreated commitinfo by @push.rocks/commitinfo
3
3
  */
4
4
  export const commitinfo = {
5
- name: '@pushrocks/smartpuppeteer',
6
- version: '2.0.2',
7
- description: 'simplified access to puppeteer'
5
+ name: '@push.rocks/smartpuppeteer',
6
+ version: '2.0.4',
7
+ description: 'Provides simplified access to Puppeteer for automation and testing purposes.'
8
8
  }
@@ -3,20 +3,24 @@ import * as plugins from './smartpuppeteer.plugins.js';
3
3
 
4
4
  export class IncognitoBrowser {
5
5
  public status: 'started' | 'stopped' = 'stopped';
6
- public browser: plugins.puppeteer.Browser;
6
+ public browser!: plugins.puppeteer.Browser;
7
7
 
8
8
  constructor() {}
9
9
 
10
10
  /**
11
- * starts the IncognitoBrowser
11
+ * Starts the IncognitoBrowser instance.
12
+ * It launches the browser using environment-aware options and sets up a listener
13
+ * to automatically re-launch if the browser disconnects while the status is 'started'.
12
14
  */
13
- public async start() {
15
+ public async start(): Promise<void> {
14
16
  this.status = 'started';
15
17
  this.browser = await getEnvAwareBrowserInstance();
16
- this.browser.on('disconnected', async (eventArg) => {
18
+ this.browser.on('disconnected', async () => {
17
19
  try {
18
20
  this.browser.removeAllListeners();
19
- } catch (err) {}
21
+ } catch (err) {
22
+ // Optionally handle the error.
23
+ }
20
24
  if (this.status === 'started') {
21
25
  this.browser = await getEnvAwareBrowserInstance();
22
26
  }
@@ -24,27 +28,41 @@ export class IncognitoBrowser {
24
28
  }
25
29
 
26
30
  /**
27
- * stops the IncognitoBrowser
31
+ * Stops the IncognitoBrowser instance.
32
+ * It forcefully kills the browser process (if needed) and then closes the browser.
28
33
  */
29
- public async stop() {
34
+ public async stop(): Promise<void> {
30
35
  this.status = 'stopped';
31
- plugins.treeKill(this.browser.process()?.pid as number, 'SIGKILL');
36
+ const pid = this.browser.process()?.pid;
37
+ if (pid) {
38
+ plugins.treeKill(pid, 'SIGKILL');
39
+ }
32
40
  await this.browser.close();
33
41
  }
34
42
 
35
43
  /**
36
- * rotate
44
+ * Rotates the browser instance.
45
+ * It closes the current browser and launches a new one.
37
46
  */
38
- public async rotateBrowser() {
39
- this.browser.close().catch();
47
+ public async rotateBrowser(): Promise<void> {
48
+ try {
49
+ await this.browser.close();
50
+ } catch (err) {
51
+ // Ignore errors if the browser is already closed.
52
+ }
40
53
  this.browser = await getEnvAwareBrowserInstance();
41
54
  }
42
55
 
56
+ /**
57
+ * Returns a new incognito browser context.
58
+ * This uses Puppeteer's createIncognitoBrowserContext() API, which is the
59
+ * correct method for creating isolated sessions.
60
+ */
43
61
  public async getNewIncognitoContext(): Promise<plugins.puppeteer.BrowserContext> {
44
- if (this.browser) {
45
- return this.browser.createIncognitoBrowserContext();
46
- } else {
47
- throw new Error('you need to start the IncognitoBrowser instance first');
62
+ if (!this.browser) {
63
+ throw new Error('You need to start the IncognitoBrowser instance first');
48
64
  }
65
+ // @ts-ignore
66
+ return this.browser.createIncognitoBrowserContext();
49
67
  }
50
- }
68
+ }
@@ -9,33 +9,44 @@ export const getEnvAwareBrowserInstance = async (
9
9
  optionsArg: IEnvAwareOptions = {}
10
10
  ): Promise<plugins.puppeteer.Browser> => {
11
11
  const options: IEnvAwareOptions = {
12
- ...{
13
- forceNoSandbox: false,
14
- },
12
+ forceNoSandbox: false,
15
13
  ...optionsArg,
16
14
  };
17
15
 
18
16
  let chromeArgs: string[] = [];
19
- if (process.env.CI || options.forceNoSandbox || plugins.os.userInfo().username === 'root') {
17
+ if (
18
+ process.env.CI ||
19
+ options.forceNoSandbox ||
20
+ plugins.os.userInfo().username === 'root'
21
+ ) {
20
22
  chromeArgs = chromeArgs.concat(['--no-sandbox', '--disable-setuid-sandbox']);
23
+ console.warn('********************************************************');
24
+ console.warn('WARNING: Launching browser without sandbox. This can be insecure!');
25
+ console.warn('********************************************************');
21
26
  }
22
27
 
23
- let headlessBrowser: plugins.puppeteer.Browser;
24
- console.log('launching puppeteer bundled chrome with arguments:');
28
+ // Automatically choose an executable if available: prefer google-chrome, then chromium, then chromium-browser.
29
+ const execPath =
30
+ plugins.smartshell.which.sync('google-chrome') ||
31
+ plugins.smartshell.which.sync('chromium') ||
32
+ plugins.smartshell.which.sync('chromium-browser');
33
+
34
+ const executablePathOptions = execPath ? { executablePath: execPath } : {};
35
+
36
+ console.log('Launching puppeteer browser with arguments:');
25
37
  console.log(chromeArgs);
26
- headlessBrowser = await plugins.puppeteer.launch({
38
+ if (execPath) {
39
+ console.log(`Using executable: ${execPath}`);
40
+ } else {
41
+ console.log('No specific browser executable found; falling back to Puppeteer default.');
42
+ }
43
+
44
+ const headlessBrowser = await plugins.puppeteer.launch({
27
45
  args: chromeArgs,
28
46
  pipe: options.usePipe !== undefined ? options.usePipe : true,
29
47
  headless: true,
30
- ...(() => {
31
- const returnObject: any = {};
32
- const googleChrome = plugins.smartshell.which.sync('google-chrome');
33
- if (googleChrome) {
34
- returnObject.executablePath = googleChrome;
35
- }
36
- return returnObject;
37
- })(),
48
+ ...executablePathOptions,
38
49
  });
39
50
 
40
51
  return headlessBrowser;
41
- };
52
+ };
@@ -4,8 +4,8 @@ import * as os from 'os';
4
4
  export { os };
5
5
 
6
6
  // @pushrocks scope
7
- import * as smartdelay from '@pushrocks/smartdelay';
8
- import * as smartshell from '@pushrocks/smartshell';
7
+ import * as smartdelay from '@push.rocks/smartdelay';
8
+ import * as smartshell from '@push.rocks/smartshell';
9
9
 
10
10
  export { smartdelay, smartshell };
11
11