Compare commits

...

76 Commits

Author SHA1 Message Date
4945b8f669 2.0.5 2025-02-25 17:34:41 +00:00
2715cc8853 fix(dependencies): Update dependencies including Puppeteer and node types. 2025-02-25 17:34:41 +00:00
2c3077a3d7 2.0.4 2025-02-25 17:33:48 +00:00
6ef281c871 fix(IncognitoBrowser): Enhance IncognitoBrowser error handling and process management 2025-02-25 17:33:48 +00:00
1839c56130 update description 2024-05-29 14:15:38 +02:00
229e35c9da update tsconfig 2024-04-14 18:11:25 +02:00
57776a67b3 2.0.3 2024-04-12 13:13:10 +02:00
8809b9755b fix(core): update 2024-04-12 13:13:09 +02:00
1138897da2 update tsconfig 2024-04-01 21:40:19 +02:00
831f967960 update npmextra.json: githost 2024-04-01 19:59:22 +02:00
fc602235cf update npmextra.json: githost 2024-03-30 21:48:22 +01:00
775f2bde13 switch to new org scheme 2023-07-11 01:26:37 +02:00
3c436b7167 switch to new org scheme 2023-07-10 10:17:36 +02:00
e955876a7c 2.0.2 2022-07-18 03:08:33 +02:00
b6472f38e2 fix(core): update 2022-07-18 03:08:32 +02:00
d4232decdd 2.0.1 2022-07-18 02:48:47 +02:00
76999d8720 fix(core): update 2022-07-18 02:48:46 +02:00
407ccf66b9 2.0.0 2022-03-24 13:51:13 +01:00
72e6ef04cb BREAKING CHANGE(core): switch to esm 2022-03-24 13:51:12 +01:00
d3732dcecc 1.0.36 2021-11-07 19:51:50 +01:00
a10b42270c fix(core): update 2021-11-07 19:51:50 +01:00
b2efe28f4d 1.0.35 2021-11-07 19:33:10 +01:00
1ab34d909b fix(core): update 2021-11-07 19:33:09 +01:00
09a8888d47 1.0.34 2021-11-07 18:55:11 +01:00
243beb2c75 1.0.33 2021-11-07 18:50:23 +01:00
c6fd1bea7a fix(core): update 2021-11-07 18:50:22 +01:00
4f94fa31ac 1.0.32 2021-11-07 18:38:16 +01:00
ac61d8ff4d fix(core): update 2021-11-07 18:38:15 +01:00
107178e9e7 1.0.31 2021-11-07 18:33:26 +01:00
0b5c030d5d fix(core): update 2021-11-07 18:33:25 +01:00
94f145c3dc 1.0.30 2021-11-07 18:30:09 +01:00
ebd60756ea fix(core): update 2021-11-07 18:30:08 +01:00
8305dd105c 1.0.29 2021-11-07 15:58:45 +01:00
797cac672b fix(core): update 2021-11-07 15:58:45 +01:00
4b1f058a21 1.0.28 2021-11-07 15:46:31 +01:00
c38f338f8e 1.0.27 2021-08-17 17:51:50 +02:00
103c7eb11e fix(core): update 2021-08-17 17:51:50 +02:00
0190f1bb77 1.0.26 2021-08-17 15:22:10 +02:00
87d7460183 fix(core): update 2021-08-17 15:22:10 +02:00
ed1522062e 1.0.25 2021-08-17 12:20:54 +02:00
116f700d5c fix(core): update 2021-08-17 12:20:54 +02:00
366a033904 1.0.24 2021-08-17 02:53:34 +02:00
5f1f4b3c53 fix(core): update 2021-08-17 02:53:34 +02:00
255bd7046e 1.0.23 2021-08-17 02:51:24 +02:00
bb82845f27 1.0.22 2021-08-17 02:50:49 +02:00
8ec23ba414 fix(core): update 2021-08-17 02:50:49 +02:00
937605f8c1 1.0.21 2021-01-12 14:41:45 +00:00
f05c2226c5 fix(core): update 2021-01-12 14:41:44 +00:00
1d2eca0991 1.0.20 2021-01-12 14:24:22 +00:00
c91f77a0d4 fix(core): update 2021-01-12 14:24:21 +00:00
d94826a7b7 1.0.19 2021-01-08 21:53:18 +00:00
636f0580c6 fix(core): update 2021-01-08 21:53:17 +00:00
aa5552ce3a 1.0.18 2021-01-08 21:42:53 +00:00
3ef298215a fix(core): update 2021-01-08 21:42:52 +00:00
23c65e23ae 1.0.17 2021-01-08 21:16:26 +00:00
ef55253b8e fix(core): update 2021-01-08 21:16:25 +00:00
1b72af4702 1.0.16 2021-01-08 21:14:01 +00:00
44314c9894 fix(core): update 2021-01-08 21:14:01 +00:00
35129df11c 1.0.15 2020-02-25 01:38:24 +00:00
5dd6450347 fix(core): update 2020-02-25 01:38:24 +00:00
190d57bbe2 1.0.14 2019-11-16 17:51:13 +01:00
8d4519d615 fix(cli logs): now correctly stating when WSL is detected. 2019-11-16 17:51:11 +01:00
42a68434c0 1.0.13 2019-11-16 01:22:10 +01:00
35fd4678d1 fix(core): update 2019-11-16 01:22:08 +01:00
01a4122529 1.0.12 2019-11-16 01:15:10 +01:00
fbb94fe51e fix(core): update 2019-11-16 01:15:08 +01:00
656e21c8fd 1.0.11 2019-11-16 00:35:05 +01:00
53b5cca687 fix(core): update 2019-11-16 00:35:03 +01:00
25d191a95b 1.0.10 2019-11-16 00:01:07 +01:00
d5b3e48f8d fix(core): update 2019-11-16 00:01:05 +01:00
dd6f24551d 1.0.9 2019-11-15 23:59:35 +01:00
f4d0e9ad87 fix(core): update 2019-11-15 23:59:34 +01:00
41aefa7f18 1.0.8 2019-11-15 23:41:41 +01:00
c9f42b4c66 fix(core): update 2019-11-15 23:41:39 +01:00
0645364ca9 1.0.7 2019-11-15 21:44:13 +01:00
2d3f59cc65 fix(core): update 2019-11-15 21:44:11 +01:00
19 changed files with 20805 additions and 1213 deletions

