Playfast is a lightweight Chromium driver built for Node.js. It provides a simple API for controlling Chromium using the Chrome DevTools Protocol.
npm install playfastconst playfast = require("playfast");
const path = require("path");
async function main() {
const browser = await new playfast().launchPersistentContext(
path.join(__dirname, "browser-profile"),
{
executablePath: path.join(__dirname, "chrome-win/chrome.exe")
}
);
const page = await browser.newPage();
await page.goto("https://www.google.com");
console.log(await page.url());
await browser.close();
}
main();Creates a new Playfast instance.
const playfast = require("playfast");
const browserManager = new playfast();Launches a persistent Chromium instance.
Parameters:
| Parameter | Type | Description |
|---|---|---|
userDataDir |
string |
Path to the user data directory |
options |
object |
Launch options |
Supported Options:
| Option | Type | Description |
|---|---|---|
executablePath |
string |
Path to Chromium/Chrome executable |
userAgent |
string |
Custom user agent |
timezoneId |
string |
Timezone to emulate |
colorScheme |
string |
"light" or "dark" |
proxy |
object |
{ server: "http://..." } |
args |
string[] |
Additional command line arguments |
Returns: Promise<Browser>
| Method | Description | Returns |
|---|---|---|
newPage() |
Creates a new page | Promise<Page> |
close() |
Closes the browser | Promise<void> |
cookies() |
Get all cookies | Promise<Array> |
addCookies(cookies) |
Add cookies | Promise<void> |
exposeFunction(name, fn) |
Expose a Node.js function to the browser | Promise<void> |
addInitScript(script) |
Add script that runs on every new document | Promise<void> |
Events:
close— Emitted when the browser closes.
| Method | Description | Returns |
|---|---|---|
goto(url) |
Navigate to a URL | Promise<void> |
url() |
Get current page URL | Promise<string> |
reload() |
Reload the current page | Promise<void> |
evaluate(fn, ...args) |
Execute JavaScript in the page context | Promise<any> |
getSelector(selector) |
Get a selector object | Promise<Selector> |
close() |
Close the page | Promise<void> |
requestGC() |
Trigger garbage collection | Promise<void> |
scriptParsed(fn) |
Listen for parsed scripts (advanced) | - |
paused(fn) |
Listen for debugger pauses (advanced) | - |
| Event | Description |
|---|---|
load |
Page finished loading |
requestfailed |
A network request failed |
websocket |
WebSocket connection created |
websocketReceived |
WebSocket message received |
| Method | Description | Returns |
|---|---|---|
evaluate(fn, ...args) |
Execute function in the context of the element | Promise<any> |
const page = await browser.newPage();
await page.goto("https://example.com");
// Run JavaScript
const title = await page.evaluate(() => document.title);
// Work with elements
const button = await page.getSelector("button.login");
await button.evaluate(el => el.click());