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 playfastconstplayfast=require("playfast");constpath=require("path");asyncfunctionmain(){constbrowser=awaitnewplayfast().launchPersistentContext(path.join(__dirname,"browser-profile"),{executablePath: path.join(__dirname,"chrome-win/chrome.exe")});constpage=awaitbrowser.newPage();awaitpage.goto("https://www.google.com");console.log(awaitpage.url());awaitbrowser.close();}main();Creates a new Playfast instance.
constplayfast=require("playfast");constbrowserManager=newplayfast();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> |
constpage=awaitbrowser.newPage();awaitpage.goto("https://example.com");// Run JavaScriptconsttitle=awaitpage.evaluate(()=>document.title);// Work with elementsconstbutton=awaitpage.getSelector("button.login");awaitbutton.evaluate(el=>el.click());