4
.gitignore vendored
View File

@ -15,8 +15,6 @@ node_modules/
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
dist_*/
# custom

View File

@ -1,16 +1,19 @@
# gitzone standard
image: hosttoday/ht-docker-node:npmci
# gitzone ci_default
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
- .npmci_cache/
key: '$CI_BUILD_STAGE'
stages:
- security
- test
- release
- metadata
- security
- test
- release
- metadata
before_script:
- npm install -g @shipzone/npmci
# ====================
# security stage
@ -18,102 +21,115 @@ stages:
mirror:
stage: security
script:
- npmci git mirror
- npmci git mirror
only:
- tags
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
snyk:
auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
- npmci command npm install --production --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
- notpriv
- docker
allow_failure: true
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=dev
tags:
- docker
allow_failure: true
# ====================
# test stage
# ====================
testLTS:
testStable:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci npm test
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
testSTABLE:
- docker
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
- docker
release:
stage: release
script:
- npmci node install stable
- npmci npm publish
- npmci node install stable
- npmci npm publish
only:
- tags
- tags
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
image: docker:stable
allow_failure: true
services:
- docker:stable-dind
only:
- tags
script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
- docker run
--env SOURCE_CODE="$PWD"
--volume "$PWD":/code
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
- npmci command npm install -g tslint typescript
- npmci npm prepare
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- docker
- priv
- lossless
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
- npmci trigger
only:
- tags
- tags
tags:
- docker
- notpriv
- lossless
- docker
- notpriv
pages:
image: hosttoday/ht-docker-node:npmci
stage: metadata
script:
- npmci command npm install -g @gitzone/tsdoc
- npmci node install lts
- npmci command npm install -g @git.zone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
tags:
- lossless
- docker
- notpriv
only:
@ -121,5 +137,5 @@ pages:
artifacts:
expire_in: 1 week
paths:
- public
- public
allow_failure: true

11
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
}
]
}

26
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}

33
changelog.md Normal file
View File

@ -0,0 +1,33 @@
# Changelog
## 2025-02-25 - 2.0.5 - fix(dependencies)
Update dependencies including Puppeteer and node types.
- Updated Puppeteer version from ^22.6.4 to ^24.3.0.
- Updated @types/node version from ^20.12.7 to ^22.13.5.
## 2025-02-25 - 2.0.4 - fix(IncognitoBrowser)
Enhance IncognitoBrowser error handling and process management
- Improve error handling during browser disconnection and process management.
- Ensure safe re-launch of the browser if it disconnects while the status is 'started'.
- Add logging for critical operations like launching without sandbox.
## 2024-04-12 to 2024-05-29 - 2.0.3 - Minor Updates
Various minor updates and configuration changes were made during this period.
- Updated project description.
- Updated TypeScript configuration.
## 2023-07-10 to 2024-04-12 - 2.0.2 - Organizational and Configuration Updates
This release cycle focused on organizational changes and configuration updates.
- Switched to new organization scheme.
- Updated `npmextra.json` to include new git host information.
- Made updates to TypeScript configuration.
## 2022-03-24 to 2022-07-18 - 2.0.0 to 2.0.1 - Core Updates and Fixes
During these versions, significant internal changes were made with continued improvements.
- **BREAKING CHANGE:** Switched to ECMAScript Modules (ESM) format.
- Multiple fixes in the core module, enhancing stability.

