Page Domain
The Page domain provides methods for navigation, screenshots, PDF generation, and page lifecycle management.
Import
const cdp = @import("cdp");
const Page = cdp.Page;Initialization
var session = try browser.newPage();
var page = Page.init(session);
try page.enable();Methods
enable / disable
Enable or disable the Page domain.
pub fn enable(self: *Page) !void
pub fn disable(self: *Page) !voidnavigate
Navigate to a URL.
pub fn navigate(self: *Page, allocator: Allocator, url: []const u8) !NavigateResultReturns: NavigateResult (caller must deinit)
Example:
var result = try page.navigate(allocator, "https://example.com");
defer result.deinit(allocator);
if (result.error_text) |err| {
std.debug.print("Navigation error: {s}\n", .{err});
}reload
Reload the current page.
pub fn reload(self: *Page, ignore_cache: ?bool) !voidExample:
try page.reload(true); // Ignore cachestopLoading
Stop page loading.
pub fn stopLoading(self: *Page) !voidcaptureScreenshot
Capture a screenshot.
pub fn captureScreenshot(
self: *Page,
allocator: Allocator,
params: CaptureScreenshotParams,
) ![]const u8Parameters:
| Field | Type | Description |
|---|---|---|
format | ?ScreenshotFormat | .png, .jpeg, .webp |
quality | ?i32 | JPEG quality (0-100) |
clip | ?Viewport | Capture region |
from_surface | ?bool | Capture from surface |
capture_beyond_viewport | ?bool | Capture full page |
Returns: Base64-encoded image data
Example:
const screenshot = try page.captureScreenshot(allocator, .{
.format = .png,
.capture_beyond_viewport = true,
});
defer allocator.free(screenshot);
// Decode base64
const decoded = try cdp.base64.decodeAlloc(allocator, screenshot);
defer allocator.free(decoded);printToPDF
Generate PDF of the page.
pub fn printToPDF(
self: *Page,
allocator: Allocator,
params: PrintToPDFParams,
) ![]const u8Parameters:
| Field | Type | Description |
|---|---|---|
landscape | ?bool | Landscape orientation |
print_background | ?bool | Print backgrounds |
scale | ?f64 | Scale factor |
paper_width | ?f64 | Width in inches |
paper_height | ?f64 | Height in inches |
margin_top/bottom/left/right | ?f64 | Margins in inches |
page_ranges | ?[]const u8 | e.g., "1-5, 8" |
header_template | ?[]const u8 | Header HTML |
footer_template | ?[]const u8 | Footer HTML |
Returns: Base64-encoded PDF data
Example:
const pdf = try page.printToPDF(allocator, .{
.landscape = false,
.print_background = true,
.margin_top = 0.5,
.margin_bottom = 0.5,
});
defer allocator.free(pdf);getMainFrame
Get the main frame information.
pub fn getMainFrame(self: *Page, allocator: Allocator) !FramesetDocumentContent
Set the page HTML content.
pub fn setDocumentContent(self: *Page, html: []const u8) !voidExample:
try page.setDocumentContent("<html><body><h1>Hello</h1></body></html>");createIsolatedWorld
Create an isolated JavaScript execution context for a frame. Useful for running scripts without interfering with the page's context.
pub fn createIsolatedWorld(self: *Page, allocator: Allocator, frame_id: FrameId, world_name: ?[]const u8) !i64Parameters:
frame_id- ID of the frame to create the world inworld_name- Optional name for the world (for debugging)
Returns: Execution context ID for use with Runtime.evaluate
Example:
const frame = try page.getMainFrame(allocator);
defer {
var f = frame;
f.deinit(allocator);
}
const context_id = try page.createIsolatedWorld(allocator, frame.id, "MyIsolatedWorld");
// Use the context for evaluation
var runtime = cdp.Runtime.init(session);
_ = try runtime.evaluate(allocator, "console.log('In isolated world')", .{
.context_id = context_id,
});getAllFrames
Get all frames in the frame tree (including iframes).
pub fn getAllFrames(self: *Page, allocator: Allocator) ![]FrameReturns: Slice of Frame (caller must deinit each and free slice)
Example:
const frames = try page.getAllFrames(allocator);
defer {
for (frames) |*f| f.deinit(allocator);
allocator.free(frames);
}
for (frames) |frame| {
std.debug.print("Frame: {s} - {s}\n", .{frame.id, frame.url});
if (frame.parent_id) |parent| {
std.debug.print(" Parent: {s}\n", .{parent});
}
}bringToFront
Bring the page to front.
pub fn bringToFront(self: *Page) !voidsetLifecycleEventsEnabled
Enable lifecycle events.
pub fn setLifecycleEventsEnabled(self: *Page, enabled: bool) !voidaddScriptToEvaluateOnNewDocument
Add script to run on every new document.
pub fn addScriptToEvaluateOnNewDocument(self: *Page, source: []const u8) ![]const u8Returns: Script identifier
removeScriptToEvaluateOnNewDocument
Remove an injected script.
pub fn removeScriptToEvaluateOnNewDocument(self: *Page, identifier: []const u8) !voidTypes
NavigateResult
pub const NavigateResult = struct {
frame_id: []const u8,
loader_id: ?[]const u8 = null,
error_text: ?[]const u8 = null,
pub fn deinit(self: *NavigateResult, allocator: Allocator) void;
};ScreenshotFormat
pub const ScreenshotFormat = enum {
jpeg,
png,
webp,
};Viewport
pub const Viewport = struct {
x: f64,
y: f64,
width: f64,
height: f64,
scale: f64 = 1.0,
};Frame
pub const Frame = struct {
id: []const u8,
parent_id: ?[]const u8 = null,
loader_id: []const u8,
name: ?[]const u8 = null,
url: []const u8,
security_origin: ?[]const u8 = null,
mime_type: ?[]const u8 = null,
pub fn deinit(self: *Frame, allocator: Allocator) void;
};Events
| Event | Description |
|---|---|
LoadEventFired | Page load completed |
DomContentEventFired | DOM content loaded |
FrameNavigated | Frame navigation completed |
FrameStartedLoading | Frame started loading |
FrameStoppedLoading | Frame stopped loading |
LifecycleEvent | Lifecycle event fired |
Complete Example
const std = @import("std");
const cdp = @import("cdp");
pub fn main(init: std.process.Init) !void {
const allocator = init.gpa;
var browser = try cdp.Browser.launch(.{
.headless = .new,
.allocator = allocator,
.io = init.io,
});
defer browser.close();
var session = try browser.newPage();
defer session.detach() catch {};
var page = cdp.Page.init(session);
try page.enable();
// Navigate
var result = try page.navigate(allocator, "https://example.com");
defer result.deinit(allocator);
// Wait for load (simplified)
var i: u32 = 0;
while (i < 500000) : (i += 1) {
std.atomic.spinLoopHint();
}
// Screenshot
const screenshot = try page.captureScreenshot(allocator, .{ .format = .png });
defer allocator.free(screenshot);
// PDF
const pdf = try page.printToPDF(allocator, .{ .print_background = true });
defer allocator.free(pdf);
std.debug.print("Screenshot: {} bytes, PDF: {} bytes\n", .{
screenshot.len, pdf.len,
});
}