-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
64 lines (52 loc) · 2.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { KameleoLocalApiClient } from "@kameleo/local-api-client";
import playwright from "playwright";
import { setTimeout } from "timers/promises";
// This is the port Kameleo.CLI is listening on. Default value is 5050, but can be overridden in appsettings.json file
const kameleoPort = process.env["KAMELEO_PORT"] || 5050;
const kameleoCliUri = `http://localhost:${kameleoPort}`;
// Initialize the Kameleo client
const client = new KameleoLocalApiClient({
basePath: kameleoCliUri,
});
// Search Firefox fingerprints
const fingerprints = await client.fingerprint.searchFingerprints("desktop", undefined, "firefox");
// Create a new profile with recommended settings
// for browser fingerprint protection
/** @type {import('@kameleo/local-api-client').CreateProfileRequest} */
const createProfileRequest = {
fingerprintId: fingerprints[0].id,
name: "connect with Playwright to Firefox example",
};
const profile = await client.profile.createProfile(createProfileRequest);
// Start the Kameleo profile and connect with Playwright
const browserWSEndpoint = `ws://localhost:${kameleoPort}/playwright/${profile.id}`;
// The Playwright framework is not designed to connect to already running
// browsers. To overcome this limitation, a tool bundled with Kameleo, named
// pw-bridge will bridge the communication gap between the running Firefox
// instance and this playwright script.
// The exact path to the bridge executable is subject to change
let pwBridgePath = process.env["PW_BRIDGE_PATH"];
if (!pwBridgePath && process.platform === "win32") {
pwBridgePath = `${process.env["LOCALAPPDATA"]}\\Programs\\Kameleo\\pw-bridge.exe`;
} else if (!pwBridgePath && process.platform === "darwin") {
pwBridgePath = "/Applications/Kameleo.app/Contents/Resources/CLI/pw-bridge";
}
const browser = await playwright.firefox.launchPersistentContext("", {
executablePath: pwBridgePath,
args: [`-target ${browserWSEndpoint}`],
viewport: null,
});
// Kameleo will open the a new page in the default browser context.
// NOTE: We DO NOT recommend using multiple browser contexts, as this might interfere
// with Kameleo's browser fingerprint modification features.
const page = await browser.newPage();
// Use any Playwright command to drive the browser
// and enjoy full protection from bot detection products
await page.goto("https://wikipedia.org");
await page.click("[name=search]");
await page.keyboard.type("Chameleon");
await page.keyboard.press("Enter");
// Wait for 5 seconds
await setTimeout(5_000);
// Stop the browser by stopping the Kameleo profile
await client.profile.stopProfile(profile.id);