View File

@ -1,17 +1,31 @@
{
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartpuppeteer",
"shortDescription": "simplified access to puppeteer",
"npmPackagename": "@pushrocks/smartpuppeteer",
"npmPackagename": "@push.rocks/smartpuppeteer",
"license": "MIT",
"projectDomain": "push.rocks"
"projectDomain": "push.rocks",
"description": "Provides simplified access to Puppeteer for automation and testing purposes.",
"keywords": [
"puppeteer",
"automation",
"browser automation",
"web scraping",
"testing",
"headless browsing",
"incognito browsing"
]
}
},
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
},
"tsdoc": {
"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"
}
}

11241
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,37 +1,57 @@
{
"name": "@pushrocks/smartpuppeteer",
"version": "1.0.6",
"name": "@push.rocks/smartpuppeteer",
"version": "2.0.5",
"private": false,
"description": "simplified access to puppeteer",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"description": "Provides simplified access to Puppeteer for automation and testing purposes.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
"author": "Lossless GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/)",
"build": "(tsbuild --web)",
"format": "(gitzone format)"
"test": "tstest test/",
"build": "tsbuild --web --allowimplicitany"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.17",
"@gitzone/tstest": "^1.0.28",
"@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^12.12.7",
"tslint": "^5.20.1",
"tslint-config-prettier": "^1.15.0"
"@git.zone/tsbuild": "^2.1.63",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.71",
"@push.rocks/tapbundle": "^5.0.8",
"@types/node": "^22.13.5"
},
"dependencies": {
"@types/puppeteer": "^1.20.2",
"puppeteer": "^2.0.0"
"@push.rocks/smartdelay": "^3.0.1",
"@push.rocks/smartshell": "^3.0.3",
"puppeteer": "^24.3.0",
"tree-kill": "^1.2.2"
},
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"assets/*",
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
]
],
"browserslist": [
"last 1 chrome versions"
],
"keywords": [
"puppeteer",
"automation",
"browser automation",
"web scraping",
"testing",
"headless browsing",
"incognito browsing"
],
"homepage": "https://code.foss.global/push.rocks/smartpuppeteer",
"repository": {
"type": "git",
"url": "https://code.foss.global/push.rocks/smartpuppeteer.git"
}
}

10110
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

1
readme.hints.md Normal file
View File

@ -0,0 +1 @@

143
readme.md
View File

