k.utils.puppeteer
Headless browser screenshots, page script evaluation, and long-lived task management
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API, background render tasks | Requires local or self-hosted headless browser; throws in hosted non-Puppeteer sites | Capturing JS-rendered pages, evaluating dynamic DOM, and managing render task slots |
k.utils.puppeteer controls headless browser instances via Puppeteer for webpage screenshots, DOM/JavaScript evaluation, and background rendering session lifecycle management.
Environment Restrictions
This module requires a local or self-hosted Kooboo environment with headless browser support; calling it on hosted environments without Puppeteer throws an exception.
Quick Start
k.api.get("evalTitle", () => {
const raw = k.utils.puppeteer.evaluate("https://www.example.com", "document.title")
const title = raw ? JSON.parse(raw) : null
return { title }
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
screenshot(url, savePath[, options]) | Capture a screenshot of an absolute URL and save to a site file | void |
evaluate(url, script[, options]) | Open an absolute URL in headless browser and evaluate JS (returns JSON text) | string |
evaluateHtml(html, script[, options]) | Load an HTML string and evaluate a JS expression (null if html is null) | string | null |
evaluatePage(pageName, script[, options]) | Render a site page by name and evaluate JS (null if page not found) | string | null |
evaluateLocalPageUrl(relativeUrl, script[, options]) | Render a site relative URL and evaluate JS (null if route/page not found) | string | null |
openTask(taskId, url[, timeoutSeconds]) | Open a concurrency-pooled long-lived browser task (null if no slot is available) | string | null |
keepTaskAlive(taskId[, timeoutSeconds]) | Refresh keep-alive timeout for an open task | boolean |
closeTask(taskId) | Close an open task and release its slot | boolean |
isTaskOpen(taskId) | Check whether a task is currently running | boolean |
openTaskCount() | Get the number of active tasks for the current site | number |
getTaskCapacity() | Read browser concurrency slot capacity and usage | TaskCapacityInfo |
screenshot()
Capture a screenshot of an absolute URL and save it to a site file.
| Parameter | Type | Required | Description |
|---|---|---|---|
url | string | Yes | Absolute HTTP/HTTPS URL |
savePath | string | Yes | Save path inside the site |
options | ScreenshotOptions | No | Screenshot options |
Returns: void; the screenshot is written to savePath.
ScreenshotOptions
| Property | Type | Default | Description |
|---|---|---|---|
width | number | 1440 | Viewport width |
height | number | 900 | Viewport height |
delaySeconds | number | 0 (when options omitted) / 3 (property default when options object omits it) | Wait time in seconds after page load |
fullPage | boolean | false | Whether to capture the full page |
k.utils.puppeteer.screenshot("https://example.com", "shots/page.png", {
width: 1440,
height: 900,
fullPage: true,
delaySeconds: 3
})Headless Page Evaluation
Load a URL, HTML string, or site page in headless browser and evaluate a JavaScript expression, returning JSON-encoded text via JSON.stringify() (for example, document.title returns "\"Example Domain\"", and undefined returns "null"; parse the value back with JSON.parse()). Returns null when evaluateHtml receives null HTML or when evaluatePage / evaluateLocalPageUrl cannot find the target page or route.
| Method | Parameters | Return | Description |
|---|---|---|---|
evaluate(url, script, options?) | string, string, EvaluateOptions? | string | Open an absolute HTTP/HTTPS URL and evaluate |
evaluateHtml(html, script, options?) | string, string, EvaluateOptions? | string | null | Load an HTML string and evaluate (null if html is null) |
evaluatePage(pageName, script, options?) | string, string, EvaluateOptions? | string | null | Render a site page by name and evaluate (null if page not found) |
evaluateLocalPageUrl(relativeUrl, script, options?) | string, string, EvaluateOptions? | string | null | Render a site relative URL and evaluate (null if route/page not found) |
EvaluateOptions
| Property | Type | Default | Description |
|---|---|---|---|
width | number | 1366 | Viewport width (when > 0) |
height | number | 768 | Viewport height (when > 0) |
timeoutSeconds | number | 30 | Navigation and waitSelector timeout in seconds |
delaySeconds | number | 0 | Extra wait in seconds after load |
waitSelector | string | — | Optional CSS selector to wait for before evaluation |
Returns: JSON-encoded string or null; string results are JSON-quoted and undefined evaluates to "null", so parse the result with JSON.parse().
const title = JSON.parse(k.utils.puppeteer.evaluate("https://www.example.com", "document.title"))
const rawText = k.utils.puppeteer.evaluateHtml("<html><body>hi</body></html>", "document.body.innerText")
const text = rawText ? JSON.parse(rawText) : null
const rawInfo = k.utils.puppeteer.evaluatePage("home", "({ title: document.title, links: document.links.length })", {
waitSelector: "#app",
timeoutSeconds: 20,
delaySeconds: 1
})
const info = rawInfo ? JSON.parse(rawInfo) : null
const rawAboutTitle = k.utils.puppeteer.evaluateLocalPageUrl("/about", "document.title")
const aboutTitle = rawAboutTitle ? JSON.parse(rawAboutTitle) : nullTask Lifecycle Management
Manage concurrency-limited long-lived headless browser sessions.
| Method | Parameters | Return | Description |
|---|---|---|---|
openTask(taskId, url, timeoutSeconds?) | string, string, number? | string | null | Open a task (url must be absolute HTTP/HTTPS; default timeout 300 seconds; returns null immediately when all page slots are occupied) |
keepTaskAlive(taskId, timeoutSeconds?) | string, number? | boolean | Refresh task keep-alive timeout |
closeTask(taskId) | string | boolean | Close a task and release its slot |
isTaskOpen(taskId) | string | boolean | Check whether a task is currently open |
openTaskCount() | None | number | Number of open tasks for the current site |
getTaskCapacity() | None | TaskCapacityInfo | Get browser slot capacity and usage |
TaskCapacityInfo
| Property | Type | Description |
|---|---|---|
maximum | number | Maximum concurrent page slots |
inUse | number | Currently used page slots |
available | number | Remaining available slots |
siteOpenTasks | number | Open tasks belonging to the current site |
Returns: Task ID string (openTask() returns null immediately without waiting or throwing when all concurrent page slots are occupied), boolean state, count, or TaskCapacityInfo.
const taskId = "render-job-1"
const openedId = k.utils.puppeteer.openTask(taskId, "https://localkooboo.com/server-render/batch?task=" + taskId, 300)
if (!openedId) {
return { error: "No available browser slots" }
}
const running = k.utils.puppeteer.isTaskOpen(taskId)
const kept = k.utils.puppeteer.keepTaskAlive(taskId, 120)
const capacity = k.utils.puppeteer.getTaskCapacity()
const closed = k.utils.puppeteer.closeTask(taskId)
return { running, kept, capacity, closed }TypeScript Definitions
interface KPuppeteer {
openTask(taskId: string, url: string, timeoutSeconds?: number): string | null;
keepTaskAlive(taskId: string, timeoutSeconds?: number): boolean;
closeTask(taskId: string): boolean;
isTaskOpen(taskId: string): boolean;
openTaskCount(): number;
getTaskCapacity(): TaskCapacityInfo;
screenshot(url: string, savePath: string, options?: ScreenshotOptions): void;
evaluate(url: string, script: string, options?: EvaluateOptions): string;
evaluateHtml(html: string, script: string, options?: EvaluateOptions): string | null;
evaluatePage(pageName: string, script: string, options?: EvaluateOptions): string | null;
evaluateLocalPageUrl(relativeUrl: string, script: string, options?: EvaluateOptions): string | null;
}
interface ScreenshotOptions {
delaySeconds: number;
width: number;
height: number;
fullPage: boolean;
}
interface EvaluateOptions {
width: number;
height: number;
timeoutSeconds: number;
delaySeconds: number;
waitSelector: string;
}
interface TaskCapacityInfo {
maximum: number;
inUse: number;
available: number;
siteOpenTasks: number;
}Related Docs
- k.utils.screenShot — Screenshot service