Skip to content

k.utils.puppeteer ​

Headless browser screenshots, page script evaluation, and long-lived task management

Execution ContextPrerequisitesTypical Use
KScript API, background render tasksRequires local or self-hosted headless browser; throws in hosted non-Puppeteer sitesCapturing 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 ​

js
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 / PropertyDescriptionReturns
screenshot(url, savePath[, options])Capture a screenshot of an absolute URL and save to a site filevoid
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 taskboolean
closeTask(taskId)Close an open task and release its slotboolean
isTaskOpen(taskId)Check whether a task is currently runningboolean
openTaskCount()Get the number of active tasks for the current sitenumber
getTaskCapacity()Read browser concurrency slot capacity and usageTaskCapacityInfo

screenshot() ​

Capture a screenshot of an absolute URL and save it to a site file.

ParameterTypeRequiredDescription
urlstringYesAbsolute HTTP/HTTPS URL
savePathstringYesSave path inside the site
optionsScreenshotOptionsNoScreenshot options

Returns: void; the screenshot is written to savePath.

ScreenshotOptions ​

PropertyTypeDefaultDescription
widthnumber1440Viewport width
heightnumber900Viewport height
delaySecondsnumber0 (when options omitted) / 3 (property default when options object omits it)Wait time in seconds after page load
fullPagebooleanfalseWhether to capture the full page
js
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.

MethodParametersReturnDescription
evaluate(url, script, options?)string, string, EvaluateOptions?stringOpen an absolute HTTP/HTTPS URL and evaluate
evaluateHtml(html, script, options?)string, string, EvaluateOptions?string | nullLoad an HTML string and evaluate (null if html is null)
evaluatePage(pageName, script, options?)string, string, EvaluateOptions?string | nullRender a site page by name and evaluate (null if page not found)
evaluateLocalPageUrl(relativeUrl, script, options?)string, string, EvaluateOptions?string | nullRender a site relative URL and evaluate (null if route/page not found)

EvaluateOptions ​

PropertyTypeDefaultDescription
widthnumber1366Viewport width (when > 0)
heightnumber768Viewport height (when > 0)
timeoutSecondsnumber30Navigation and waitSelector timeout in seconds
delaySecondsnumber0Extra wait in seconds after load
waitSelectorstring—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().

js
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) : null

Task Lifecycle Management ​

Manage concurrency-limited long-lived headless browser sessions.

MethodParametersReturnDescription
openTask(taskId, url, timeoutSeconds?)string, string, number?string | nullOpen 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?booleanRefresh task keep-alive timeout
closeTask(taskId)stringbooleanClose a task and release its slot
isTaskOpen(taskId)stringbooleanCheck whether a task is currently open
openTaskCount()NonenumberNumber of open tasks for the current site
getTaskCapacity()NoneTaskCapacityInfoGet browser slot capacity and usage

TaskCapacityInfo ​

PropertyTypeDescription
maximumnumberMaximum concurrent page slots
inUsenumberCurrently used page slots
availablenumberRemaining available slots
siteOpenTasksnumberOpen 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.

js
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 ​

ts
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;
}