@ -1,26 +1,133 @@
# @pushrocks/smartpuppeteer
# @push.rocks/smartpuppeteer
simplified access to puppeteer
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartpuppeteer)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartpuppeteer)
* [github.com (source mirror)](https://github.com/pushrocks/smartpuppeteer)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartpuppeteer/)
## Install
To install `@push.rocks/smartpuppeteer` in your project, run the following command using npm:
## Status for master
[![build status](https://gitlab.com/pushrocks/smartpuppeteer/badges/master/build.svg)](https://gitlab.com/pushrocks/smartpuppeteer/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartpuppeteer/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartpuppeteer/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartpuppeteer.svg)](https://www.npmjs.com/package/@pushrocks/smartpuppeteer)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smartpuppeteer/badge.svg)](https://snyk.io/test/npm/@pushrocks/smartpuppeteer)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
```sh
npm install @push.rocks/smartpuppeteer --save
```
Or using yarn:
```sh
yarn add @push.rocks/smartpuppeteer
```
## Usage
`@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.
For further information read the linked docs at the top of this readme.
Here, we give a comprehensive guide to using `@push.rocks/smartpuppeteer` in various scenarios, using ESM syntax and TypeScript.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
### Basic Setup
Firstly, lets set up the basic environment for using `@push.rocks/smartpuppeteer`:
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://maintainedby.lossless.com)
```typescript
import { getEnvAwareBrowserInstance, IncognitoBrowser, puppeteer } from '@push.rocks/smartpuppeteer';
// Usually, you would initialize the browser instance at the start of your script or application logic
const initializeBrowser = async () => {
const browser = await getEnvAwareBrowserInstance({
forceNoSandbox: true, // A flag useful for certain environments; use it with caution
});
return browser;
};
```
### Opening a Page and Navigating
After obtaining a browser instance, you commonly want to open a page and navigate to a URL:
```typescript
const openPage = async (browser: puppeteer.Browser) => {
const page = await browser.newPage();
await page.goto('https://www.example.com');
const pageTitle = await page.title();
console.log(`Page title: ${pageTitle}`);
// Always close the browser after you are done to free resources
await browser.close();
};
// Utilize the async function
initializeBrowser()
.then(openPage)
.catch(console.error);
```
### Using Incognito Mode for Isolated Sessions
`@push.rocks/smartpuppeteer` offers easy management of incognito sessions, allowing isolated environments within the same browser instance:
```typescript
const useIncognitoBrowser = async () => {
const incognitoBrowser = new IncognitoBrowser();
await incognitoBrowser.start(); // Initializes a new incognito browser instance
const context = await incognitoBrowser.getNewIncognitoContext();
const page = await context.newPage();
await page.goto('https://www.privacyfocusedsite.com');
// Perform actions in the isolated session
// Tidy up
await incognitoBrowser.stop(); // Stops the incognito browser and closes all its pages and contexts
};
useIncognitoBrowser()
.then(() => console.log('Incognito session used successfully'))
.catch(console.error);
```
### Advanced Configuration
`@push.rocks/smartpuppeteer` allows further customization for launching the Puppeteer browser, such as disabling the sandbox environment (not recommended for production).
### Handling Browser Events
It's important to handle browser events, such as disconnections, which might occur due to various reasons:
```typescript
const browserWithEventHandling = async () => {
const browser = await getEnvAwareBrowserInstance();
browser.on('disconnected', () => {
console.log('Browser disconnected. Handling reconnection...');
// Implement reconnection logic here
});
// Utilize the browser for tasks
};
browserWithEventHandling()
.then(() => console.log('Handled browser events successfully'))
.catch(console.error);
```
### Rotation of Browsers and Pages
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:
```typescript
const rotateBrowserInstances = async (incognitoBrowser: IncognitoBrowser) => {
// Assuming incognitoBrowser is already initialized and started
await incognitoBrowser.rotateBrowser(); // Closes the current browser and starts a new instance
// Now you have a fresh browser instance
};
// Example usage
const incognitoBrowser = new IncognitoBrowser();
incognitoBrowser.start()
.then(() => rotateBrowserInstances(incognitoBrowser))
.catch(console.error);
```
`@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.
## License and Legal Information
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.
**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.
### Trademarks
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.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
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.

View File

@ -1,11 +1,34 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartpuppeteer from '../ts/index';
import { expect, tap } from '@push.rocks/tapbundle';
import * as smartpuppeteer from '../ts/index.js';
tap.test('first test', async () => {
tap.test('should use pipe', async (tools) => {
const headlessBrowser = await smartpuppeteer.getEnvAwareBrowserInstance({
forceNoSandbox: true
forceNoSandbox: false,
});
const page = await headlessBrowser.newPage();
await page.goto('https://lossless.com');
console.log(await page.title());
expect(headlessBrowser.wsEndpoint()).toEqual('');
await headlessBrowser.close();
});
tap.test('should use websocket', async (tools) => {
const headlessBrowser = await smartpuppeteer.getEnvAwareBrowserInstance({
forceNoSandbox: false,
usePipe: false,
});
const page = await headlessBrowser.newPage();
await page.goto('https://lossless.com');
console.log(await page.title());
expect(headlessBrowser.wsEndpoint()).not.toEqual('');
await headlessBrowser.close();
});
tap.test('should get and stop an Incognito browser', async () => {
const incognitoInstance = new smartpuppeteer.IncognitoBrowser();
await incognitoInstance.start();
const page = await incognitoInstance.browser.newPage();
await incognitoInstance.stop();
});
tap.start();

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartpuppeteer',
version: '2.0.5',
description: 'Provides simplified access to Puppeteer for automation and testing purposes.'
}

View File

@ -1,25 +1,8 @@
import puppeteer from 'puppeteer';
// module exports
export interface IEnvAwareOptions {
forceNoSandbox?: boolean;
}
export const getEnvAwareBrowserInstance = async (optionsArg: IEnvAwareOptions = {}): Promise<puppeteer.Browser> => {
const options: IEnvAwareOptions = {
...{
forceNoSandbox: false
},
...optionsArg
}
let chromeArgs: string[] = [];
if (process.env.CI || options.forceNoSandbox) {
chromeArgs = chromeArgs.concat(['--no-sandbox', '--disable-setuid-sandbox']);
}
const headlessBrowser = await puppeteer.launch({
args: chromeArgs
});
return headlessBrowser;
};
export * from './smartpuppeteer.classes.smartpuppeteer.js';
export * from './smartpuppeteer.classes.incognitobrowser.js';
// direct exports
import { puppeteer } from './smartpuppeteer.plugins.js';
export { puppeteer };

View File

@ -0,0 +1,68 @@
import { getEnvAwareBrowserInstance } from './smartpuppeteer.classes.smartpuppeteer.js';
import * as plugins from './smartpuppeteer.plugins.js';
export class IncognitoBrowser {
public status: 'started' | 'stopped' = 'stopped';
public browser!: plugins.puppeteer.Browser;
constructor() {}
/**
* Starts the IncognitoBrowser instance.
* It launches the browser using environment-aware options and sets up a listener
* to automatically re-launch if the browser disconnects while the status is 'started'.
*/
public async start(): Promise<void> {
this.status = 'started';
this.browser = await getEnvAwareBrowserInstance();
this.browser.on('disconnected', async () => {
try {
this.browser.removeAllListeners();
} catch (err) {
// Optionally handle the error.
}
if (this.status === 'started') {
this.browser = await getEnvAwareBrowserInstance();
}
});
}
/**
* Stops the IncognitoBrowser instance.
* It forcefully kills the browser process (if needed) and then closes the browser.
*/
public async stop(): Promise<void> {
this.status = 'stopped';
const pid = this.browser.process()?.pid;
if (pid) {
plugins.treeKill(pid, 'SIGKILL');
}
await this.browser.close();
}
/**
* Rotates the browser instance.
* It closes the current browser and launches a new one.
*/
public async rotateBrowser(): Promise<void> {
try {
await this.browser.close();
} catch (err) {
// Ignore errors if the browser is already closed.
}
this.browser = await getEnvAwareBrowserInstance();
}
/**
* Returns a new incognito browser context.
* This uses Puppeteer's createIncognitoBrowserContext() API, which is the
* correct method for creating isolated sessions.
*/
public async getNewIncognitoContext(): Promise<plugins.puppeteer.BrowserContext> {
if (!this.browser) {
throw new Error('You need to start the IncognitoBrowser instance first');
}
// @ts-ignore
return this.browser.createIncognitoBrowserContext();
}
}

View File

@ -0,0 +1,52 @@
import * as plugins from './smartpuppeteer.plugins.js';
export interface IEnvAwareOptions {
forceNoSandbox?: boolean;
usePipe?: boolean;
}
export const getEnvAwareBrowserInstance = async (
optionsArg: IEnvAwareOptions = {}
): Promise<plugins.puppeteer.Browser> => {
const options: IEnvAwareOptions = {
forceNoSandbox: false,
...optionsArg,
};
let chromeArgs: string[] = [];
if (
process.env.CI ||
options.forceNoSandbox ||
plugins.os.userInfo().username === 'root'
) {
chromeArgs = chromeArgs.concat(['--no-sandbox', '--disable-setuid-sandbox']);
console.warn('********************************************************');
console.warn('WARNING: Launching browser without sandbox. This can be insecure!');
console.warn('********************************************************');
}
// Automatically choose an executable if available: prefer google-chrome, then chromium, then chromium-browser.
const execPath =
plugins.smartshell.which.sync('google-chrome') ||
plugins.smartshell.which.sync('chromium') ||
plugins.smartshell.which.sync('chromium-browser');
const executablePathOptions = execPath ? { executablePath: execPath } : {};
console.log('Launching puppeteer browser with arguments:');
console.log(chromeArgs);
if (execPath) {
console.log(`Using executable: ${execPath}`);
} else {
console.log('No specific browser executable found; falling back to Puppeteer default.');
}
const headlessBrowser = await plugins.puppeteer.launch({
args: chromeArgs,
pipe: options.usePipe !== undefined ? options.usePipe : true,
headless: true,
...executablePathOptions,
});
return headlessBrowser;
};

View File

@ -0,0 +1,16 @@
// node native scope
import * as os from 'os';
export { os };
// @pushrocks scope
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartshell from '@push.rocks/smartshell';
export { smartdelay, smartshell };
// third party scope
import puppeteer from 'puppeteer';
import treeKill from 'tree-kill';
export { puppeteer, treeKill };

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true
},
"exclude": [
"dist_*/**/*.d.ts"
]
}

View File

@ -1,17 +0,0 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}