Compare commits

...

15 Commits

Author SHA1 Message Date
Pierre Tachoire
68e2140bb3 Add HTMLElement.dir and HTMLElement.lang properties
Added as attribute-backed accessors on HTMLElement (inherited by all
HTML elements) and on HTMLDocument (delegates to documentElement).
2026-03-31 16:12:26 +02:00
Karl Seguin
20cadd2282 Merge pull request #2053 from lightpanda-io/remove_dead_code
remove dead code
2026-03-31 18:24:48 +08:00
Karl Seguin
4c4f395b1e Merge pull request #2036 from lightpanda-io/remove_cdp_generics
Removing remaining CDP generic
2026-03-31 17:54:34 +08:00
Karl Seguin
26653120fa Removing remaining CDP generic
Follow up to https://github.com/lightpanda-io/browser/pull/1990 which makes
both BrowserContext and Command non-generic.
2026-03-31 16:53:58 +08:00
Karl Seguin
a4a024ce9c remove dead code 2026-03-31 16:48:47 +08:00
Adrià Arrufat
55666ab7cd Merge pull request #2049 from lightpanda-io/refactor/getNodeDetails-param-order
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
SemanticTree: reorder getNodeDetails params
2026-03-31 07:58:28 +02:00
Adrià Arrufat
008235222b SemanticTree: reorder getNodeDetails params 2026-03-31 07:29:33 +02:00
Adrià Arrufat
b03dbc77ab Merge pull request #2048 from lightpanda-io/feature/semantic-tree-checked-state
SemanticTree: add checked state to node data and output
2026-03-31 07:08:36 +02:00
Adrià Arrufat
fc057a3bb3 SemanticTree: add checked state to node data and output 2026-03-31 06:43:01 +02:00
Adrià Arrufat
16f17ead9a Merge pull request #2045 from lightpanda-io/semantic-tree-node-details
SemanticTree: Add nodeDetails tool
2026-03-31 05:28:24 +02:00
Adrià Arrufat
367d20d39f SemanticTree: simplify lp.String.wrap calls 2026-03-31 05:20:32 +02:00
Karl Seguin
a6a8752787 Merge pull request #2032 from lightpanda-io/cdp_navigation_event_order
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
Improve/Fix CDP navigation event order
2026-03-31 07:12:14 +08:00
Karl Seguin
568fa25add Remove DOMContentLoaded and Loaded events from page_navigated
These were moved to their own distinct events, and should have been removed from
here.
2026-03-31 06:56:01 +08:00
Karl Seguin
752184b12b Improve/Fix CDP navigation event order
These changes all better align with chrome's event ordering/timing.

There are two big changes. The first is that our internal page_navigated event,
which is kind of our heavy hitter, is sent once the header is received as
opposed to (much later) on document load. The main goal of this internal event
is to trigger the "Page.frameNavigated" CDP event which is meant to happen
once the URL is committed, which _is_ on header response.

To accommodate this earlier trigger, new explicit events for DOMContentLoaded
and load have be added.

This drastically changes the flow of events as things go from:
Start Page Navigation
Response Received
  Start Frame Navigation
  Response Received
  End Frame Navigation
End Page Navigation
context clear + reset
DOMContentLoaded
Loaded

TO:
Start Page Navigation
Response Received
End Page Navigation
context clear + reset
Start Frame Navigation
Response Received
End Frame Navigation
DOMContentLoaded
Loaded

So not only does it remove the nesting, but it ensures that the context are
cleared and reset once the main page's navigation is locked in, and before any
frame is created.
2026-03-31 06:56:00 +08:00
Adrià Arrufat
9c8fe9b20f SemanticTree: Add nodeDetails tool
Adds a tool to retrieve detailed node metadata and updates the
semantic tree to track and display the disabled state of elements.
2026-03-30 16:38:23 +02:00
28 changed files with 1087 additions and 661 deletions

View File

@@ -74,6 +74,8 @@ const EventListeners = struct {
page_network_idle: List = .{},
page_network_almost_idle: List = .{},
page_frame_created: List = .{},
page_dom_content_loaded: List = .{},
page_loaded: List = .{},
http_request_fail: List = .{},
http_request_start: List = .{},
http_request_intercept: List = .{},
@@ -91,6 +93,8 @@ const Events = union(enum) {
page_network_idle: *const PageNetworkIdle,
page_network_almost_idle: *const PageNetworkAlmostIdle,
page_frame_created: *const PageFrameCreated,
page_dom_content_loaded: *const PageDOMContentLoaded,
page_loaded: *const PageLoaded,
http_request_fail: *const RequestFail,
http_request_start: *const RequestStart,
http_request_intercept: *const RequestIntercept,
@@ -137,6 +141,18 @@ pub const PageFrameCreated = struct {
timestamp: u64,
};
pub const PageDOMContentLoaded = struct {
req_id: u32,
frame_id: u32,
timestamp: u64,
};
pub const PageLoaded = struct {
req_id: u32,
frame_id: u32,
timestamp: u64,
};
pub const RequestStart = struct {
transfer: *Transfer,
};

View File

@@ -95,9 +95,11 @@ const NodeData = struct {
name: ?[]const u8,
value: ?[]const u8,
options: ?[]OptionData = null,
checked: ?bool = null,
xpath: []const u8,
is_interactive: bool,
node_name: []const u8,
interactive: bool,
disabled: bool,
tag_name: []const u8,
};
const WalkContext = struct {
@@ -148,16 +150,21 @@ fn walk(
const role = try axn.getRole();
var is_interactive = false;
var is_disabled = false;
var value: ?[]const u8 = null;
var options: ?[]OptionData = null;
var node_name: []const u8 = "text";
var checked: ?bool = null;
var tag_name: []const u8 = "text";
if (node.is(Element)) |el| {
node_name = el.getTagNameLower();
tag_name = el.getTagNameLower();
if (el.is(Element.Html.Input)) |input| {
value = input.getValue();
if (el.getAttributeSafe(comptime lp.String.wrap("list"))) |list_id| {
if (input._input_type == .checkbox or input._input_type == .radio) {
checked = input.getChecked();
}
if (el.getAttributeSafe(comptime .wrap("list"))) |list_id| {
options = try extractDataListOptions(list_id, self.page, self.arena);
}
} else if (el.is(Element.Html.TextArea)) |textarea| {
@@ -172,8 +179,10 @@ fn walk(
is_interactive = true;
}
}
is_disabled = el.isDisabled();
} else if (node._type == .document or node._type == .document_fragment) {
node_name = "root";
tag_name = "root";
}
const initial_xpath_len = ctx.xpath_buffer.items.len;
@@ -234,9 +243,11 @@ fn walk(
.name = name,
.value = value,
.options = options,
.checked = checked,
.xpath = xpath,
.is_interactive = is_interactive,
.node_name = node_name,
.interactive = is_interactive,
.disabled = is_disabled,
.tag_name = tag_name,
};
if (should_visit) {
@@ -335,7 +346,7 @@ const JsonVisitor = struct {
try self.jw.write(data.id);
try self.jw.objectField("nodeName");
try self.jw.write(data.node_name);
try self.jw.write(data.tag_name);
try self.jw.objectField("xpath");
try self.jw.write(data.xpath);
@@ -345,7 +356,12 @@ const JsonVisitor = struct {
try self.jw.write(1);
try self.jw.objectField("isInteractive");
try self.jw.write(data.is_interactive);
try self.jw.write(data.interactive);
if (data.disabled) {
try self.jw.objectField("isDisabled");
try self.jw.write(true);
}
try self.jw.objectField("role");
try self.jw.write(data.role);
@@ -373,6 +389,11 @@ const JsonVisitor = struct {
try self.jw.endObject();
}
if (data.checked) |checked| {
try self.jw.objectField("checked");
try self.jw.write(checked);
}
if (data.options) |options| {
try self.jw.objectField("options");
try self.jw.beginArray();
@@ -459,6 +480,9 @@ const TextVisitor = struct {
const is_text_only = std.mem.eql(u8, data.role, "StaticText") or std.mem.eql(u8, data.role, "none") or std.mem.eql(u8, data.role, "generic");
try self.writer.print("{d}", .{data.id});
if (data.interactive) {
try self.writer.writeAll(if (data.disabled) " [i:disabled]" else " [i]");
}
if (!is_text_only) {
try self.writer.print(" {s}", .{data.role});
}
@@ -472,6 +496,14 @@ const TextVisitor = struct {
}
}
if (data.checked) |c| {
if (c) {
try self.writer.writeAll(" [checked]");
} else {
try self.writer.writeAll(" [unchecked]");
}
}
if (data.options) |options| {
try self.writer.writeAll(" options=[");
for (options, 0..) |opt, i| {
@@ -509,6 +541,182 @@ const TextVisitor = struct {
}
};
pub const NodeDetails = struct {
backendNodeId: CDPNode.Id,
tag_name: []const u8,
role: []const u8,
name: ?[]const u8,
interactive: bool,
disabled: bool,
value: ?[]const u8 = null,
input_type: ?[]const u8 = null,
placeholder: ?[]const u8 = null,
href: ?[]const u8 = null,
id: ?[]const u8 = null,
class: ?[]const u8 = null,
checked: ?bool = null,
options: ?[]OptionData = null,
pub fn jsonStringify(self: *const NodeDetails, jw: anytype) !void {
try jw.beginObject();
try jw.objectField("backendNodeId");
try jw.write(self.backendNodeId);
try jw.objectField("tagName");
try jw.write(self.tag_name);
try jw.objectField("role");
try jw.write(self.role);
if (self.name) |n| {
try jw.objectField("name");
try jw.write(n);
}
try jw.objectField("isInteractive");
try jw.write(self.interactive);
if (self.disabled) {
try jw.objectField("isDisabled");
try jw.write(true);
}
if (self.value) |v| {
try jw.objectField("value");
try jw.write(v);
}
if (self.input_type) |v| {
try jw.objectField("inputType");
try jw.write(v);
}
if (self.placeholder) |v| {
try jw.objectField("placeholder");
try jw.write(v);
}
if (self.href) |v| {
try jw.objectField("href");
try jw.write(v);
}
if (self.id) |v| {
try jw.objectField("id");
try jw.write(v);
}
if (self.class) |v| {
try jw.objectField("class");
try jw.write(v);
}
if (self.checked) |c| {
try jw.objectField("checked");
try jw.write(c);
}
if (self.options) |opts| {
try jw.objectField("options");
try jw.beginArray();
for (opts) |opt| {
try jw.beginObject();
try jw.objectField("value");
try jw.write(opt.value);
try jw.objectField("text");
try jw.write(opt.text);
if (opt.selected) {
try jw.objectField("selected");
try jw.write(true);
}
try jw.endObject();
}
try jw.endArray();
}
try jw.endObject();
}
};
pub fn getNodeDetails(
arena: std.mem.Allocator,
node: *Node,
registry: *CDPNode.Registry,
page: *Page,
) !NodeDetails {
const cdp_node = try registry.register(node);
const axn = AXNode.fromNode(node);
const role = try axn.getRole();
const name = try axn.getName(page, arena);
var is_interactive = false;
var is_disabled = false;
var tag_name: []const u8 = "text";
var value: ?[]const u8 = null;
var input_type: ?[]const u8 = null;
var placeholder: ?[]const u8 = null;
var href: ?[]const u8 = null;
var id_attr: ?[]const u8 = null;
var class_attr: ?[]const u8 = null;
var checked: ?bool = null;
var options: ?[]OptionData = null;
if (node.is(Element)) |el| {
tag_name = el.getTagNameLower();
is_disabled = el.isDisabled();
id_attr = el.getAttributeSafe(comptime .wrap("id"));
class_attr = el.getAttributeSafe(comptime .wrap("class"));
placeholder = el.getAttributeSafe(comptime .wrap("placeholder"));
if (el.getAttributeSafe(comptime .wrap("href"))) |h| {
const URL = lp.URL;
href = URL.resolve(arena, page.base(), h, .{ .encode = true }) catch h;
}
if (el.is(Element.Html.Input)) |input| {
value = input.getValue();
input_type = input._input_type.toString();
if (input._input_type == .checkbox or input._input_type == .radio) {
checked = input.getChecked();
}
if (el.getAttributeSafe(comptime .wrap("list"))) |list_id| {
options = try extractDataListOptions(list_id, page, arena);
}
} else if (el.is(Element.Html.TextArea)) |textarea| {
value = textarea.getValue();
} else if (el.is(Element.Html.Select)) |select| {
value = select.getValue(page);
options = try extractSelectOptions(el.asNode(), page, arena);
}
if (el.is(Element.Html)) |html_el| {
const listener_targets = try interactive.buildListenerTargetMap(page, arena);
var pointer_events_cache: Element.PointerEventsCache = .empty;
if (interactive.classifyInteractivity(page, el, html_el, listener_targets, &pointer_events_cache) != null) {
is_interactive = true;
}
}
}
return .{
.backendNodeId = cdp_node.id,
.tag_name = tag_name,
.role = role,
.name = name,
.interactive = is_interactive,
.disabled = is_disabled,
.value = value,
.input_type = input_type,
.placeholder = placeholder,
.href = href,
.id = id_attr,
.class = class_attr,
.checked = checked,
.options = options,
};
}
const testing = @import("testing.zig");
test "SemanticTree backendDOMNodeId" {

View File

@@ -1348,15 +1348,15 @@ pub const Transfer = struct {
}
}
transfer.req.notification.dispatch(.http_response_header_done, &.{
.transfer = transfer,
});
const proceed = transfer.req.header_callback(transfer) catch |err| {
log.err(.http, "header_callback", .{ .err = err, .req = transfer });
return err;
};
transfer.req.notification.dispatch(.http_response_header_done, &.{
.transfer = transfer,
});
return proceed and transfer.aborted == false;
}

View File

@@ -487,7 +487,6 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi
return error.InjectBlankFailed;
};
}
self.documentIsComplete();
session.notification.dispatch(.page_navigate, &.{
.frame_id = self._frame_id,
@@ -519,6 +518,8 @@ pub fn navigate(self: *Page, request_url: [:0]const u8, opts: NavigateOpts) !voi
// force next request id manually b/c we won't create a real req.
_ = session.browser.http_client.incrReqId();
self.documentIsComplete();
return;
}
@@ -738,6 +739,12 @@ pub fn _documentIsLoaded(self: *Page) !void {
self.document.asEventTarget(),
event,
);
self._session.notification.dispatch(.page_dom_content_loaded, &.{
.frame_id = self._frame_id,
.req_id = self._req_id,
.timestamp = timestamp(.monotonic),
});
}
pub fn scriptsCompletedLoading(self: *Page) void {
@@ -796,19 +803,6 @@ pub fn documentIsComplete(self: *Page) void {
self._documentIsComplete() catch |err| {
log.err(.page, "document is complete", .{ .err = err, .type = self._type, .url = self.url });
};
if (self._navigated_options) |no| {
// _navigated_options will be null in special short-circuit cases, like
// "navigating" to about:blank, in which case this notification has
// already been sent
self._session.notification.dispatch(.page_navigated, &.{
.frame_id = self._frame_id,
.req_id = self._req_id,
.opts = no,
.url = self.url,
.timestamp = timestamp(.monotonic),
});
}
}
fn _documentIsComplete(self: *Page) !void {
@@ -827,6 +821,12 @@ fn _documentIsComplete(self: *Page) !void {
try self._event_manager.dispatchDirect(window_target, event, self.window._on_load, .{ .inject_target = false, .context = "page load" });
}
self._session.notification.dispatch(.page_loaded, &.{
.frame_id = self._frame_id,
.req_id = self._req_id,
.timestamp = timestamp(.monotonic),
});
if (self._event_manager.hasDirectListeners(window_target, "pageshow", self.window._on_pageshow)) {
const pageshow_event = (try PageTransitionEvent.initTrusted(comptime .wrap("pageshow"), .{}, self)).asEvent();
try self._event_manager.dispatchDirect(window_target, pageshow_event, self.window._on_pageshow, .{ .context = "page show" });
@@ -879,6 +879,19 @@ fn pageHeaderDoneCallback(transfer: *HttpClient.Transfer) !bool {
});
}
if (self._navigated_options) |no| {
// _navigated_options will be null in special short-circuit cases, like
// "navigating" to about:blank, in which case this notification has
// already been sent
self._session.notification.dispatch(.page_navigated, &.{
.frame_id = self._frame_id,
.req_id = self._req_id,
.opts = no,
.url = self.url,
.timestamp = timestamp(.monotonic),
});
}
return true;
}

View File

@@ -104,18 +104,6 @@ pub fn Builder(comptime T: type) type {
};
}
fn releaseRef(comptime T: type, ptr_id: usize, session: *Session) void {
if (@hasDecl(T, "releaseRef")) {
T.releaseRef(@ptrFromInt(ptr_id), session);
return;
}
if (@hasField(T, "_proto")) {
releaseRef(Struct(std.meta.fieldInfo(T, ._proto).type), ptr_id, session);
return;
}
@compileError(@typeName(T) ++ " marked with finalizer without an acquireRef in its prototype chain");
}
pub const Constructor = struct {
func: *const fn (?*const v8.FunctionCallbackInfo) callconv(.c) void,

View File

@@ -0,0 +1,41 @@
<html lang="en" dir="ltr">
<head>
<script src="../testing.js"></script>
</head>
<body>
<script id="test-document-dir-lang">
// HTMLElement.dir and HTMLElement.lang
testing.expectEqual('ltr', document.documentElement.dir);
testing.expectEqual('en', document.documentElement.lang);
// Document.dir and Document.lang delegate to documentElement
testing.expectEqual('ltr', document.dir);
testing.expectEqual('en', document.lang);
// Setting via document updates the documentElement attribute
document.dir = 'rtl';
testing.expectEqual('rtl', document.documentElement.dir);
testing.expectEqual('rtl', document.documentElement.getAttribute('dir'));
document.lang = 'fr';
testing.expectEqual('fr', document.documentElement.lang);
testing.expectEqual('fr', document.documentElement.getAttribute('lang'));
// Setting via element is reflected in document
document.documentElement.dir = 'ltr';
testing.expectEqual('ltr', document.dir);
document.documentElement.lang = 'de';
testing.expectEqual('de', document.lang);
// div elements also have dir and lang
const div = document.createElement('div');
testing.expectEqual('', div.dir);
testing.expectEqual('', div.lang);
div.dir = 'rtl';
div.lang = 'ar';
testing.expectEqual('rtl', div.dir);
testing.expectEqual('ar', div.lang);
</script>
</body>
</html>

View File

@@ -182,6 +182,30 @@ pub fn setLocation(self: *HTMLDocument, url: [:0]const u8, page: *Page) !void {
return page.scheduleNavigation(url, .{ .reason = .script, .kind = .{ .push = null } }, .{ .script = self._proto._page });
}
pub fn getDir(self: *HTMLDocument) []const u8 {
const el = self._proto.getDocumentElement() orelse return "";
const html = el.is(Element.Html) orelse return "";
return html.getDir();
}
pub fn setDir(self: *HTMLDocument, value: []const u8, page: *Page) !void {
const el = self._proto.getDocumentElement() orelse return;
const html = el.is(Element.Html) orelse return;
try html.setDir(value, page);
}
pub fn getLang(self: *HTMLDocument) []const u8 {
const el = self._proto.getDocumentElement() orelse return "";
const html = el.is(Element.Html) orelse return "";
return html.getLang();
}
pub fn setLang(self: *HTMLDocument, value: []const u8, page: *Page) !void {
const el = self._proto.getDocumentElement() orelse return;
const html = el.is(Element.Html) orelse return;
try html.setLang(value, page);
}
pub fn getAll(self: *HTMLDocument, page: *Page) !*collections.HTMLAllCollection {
return page._factory.create(collections.HTMLAllCollection.init(self.asNode(), page));
}
@@ -250,8 +274,10 @@ pub const JsApi = struct {
});
}
pub const dir = bridge.accessor(HTMLDocument.getDir, HTMLDocument.setDir, .{});
pub const head = bridge.accessor(HTMLDocument.getHead, null, .{});
pub const body = bridge.accessor(HTMLDocument.getBody, null, .{});
pub const lang = bridge.accessor(HTMLDocument.getLang, HTMLDocument.setLang, .{});
pub const title = bridge.accessor(HTMLDocument.getTitle, HTMLDocument.setTitle, .{});
pub const images = bridge.accessor(HTMLDocument.getImages, null, .{});
pub const scripts = bridge.accessor(HTMLDocument.getScripts, null, .{});

View File

@@ -375,6 +375,22 @@ pub fn setTabIndex(self: *HtmlElement, value: i32, page: *Page) !void {
try self.asElement().setAttributeSafe(comptime .wrap("tabindex"), .wrap(str), page);
}
pub fn getDir(self: *HtmlElement) []const u8 {
return self.asElement().getAttributeSafe(comptime .wrap("dir")) orelse "";
}
pub fn setDir(self: *HtmlElement, value: []const u8, page: *Page) !void {
try self.asElement().setAttributeSafe(comptime .wrap("dir"), .wrap(value), page);
}
pub fn getLang(self: *HtmlElement) []const u8 {
return self.asElement().getAttributeSafe(comptime .wrap("lang")) orelse "";
}
pub fn setLang(self: *HtmlElement, value: []const u8, page: *Page) !void {
try self.asElement().setAttributeSafe(comptime .wrap("lang"), .wrap(value), page);
}
pub fn getAttributeFunction(
self: *HtmlElement,
listener_type: GlobalEventHandler,
@@ -1211,7 +1227,9 @@ pub const JsApi = struct {
pub const insertAdjacentHTML = bridge.function(HtmlElement.insertAdjacentHTML, .{ .dom_exception = true });
pub const click = bridge.function(HtmlElement.click, .{});
pub const dir = bridge.accessor(HtmlElement.getDir, HtmlElement.setDir, .{});
pub const hidden = bridge.accessor(HtmlElement.getHidden, HtmlElement.setHidden, .{});
pub const lang = bridge.accessor(HtmlElement.getLang, HtmlElement.setLang, .{});
pub const tabIndex = bridge.accessor(HtmlElement.getTabIndex, HtmlElement.setTabIndex, .{});
pub const onabort = bridge.accessor(HtmlElement.getOnAbort, HtmlElement.setOnAbort, .{});

File diff suppressed because it is too large Load Diff

View File

@@ -18,8 +18,9 @@
const std = @import("std");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,
@@ -32,15 +33,15 @@ pub fn processMessage(cmd: anytype) !void {
.getFullAXTree => return getFullAXTree(cmd),
}
}
fn enable(cmd: anytype) !void {
fn enable(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn disable(cmd: anytype) !void {
fn disable(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn getFullAXTree(cmd: anytype) !void {
fn getFullAXTree(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
depth: ?i32 = null,
frameId: ?[]const u8 = null,

View File

@@ -17,6 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
// TODO: hard coded data
const PROTOCOL_VERSION = "1.3";
@@ -35,7 +36,7 @@ const PRODUCT = "Chrome/124.0.6367.29";
const JS_VERSION = "12.4.254.8";
const DEV_TOOLS_WINDOW_ID = 1923710101;
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
getVersion,
setPermission,
@@ -57,7 +58,7 @@ pub fn processMessage(cmd: anytype) !void {
}
}
fn getVersion(cmd: anytype) !void {
fn getVersion(cmd: *CDP.Command) !void {
// TODO: pre-serialize?
return cmd.sendResult(.{
.protocolVersion = PROTOCOL_VERSION,
@@ -69,7 +70,7 @@ fn getVersion(cmd: anytype) !void {
}
// TODO: noop method
fn setDownloadBehavior(cmd: anytype) !void {
fn setDownloadBehavior(cmd: *CDP.Command) !void {
// const params = (try cmd.params(struct {
// behavior: []const u8,
// browserContextId: ?[]const u8 = null,
@@ -80,7 +81,7 @@ fn setDownloadBehavior(cmd: anytype) !void {
return cmd.sendResult(null, .{ .include_session_id = false });
}
fn getWindowForTarget(cmd: anytype) !void {
fn getWindowForTarget(cmd: *CDP.Command) !void {
// const params = (try cmd.params(struct {
// targetId: ?[]const u8 = null,
// })) orelse return error.InvalidParams;
@@ -91,22 +92,22 @@ fn getWindowForTarget(cmd: anytype) !void {
}
// TODO: noop method
fn setWindowBounds(cmd: anytype) !void {
fn setWindowBounds(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
// TODO: noop method
fn grantPermissions(cmd: anytype) !void {
fn grantPermissions(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
// TODO: noop method
fn setPermission(cmd: anytype) !void {
fn setPermission(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
// TODO: noop method
fn resetPermissions(cmd: anytype) !void {
fn resetPermissions(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}

View File

@@ -17,8 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
enable,
}, cmd.input.action) orelse return error.UnknownMethod;

View File

@@ -18,17 +18,18 @@
const std = @import("std");
const id = @import("../id.zig");
const log = @import("../../log.zig");
const CDP = @import("../CDP.zig");
const Node = @import("../Node.zig");
const log = @import("../../log.zig");
const dump = @import("../../browser/dump.zig");
const js = @import("../../browser/js/js.zig");
const DOMNode = @import("../../browser/webapi/Node.zig");
const Selector = @import("../../browser/webapi/selector/Selector.zig");
const dump = @import("../../browser/dump.zig");
const js = @import("../../browser/js/js.zig");
const Allocator = std.mem.Allocator;
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
enable,
getDocument,
@@ -69,7 +70,7 @@ pub fn processMessage(cmd: anytype) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getDocument
fn getDocument(cmd: anytype) !void {
fn getDocument(cmd: *CDP.Command) !void {
const Params = struct {
// CDP documentation implies that 0 isn't valid, but it _does_ work in Chrome
depth: i32 = 3,
@@ -89,7 +90,7 @@ fn getDocument(cmd: anytype) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-performSearch
fn performSearch(cmd: anytype) !void {
fn performSearch(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
query: []const u8,
includeUserAgentShadowDOM: ?bool = null,
@@ -116,7 +117,7 @@ fn performSearch(cmd: anytype) !void {
// hierarchy of each nodes.
// We dispatch event in the reverse order: from the top level to the direct parents.
// We should dispatch a node only if it has never been sent.
fn dispatchSetChildNodes(cmd: anytype, dom_nodes: []const *DOMNode) !void {
fn dispatchSetChildNodes(cmd: *CDP.Command, dom_nodes: []const *DOMNode) !void {
const arena = cmd.arena;
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const session_id = bc.session_id orelse return error.SessionIdNotLoaded;
@@ -172,7 +173,7 @@ fn dispatchSetChildNodes(cmd: anytype, dom_nodes: []const *DOMNode) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-discardSearchResults
fn discardSearchResults(cmd: anytype) !void {
fn discardSearchResults(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
searchId: []const u8,
})) orelse return error.InvalidParams;
@@ -184,7 +185,7 @@ fn discardSearchResults(cmd: anytype) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getSearchResults
fn getSearchResults(cmd: anytype) !void {
fn getSearchResults(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
searchId: []const u8,
fromIndex: u32,
@@ -209,7 +210,7 @@ fn getSearchResults(cmd: anytype) !void {
return cmd.sendResult(.{ .nodeIds = node_ids[params.fromIndex..params.toIndex] }, .{});
}
fn querySelector(cmd: anytype) !void {
fn querySelector(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
nodeId: Node.Id,
selector: []const u8,
@@ -235,7 +236,7 @@ fn querySelector(cmd: anytype) !void {
}, .{});
}
fn querySelectorAll(cmd: anytype) !void {
fn querySelectorAll(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
nodeId: Node.Id,
selector: []const u8,
@@ -266,7 +267,7 @@ fn querySelectorAll(cmd: anytype) !void {
}, .{});
}
fn resolveNode(cmd: anytype) !void {
fn resolveNode(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?u32 = null,
@@ -327,7 +328,7 @@ fn resolveNode(cmd: anytype) !void {
} }, .{});
}
fn describeNode(cmd: anytype) !void {
fn describeNode(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?Node.Id = null,
@@ -374,7 +375,7 @@ fn rectToQuad(rect: DOMNode.Element.DOMRect) Quad {
};
}
fn scrollIntoViewIfNeeded(cmd: anytype) !void {
fn scrollIntoViewIfNeeded(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?u32 = null,
@@ -397,7 +398,7 @@ fn scrollIntoViewIfNeeded(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn getNode(arena: Allocator, bc: anytype, node_id: ?Node.Id, backend_node_id: ?Node.Id, object_id: ?[]const u8) !*Node {
fn getNode(arena: Allocator, bc: *CDP.BrowserContext, node_id: ?Node.Id, backend_node_id: ?Node.Id, object_id: ?[]const u8) !*Node {
const input_node_id = node_id orelse backend_node_id;
if (input_node_id) |input_node_id_| {
return bc.node_registry.lookup_by_id.get(input_node_id_) orelse return error.NodeNotFound;
@@ -417,7 +418,7 @@ fn getNode(arena: Allocator, bc: anytype, node_id: ?Node.Id, backend_node_id: ?N
// https://chromedevtools.github.io/devtools-protocol/tot/DOM/#method-getContentQuads
// Related to: https://drafts.csswg.org/cssom-view/#the-geometryutils-interface
fn getContentQuads(cmd: anytype) !void {
fn getContentQuads(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?Node.Id = null,
@@ -443,7 +444,7 @@ fn getContentQuads(cmd: anytype) !void {
return cmd.sendResult(.{ .quads = &.{quad} }, .{});
}
fn getBoxModel(cmd: anytype) !void {
fn getBoxModel(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?u32 = null,
@@ -472,7 +473,7 @@ fn getBoxModel(cmd: anytype) !void {
} }, .{});
}
fn requestChildNodes(cmd: anytype) !void {
fn requestChildNodes(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
nodeId: Node.Id,
depth: i32 = 1,
@@ -496,7 +497,7 @@ fn requestChildNodes(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn getFrameOwner(cmd: anytype) !void {
fn getFrameOwner(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
frameId: []const u8,
})) orelse return error.InvalidParams;
@@ -512,7 +513,7 @@ fn getFrameOwner(cmd: anytype) !void {
return cmd.sendResult(.{ .nodeId = node.id, .backendNodeId = node.id }, .{});
}
fn getOuterHTML(cmd: anytype) !void {
fn getOuterHTML(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
nodeId: ?Node.Id = null,
backendNodeId: ?Node.Id = null,
@@ -534,7 +535,7 @@ fn getOuterHTML(cmd: anytype) !void {
return cmd.sendResult(.{ .outerHTML = aw.written() }, .{});
}
fn requestNode(cmd: anytype) !void {
fn requestNode(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
objectId: []const u8,
})) orelse return error.InvalidParams;

View File

@@ -17,9 +17,10 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
const log = @import("../../log.zig");
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
setEmulatedMedia,
setFocusEmulationEnabled,
@@ -38,7 +39,7 @@ pub fn processMessage(cmd: anytype) !void {
}
// TODO: noop method
fn setEmulatedMedia(cmd: anytype) !void {
fn setEmulatedMedia(cmd: *CDP.Command) !void {
// const input = (try const incoming.params(struct {
// media: ?[]const u8 = null,
// features: ?[]struct{
@@ -51,7 +52,7 @@ fn setEmulatedMedia(cmd: anytype) !void {
}
// TODO: noop method
fn setFocusEmulationEnabled(cmd: anytype) !void {
fn setFocusEmulationEnabled(cmd: *CDP.Command) !void {
// const input = (try const incoming.params(struct {
// enabled: bool,
// })) orelse return error.InvalidParams;
@@ -59,16 +60,16 @@ fn setFocusEmulationEnabled(cmd: anytype) !void {
}
// TODO: noop method
fn setDeviceMetricsOverride(cmd: anytype) !void {
fn setDeviceMetricsOverride(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
// TODO: noop method
fn setTouchEmulationEnabled(cmd: anytype) !void {
fn setTouchEmulationEnabled(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn setUserAgentOverride(cmd: anytype) !void {
fn setUserAgentOverride(cmd: *CDP.Command) !void {
log.info(.app, "setUserAgentOverride ignored", .{});
return cmd.sendResult(null, .{});
}

View File

@@ -17,17 +17,19 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Allocator = std.mem.Allocator;
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
const log = @import("../../log.zig");
const network = @import("network.zig");
const HttpClient = @import("../../browser/HttpClient.zig");
const net_http = @import("../../network/http.zig");
const Notification = @import("../../Notification.zig");
pub fn processMessage(cmd: anytype) !void {
const network = @import("network.zig");
const Allocator = std.mem.Allocator;
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
disable,
enable,
@@ -135,13 +137,13 @@ const ErrorReason = enum {
BlockedByResponse,
};
fn disable(cmd: anytype) !void {
fn disable(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
bc.fetchDisable();
return cmd.sendResult(null, .{});
}
fn enable(cmd: anytype) !void {
fn enable(cmd: *CDP.Command) !void {
const params = (try cmd.params(EnableParam)) orelse EnableParam{};
if (!arePatternsSupported(params.patterns)) {
log.warn(.not_implemented, "Fetch.enable", .{ .params = "pattern" });
@@ -180,7 +182,7 @@ fn arePatternsSupported(patterns: []RequestPattern) bool {
return true;
}
pub fn requestIntercept(bc: anytype, intercept: *const Notification.RequestIntercept) !void {
pub fn requestIntercept(bc: *CDP.BrowserContext, intercept: *const Notification.RequestIntercept) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -215,7 +217,7 @@ pub fn requestIntercept(bc: anytype, intercept: *const Notification.RequestInter
intercept.wait_for_interception.* = true;
}
fn continueRequest(cmd: anytype) !void {
fn continueRequest(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(struct {
requestId: []const u8, // INT-{d}"
@@ -275,7 +277,7 @@ const AuthChallengeResponse = enum {
ProvideCredentials,
};
fn continueWithAuth(cmd: anytype) !void {
fn continueWithAuth(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(struct {
requestId: []const u8, // "INT-{d}"
@@ -318,7 +320,7 @@ fn continueWithAuth(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn fulfillRequest(cmd: anytype) !void {
fn fulfillRequest(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(struct {
@@ -360,7 +362,7 @@ fn fulfillRequest(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn failRequest(cmd: anytype) !void {
fn failRequest(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(struct {
requestId: []const u8, // "INT-{d}"
@@ -382,7 +384,7 @@ fn failRequest(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
pub fn requestAuthRequired(bc: anytype, intercept: *const Notification.RequestAuthRequired) !void {
pub fn requestAuthRequired(bc: *CDP.BrowserContext, intercept: *const Notification.RequestAuthRequired) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;

View File

@@ -17,8 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
dispatchKeyEvent,
dispatchMouseEvent,
@@ -33,7 +34,7 @@ pub fn processMessage(cmd: anytype) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchKeyEvent
fn dispatchKeyEvent(cmd: anytype) !void {
fn dispatchKeyEvent(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
type: Type,
key: []const u8 = "",
@@ -74,7 +75,7 @@ fn dispatchKeyEvent(cmd: anytype) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchMouseEvent
fn dispatchMouseEvent(cmd: anytype) !void {
fn dispatchMouseEvent(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
x: f64,
y: f64,
@@ -104,7 +105,7 @@ fn dispatchMouseEvent(cmd: anytype) !void {
}
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-insertText
fn insertText(cmd: anytype) !void {
fn insertText(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
text: []const u8, // The text to insert
})) orelse return error.InvalidParams;

View File

@@ -17,8 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,

View File

@@ -17,8 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,

View File

@@ -18,18 +18,23 @@
const std = @import("std");
const lp = @import("lightpanda");
const CDP = @import("../CDP.zig");
const Node = @import("../Node.zig");
const DOMNode = @import("../../browser/webapi/Node.zig");
const markdown = lp.markdown;
const SemanticTree = lp.SemanticTree;
const interactive = lp.interactive;
const structured_data = lp.structured_data;
const Node = @import("../Node.zig");
const DOMNode = @import("../../browser/webapi/Node.zig");
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
getMarkdown,
getSemanticTree,
getInteractiveElements,
getNodeDetails,
getStructuredData,
detectForms,
clickNode,
@@ -42,6 +47,7 @@ pub fn processMessage(cmd: anytype) !void {
.getMarkdown => return getMarkdown(cmd),
.getSemanticTree => return getSemanticTree(cmd),
.getInteractiveElements => return getInteractiveElements(cmd),
.getNodeDetails => return getNodeDetails(cmd),
.getStructuredData => return getStructuredData(cmd),
.detectForms => return detectForms(cmd),
.clickNode => return clickNode(cmd),
@@ -141,6 +147,24 @@ fn getInteractiveElements(cmd: anytype) !void {
}, .{});
}
fn getNodeDetails(cmd: anytype) !void {
const Params = struct {
backendNodeId: Node.Id,
};
const params = (try cmd.params(Params)) orelse return error.InvalidParam;
const bc = cmd.browser_context orelse return error.NoBrowserContext;
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
const node = (bc.node_registry.lookup_by_id.get(params.backendNodeId) orelse return error.InvalidNodeId).dom;
const details = SemanticTree.getNodeDetails(cmd.arena, node, &bc.node_registry, page) catch return error.InternalError;
return cmd.sendResult(.{
.nodeDetails = details,
}, .{});
}
fn getStructuredData(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.NoBrowserContext;
const page = bc.session.currentPage() orelse return error.PageNotLoaded;

View File

@@ -18,18 +18,21 @@
const std = @import("std");
const lp = @import("lightpanda");
const Allocator = std.mem.Allocator;
const log = @import("../../log.zig");
const CdpStorage = @import("storage.zig");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
const URL = @import("../../browser/URL.zig");
const Transfer = @import("../../browser/HttpClient.zig").Transfer;
const Notification = @import("../../Notification.zig");
const Mime = @import("../../browser/Mime.zig");
pub fn processMessage(cmd: anytype) !void {
const CdpStorage = @import("storage.zig");
const Allocator = std.mem.Allocator;
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,
@@ -59,19 +62,19 @@ pub fn processMessage(cmd: anytype) !void {
}
}
fn enable(cmd: anytype) !void {
fn enable(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
try bc.networkEnable();
return cmd.sendResult(null, .{});
}
fn disable(cmd: anytype) !void {
fn disable(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
bc.networkDisable();
return cmd.sendResult(null, .{});
}
fn setExtraHTTPHeaders(cmd: anytype) !void {
fn setExtraHTTPHeaders(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
headers: std.json.ArrayHashMap([]const u8),
})) orelse return error.InvalidParams;
@@ -110,7 +113,7 @@ fn cookieMatches(cookie: *const Cookie, name: []const u8, domain: ?[]const u8, p
return true;
}
fn deleteCookies(cmd: anytype) !void {
fn deleteCookies(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
name: []const u8,
url: ?[:0]const u8 = null,
@@ -144,14 +147,14 @@ fn deleteCookies(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn clearBrowserCookies(cmd: anytype) !void {
fn clearBrowserCookies(cmd: *CDP.Command) !void {
if (try cmd.params(struct {}) != null) return error.InvalidParams;
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
bc.session.cookie_jar.clearRetainingCapacity();
return cmd.sendResult(null, .{});
}
fn setCookie(cmd: anytype) !void {
fn setCookie(cmd: *CDP.Command) !void {
const params = (try cmd.params(
CdpStorage.CdpCookie,
)) orelse return error.InvalidParams;
@@ -162,7 +165,7 @@ fn setCookie(cmd: anytype) !void {
try cmd.sendResult(.{ .success = true }, .{});
}
fn setCookies(cmd: anytype) !void {
fn setCookies(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
cookies: []const CdpStorage.CdpCookie,
})) orelse return error.InvalidParams;
@@ -178,7 +181,7 @@ fn setCookies(cmd: anytype) !void {
const GetCookiesParam = struct {
urls: ?[]const [:0]const u8 = null,
};
fn getCookies(cmd: anytype) !void {
fn getCookies(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(GetCookiesParam)) orelse GetCookiesParam{};
@@ -201,7 +204,7 @@ fn getCookies(cmd: anytype) !void {
try cmd.sendResult(.{ .cookies = writer }, .{});
}
fn getResponseBody(cmd: anytype) !void {
fn getResponseBody(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
requestId: []const u8, // "REQ-{d}"
})) orelse return error.InvalidParams;
@@ -227,7 +230,7 @@ fn getResponseBody(cmd: anytype) !void {
}, .{});
}
pub fn httpRequestFail(bc: anytype, msg: *const Notification.RequestFail) !void {
pub fn httpRequestFail(bc: *CDP.BrowserContext, msg: *const Notification.RequestFail) !void {
// It's possible that the request failed because we aborted when the client
// sent Target.closeTarget. In that case, bc.session_id will be cleared
// already, and we can skip sending these messages to the client.
@@ -247,7 +250,7 @@ pub fn httpRequestFail(bc: anytype, msg: *const Notification.RequestFail) !void
}, .{ .session_id = session_id });
}
pub fn httpRequestStart(bc: anytype, msg: *const Notification.RequestStart) !void {
pub fn httpRequestStart(bc: *CDP.BrowserContext, msg: *const Notification.RequestStart) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -276,7 +279,7 @@ pub fn httpRequestStart(bc: anytype, msg: *const Notification.RequestStart) !voi
}, .{ .session_id = session_id });
}
pub fn httpResponseHeaderDone(arena: Allocator, bc: anytype, msg: *const Notification.ResponseHeaderDone) !void {
pub fn httpResponseHeaderDone(arena: Allocator, bc: *CDP.BrowserContext, msg: *const Notification.ResponseHeaderDone) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -293,7 +296,7 @@ pub fn httpResponseHeaderDone(arena: Allocator, bc: anytype, msg: *const Notific
}, .{ .session_id = session_id });
}
pub fn httpRequestDone(bc: anytype, msg: *const Notification.RequestDone) !void {
pub fn httpRequestDone(bc: *CDP.BrowserContext, msg: *const Notification.RequestDone) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;

View File

@@ -1,4 +1,5 @@
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
@@ -22,6 +23,8 @@ const lp = @import("lightpanda");
const screenshot_png = @embedFile("screenshot.png");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
const log = @import("../../log.zig");
const js = @import("../../browser/js/js.zig");
const URL = @import("../../browser/URL.zig");
@@ -31,7 +34,7 @@ const Notification = @import("../../Notification.zig");
const Allocator = std.mem.Allocator;
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
enable,
getFrameTree,
@@ -78,7 +81,7 @@ const Frame = struct {
gatedAPIFeatures: [][]const u8 = &[0][]const u8{},
};
fn getFrameTree(cmd: anytype) !void {
fn getFrameTree(cmd: *CDP.Command) !void {
// Stagehand parses the response and error if we don't return a
// correct one for this call when browser context or target id are missing.
const startup = .{
@@ -108,7 +111,7 @@ fn getFrameTree(cmd: anytype) !void {
}, .{});
}
fn setLifecycleEventsEnabled(cmd: anytype) !void {
fn setLifecycleEventsEnabled(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
enabled: bool,
})) orelse return error.InvalidParams;
@@ -149,7 +152,7 @@ fn setLifecycleEventsEnabled(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn addScriptToEvaluateOnNewDocument(cmd: anytype) !void {
fn addScriptToEvaluateOnNewDocument(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
source: []const u8,
worldName: ?[]const u8 = null,
@@ -179,7 +182,7 @@ fn addScriptToEvaluateOnNewDocument(cmd: anytype) !void {
}, .{});
}
fn removeScriptToEvaluateOnNewDocument(cmd: anytype) !void {
fn removeScriptToEvaluateOnNewDocument(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
identifier: []const u8,
})) orelse return error.InvalidParams;
@@ -198,7 +201,7 @@ fn removeScriptToEvaluateOnNewDocument(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn close(cmd: anytype) !void {
fn close(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const target_id = bc.target_id orelse return error.TargetNotLoaded;
@@ -235,7 +238,7 @@ fn close(cmd: anytype) !void {
bc.target_id = null;
}
fn createIsolatedWorld(cmd: anytype) !void {
fn createIsolatedWorld(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
frameId: []const u8,
worldName: []const u8,
@@ -255,7 +258,7 @@ fn createIsolatedWorld(cmd: anytype) !void {
return cmd.sendResult(.{ .executionContextId = js_context.id }, .{});
}
fn navigate(cmd: anytype) !void {
fn navigate(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
url: [:0]const u8,
// referrer: ?[]const u8 = null,
@@ -289,7 +292,7 @@ fn navigate(cmd: anytype) !void {
});
}
fn doReload(cmd: anytype) !void {
fn doReload(cmd: *CDP.Command) !void {
const params = try cmd.params(struct {
ignoreCache: ?bool = null,
scriptToEvaluateOnLoad: ?[]const u8 = null,
@@ -319,7 +322,7 @@ fn doReload(cmd: anytype) !void {
});
}
pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void {
pub fn pageNavigate(bc: *CDP.BrowserContext, event: *const Notification.PageNavigate) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -371,7 +374,7 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void
}, .{ .session_id = session_id });
}
pub fn pageRemove(bc: anytype) !void {
pub fn pageRemove(bc: *CDP.BrowserContext) !void {
// Clear all remote object mappings to prevent stale objectIds from being used
// after the context is destroy
bc.inspector_session.inspector.resetContextGroup();
@@ -382,7 +385,7 @@ pub fn pageRemove(bc: anytype) !void {
}
}
pub fn pageCreated(bc: anytype, page: *Page) !void {
pub fn pageCreated(bc: *CDP.BrowserContext, page: *Page) !void {
_ = bc.cdp.page_arena.reset(.{ .retain_with_limit = 1024 * 512 });
for (bc.isolated_worlds.items) |isolated_world| {
@@ -394,7 +397,7 @@ pub fn pageCreated(bc: anytype, page: *Page) !void {
bc.captured_responses = .empty;
}
pub fn pageFrameCreated(bc: anytype, event: *const Notification.PageFrameCreated) !void {
pub fn pageFrameCreated(bc: *CDP.BrowserContext, event: *const Notification.PageFrameCreated) !void {
const session_id = bc.session_id orelse return;
const cdp = bc.cdp;
@@ -415,12 +418,11 @@ pub fn pageFrameCreated(bc: anytype, event: *const Notification.PageFrameCreated
}
}
pub fn pageNavigated(arena: Allocator, bc: anytype, event: *const Notification.PageNavigated) !void {
pub fn pageNavigated(arena: Allocator, bc: *CDP.BrowserContext, event: *const Notification.PageNavigated) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
const timestamp = event.timestamp;
const frame_id = &id.toFrameId(event.frame_id);
const loader_id = &id.toLoaderId(event.req_id);
@@ -472,9 +474,9 @@ pub fn pageNavigated(arena: Allocator, bc: anytype, event: *const Notification.P
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
// When we actually recreated the context we should have the inspector send
// this event, see: resetContextGroup Sending this event will tell the
// client that the context ids they had are invalid and the context shouls
// be dropped The client will expect us to send new contextCreated events,
// this event, see: resetContextGroup. Sending this event will tell the
// client that the context ids they had are invalid and the context should
// be dropped. The client will expect us to send new contextCreated events,
// such that the client has new id's for the active contexts.
// Only send executionContextsCleared for main frame navigations. For child
// frames (iframes), clearing all contexts would destroy the main frame's
@@ -484,6 +486,18 @@ pub fn pageNavigated(arena: Allocator, bc: anytype, event: *const Notification.P
try cdp.sendEvent("Runtime.executionContextsCleared", null, .{ .session_id = session_id });
}
// frameNavigated event
try cdp.sendEvent("Page.frameNavigated", .{
.type = "Navigation",
.frame = Frame{
.id = frame_id,
.url = event.url,
.loaderId = loader_id,
.securityOrigin = bc.security_origin,
.secureContextType = bc.secure_context_type,
},
}, .{ .session_id = session_id });
{
const aux_data = try std.fmt.allocPrint(arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\",\"loaderId\":\"{s}\"}}", .{ frame_id, loader_id });
@@ -554,18 +568,22 @@ pub fn pageNavigated(arena: Allocator, bc: anytype, event: *const Notification.P
// chromedp client expects to receive the events is this order.
// see https://github.com/chromedp/chromedp/issues/1558
try cdp.sendEvent("DOM.documentUpdated", null, .{ .session_id = session_id });
}
pub fn pageDOMContentLoaded(bc: anytype, event: *const Notification.PageDOMContentLoaded) !void {
const session_id = bc.session_id orelse return;
const timestamp = event.timestamp;
var cdp = bc.cdp;
// domContentEventFired event
// TODO: partially hard coded
try cdp.sendEvent(
"Page.domContentEventFired",
.{ .timestamp = timestamp },
.{ .session_id = session_id },
);
// lifecycle DOMContentLoaded event
// TODO: partially hard coded
if (bc.page_life_cycle_events) {
const frame_id = &id.toFrameId(event.frame_id);
const loader_id = &id.toLoaderId(event.req_id);
try cdp.sendEvent("Page.lifecycleEvent", LifecycleEvent{
.timestamp = timestamp,
.name = "DOMContentLoaded",
@@ -573,16 +591,23 @@ pub fn pageNavigated(arena: Allocator, bc: anytype, event: *const Notification.P
.loaderId = loader_id,
}, .{ .session_id = session_id });
}
}
pub fn pageLoaded(bc: anytype, event: *const Notification.PageLoaded) !void {
const session_id = bc.session_id orelse return;
const timestamp = event.timestamp;
var cdp = bc.cdp;
const frame_id = &id.toFrameId(event.frame_id);
// loadEventFired event
try cdp.sendEvent(
"Page.loadEventFired",
.{ .timestamp = timestamp },
.{ .session_id = session_id },
);
// lifecycle DOMContentLoaded event
if (bc.page_life_cycle_events) {
const loader_id = &id.toLoaderId(event.req_id);
try cdp.sendEvent("Page.lifecycleEvent", LifecycleEvent{
.timestamp = timestamp,
.name = "load",
@@ -591,21 +616,20 @@ pub fn pageNavigated(arena: Allocator, bc: anytype, event: *const Notification.P
}, .{ .session_id = session_id });
}
// frameStoppedLoading
return cdp.sendEvent("Page.frameStoppedLoading", .{
.frameId = frame_id,
}, .{ .session_id = session_id });
}
pub fn pageNetworkIdle(bc: anytype, event: *const Notification.PageNetworkIdle) !void {
pub fn pageNetworkIdle(bc: *CDP.BrowserContext, event: *const Notification.PageNetworkIdle) !void {
return sendPageLifecycle(bc, "networkIdle", event.timestamp, &id.toFrameId(event.frame_id), &id.toLoaderId(event.req_id));
}
pub fn pageNetworkAlmostIdle(bc: anytype, event: *const Notification.PageNetworkAlmostIdle) !void {
pub fn pageNetworkAlmostIdle(bc: *CDP.BrowserContext, event: *const Notification.PageNetworkAlmostIdle) !void {
return sendPageLifecycle(bc, "networkAlmostIdle", event.timestamp, &id.toFrameId(event.frame_id), &id.toLoaderId(event.req_id));
}
fn sendPageLifecycle(bc: anytype, name: []const u8, timestamp: u64, frame_id: []const u8, loader_id: []const u8) !void {
fn sendPageLifecycle(bc: *CDP.BrowserContext, name: []const u8, timestamp: u64, frame_id: []const u8, loader_id: []const u8) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -640,7 +664,7 @@ fn base64Encode(comptime input: []const u8) [std.base64.standard.Encoder.calcSiz
return buf;
}
fn captureScreenshot(cmd: anytype) !void {
fn captureScreenshot(cmd: *CDP.Command) !void {
const Params = struct {
format: ?[]const u8 = "png",
quality: ?u8 = null,
@@ -676,7 +700,7 @@ fn captureScreenshot(cmd: anytype) !void {
}, .{});
}
fn getLayoutMetrics(cmd: anytype) !void {
fn getLayoutMetrics(cmd: *CDP.Command) !void {
const width = 1920;
const height = 1080;

View File

@@ -17,8 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,

View File

@@ -19,7 +19,9 @@
const std = @import("std");
const builtin = @import("builtin");
pub fn processMessage(cmd: anytype) !void {
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
enable,
runIfWaitingForDebugger,
@@ -36,7 +38,7 @@ pub fn processMessage(cmd: anytype) !void {
}
}
fn sendInspector(cmd: anytype, action: anytype) !void {
fn sendInspector(cmd: *CDP.Command, action: anytype) !void {
// save script in file at debug mode
if (builtin.mode == .Debug) {
try logInspector(cmd, action);
@@ -48,7 +50,7 @@ fn sendInspector(cmd: anytype, action: anytype) !void {
bc.callInspector(cmd.input.json);
}
fn logInspector(cmd: anytype, action: anytype) !void {
fn logInspector(cmd: *CDP.Command, action: anytype) !void {
const script = switch (action) {
.evaluate => blk: {
const params = (try cmd.params(struct {

View File

@@ -17,8 +17,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const CDP = @import("../CDP.zig");
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
enable,
disable,
@@ -32,7 +33,7 @@ pub fn processMessage(cmd: anytype) !void {
}
}
fn setIgnoreCertificateErrors(cmd: anytype) !void {
fn setIgnoreCertificateErrors(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
ignore: bool,
})) orelse return error.InvalidParams;

View File

@@ -18,13 +18,16 @@
const std = @import("std");
const CDP = @import("../CDP.zig");
const log = @import("../../log.zig");
const URL = @import("../../browser/URL.zig");
const Cookie = @import("../../browser/webapi/storage/storage.zig").Cookie;
const CookieJar = Cookie.Jar;
pub const PreparedUri = Cookie.PreparedUri;
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
clearCookies,
setCookies,
@@ -40,7 +43,7 @@ pub fn processMessage(cmd: anytype) !void {
const BrowserContextParam = struct { browserContextId: ?[]const u8 = null };
fn clearCookies(cmd: anytype) !void {
fn clearCookies(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(BrowserContextParam)) orelse BrowserContextParam{};
@@ -55,7 +58,7 @@ fn clearCookies(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
fn getCookies(cmd: anytype) !void {
fn getCookies(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(BrowserContextParam)) orelse BrowserContextParam{};
@@ -69,7 +72,7 @@ fn getCookies(cmd: anytype) !void {
try cmd.sendResult(.{ .cookies = writer }, .{});
}
fn setCookies(cmd: anytype) !void {
fn setCookies(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const params = (try cmd.params(struct {
cookies: []const CdpCookie,

View File

@@ -20,11 +20,13 @@ const std = @import("std");
const lp = @import("lightpanda");
const id = @import("../id.zig");
const CDP = @import("../CDP.zig");
const log = @import("../../log.zig");
const URL = @import("../../browser/URL.zig");
const js = @import("../../browser/js/js.zig");
pub fn processMessage(cmd: anytype) !void {
pub fn processMessage(cmd: *CDP.Command) !void {
const action = std.meta.stringToEnum(enum {
getTargets,
attachToTarget,
@@ -60,7 +62,7 @@ pub fn processMessage(cmd: anytype) !void {
}
}
fn getTargets(cmd: anytype) !void {
fn getTargets(cmd: *CDP.Command) !void {
// If no context available, return an empty array.
const bc = cmd.browser_context orelse {
return cmd.sendResult(.{
@@ -86,7 +88,7 @@ fn getTargets(cmd: anytype) !void {
}, .{ .include_session_id = false });
}
fn getBrowserContexts(cmd: anytype) !void {
fn getBrowserContexts(cmd: *CDP.Command) !void {
var browser_context_ids: []const []const u8 = undefined;
if (cmd.browser_context) |bc| {
browser_context_ids = &.{bc.id};
@@ -99,7 +101,7 @@ fn getBrowserContexts(cmd: anytype) !void {
}, .{ .include_session_id = false });
}
fn createBrowserContext(cmd: anytype) !void {
fn createBrowserContext(cmd: *CDP.Command) !void {
const params = try cmd.params(struct {
disposeOnDetach: bool = false,
proxyServer: ?[:0]const u8 = null,
@@ -130,7 +132,7 @@ fn createBrowserContext(cmd: anytype) !void {
}, .{});
}
fn disposeBrowserContext(cmd: anytype) !void {
fn disposeBrowserContext(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
browserContextId: []const u8,
})) orelse return error.InvalidParams;
@@ -141,7 +143,7 @@ fn disposeBrowserContext(cmd: anytype) !void {
try cmd.sendResult(null, .{});
}
fn createTarget(cmd: anytype) !void {
fn createTarget(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
url: [:0]const u8 = "about:blank",
// width: ?u64 = null,
@@ -230,7 +232,7 @@ fn createTarget(cmd: anytype) !void {
}, .{});
}
fn attachToTarget(cmd: anytype) !void {
fn attachToTarget(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
targetId: []const u8,
flatten: bool = true,
@@ -247,7 +249,7 @@ fn attachToTarget(cmd: anytype) !void {
return cmd.sendResult(.{ .sessionId = bc.session_id }, .{});
}
fn attachToBrowserTarget(cmd: anytype) !void {
fn attachToBrowserTarget(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const session_id = bc.session_id orelse cmd.cdp.session_id_gen.next();
@@ -269,7 +271,7 @@ fn attachToBrowserTarget(cmd: anytype) !void {
return cmd.sendResult(.{ .sessionId = bc.session_id }, .{});
}
fn closeTarget(cmd: anytype) !void {
fn closeTarget(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
targetId: []const u8,
})) orelse return error.InvalidParams;
@@ -310,7 +312,7 @@ fn closeTarget(cmd: anytype) !void {
bc.target_id = null;
}
fn getTargetInfo(cmd: anytype) !void {
fn getTargetInfo(cmd: *CDP.Command) !void {
const Params = struct {
targetId: ?[]const u8 = null,
};
@@ -347,7 +349,7 @@ fn getTargetInfo(cmd: anytype) !void {
}, .{ .include_session_id = false });
}
fn sendMessageToTarget(cmd: anytype) !void {
fn sendMessageToTarget(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
message: []const u8,
sessionId: []const u8,
@@ -365,32 +367,19 @@ fn sendMessageToTarget(cmd: anytype) !void {
return error.UnknownSessionId;
}
const Capture = struct {
aw: std.Io.Writer.Allocating,
pub fn sendJSON(self: *@This(), message: anytype) !void {
return std.json.Stringify.value(message, .{
.emit_null_optional_fields = false,
}, &self.aw.writer);
}
};
var capture = Capture{
.aw = .init(cmd.arena),
};
cmd.cdp.dispatch(cmd.arena, &capture, params.message) catch |err| {
var aw = std.Io.Writer.Allocating.init(cmd.arena);
cmd.cdp.dispatch(cmd.arena, .{ .capture = &aw.writer }, params.message) catch |err| {
log.err(.cdp, "internal dispatch error", .{ .err = err, .id = cmd.input.id, .message = params.message });
return err;
};
try cmd.sendEvent("Target.receivedMessageFromTarget", .{
.message = capture.aw.written(),
.message = aw.written(),
.sessionId = params.sessionId,
}, .{});
}
fn detachFromTarget(cmd: anytype) !void {
fn detachFromTarget(cmd: *CDP.Command) !void {
if (cmd.browser_context) |bc| {
if (bc.session_id) |session_id| {
try cmd.sendEvent("Target.detachedFromTarget", .{
@@ -404,11 +393,11 @@ fn detachFromTarget(cmd: anytype) !void {
}
// TODO: noop method
fn setDiscoverTargets(cmd: anytype) !void {
fn setDiscoverTargets(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
fn setAutoAttach(cmd: anytype) !void {
fn setAutoAttach(cmd: *CDP.Command) !void {
const params = (try cmd.params(struct {
autoAttach: bool,
waitForDebuggerOnStart: bool,
@@ -468,7 +457,7 @@ fn setAutoAttach(cmd: anytype) !void {
try cmd.sendResult(null, .{});
}
fn doAttachtoTarget(cmd: anytype, target_id: []const u8) !void {
fn doAttachtoTarget(cmd: *CDP.Command, target_id: []const u8) !void {
const bc = cmd.browser_context.?;
const session_id = bc.session_id orelse cmd.cdp.session_id_gen.next();

View File

@@ -64,7 +64,7 @@ const TestContext = struct {
session_id: ?[]const u8 = null,
url: ?[:0]const u8 = null,
};
pub fn loadBrowserContext(self: *TestContext, opts: BrowserContextOpts) !*CDP.BrowserContext(CDP) {
pub fn loadBrowserContext(self: *TestContext, opts: BrowserContextOpts) !*CDP.BrowserContext {
var c = self.cdp();
if (c.browser_context) |bc| {
_ = c.disposeBrowserContext(bc.id);

View File

@@ -75,6 +75,19 @@ pub const tool_list = [_]protocol.Tool{
\\}
),
},
.{
.name = "nodeDetails",
.description = "Get detailed information about a specific node by its backend node ID. Returns tag, role, name, interactivity, disabled state, value, input type, placeholder, href, checked state, and select options.",
.inputSchema = protocol.minify(
\\{
\\ "type": "object",
\\ "properties": {
\\ "backendNodeId": { "type": "integer", "description": "The backend node ID of the element to inspect." }
\\ },
\\ "required": ["backendNodeId"]
\\}
),
},
.{
.name = "interactiveElements",
.description = "Extract interactive elements from the opened page. If a url is provided, it navigates to that url first.",
@@ -256,6 +269,7 @@ const ToolAction = enum {
navigate,
markdown,
links,
nodeDetails,
interactiveElements,
structuredData,
detectForms,
@@ -272,6 +286,7 @@ const tool_map = std.StaticStringMap(ToolAction).initComptime(.{
.{ "navigate", .navigate },
.{ "markdown", .markdown },
.{ "links", .links },
.{ "nodeDetails", .nodeDetails },
.{ "interactiveElements", .interactiveElements },
.{ "structuredData", .structuredData },
.{ "detectForms", .detectForms },
@@ -305,6 +320,7 @@ pub fn handleCall(server: *Server, arena: std.mem.Allocator, req: protocol.Reque
.goto, .navigate => try handleGoto(server, arena, req.id.?, call_params.arguments),
.markdown => try handleMarkdown(server, arena, req.id.?, call_params.arguments),
.links => try handleLinks(server, arena, req.id.?, call_params.arguments),
.nodeDetails => try handleNodeDetails(server, arena, req.id.?, call_params.arguments),
.interactiveElements => try handleInteractiveElements(server, arena, req.id.?, call_params.arguments),
.structuredData => try handleStructuredData(server, arena, req.id.?, call_params.arguments),
.detectForms => try handleDetectForms(server, arena, req.id.?, call_params.arguments),
@@ -373,6 +389,32 @@ fn handleSemanticTree(server: *Server, arena: std.mem.Allocator, id: std.json.Va
};
}
fn handleNodeDetails(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
const Params = struct {
backendNodeId: CDPNode.Id,
};
const args = try parseArgs(Params, arena, arguments, server, id, "nodeDetails");
_ = server.session.currentPage() orelse {
return server.sendError(id, .PageNotLoaded, "Page not loaded");
};
const node = server.node_registry.lookup_by_id.get(args.backendNodeId) orelse {
return server.sendError(id, .InvalidParams, "Node not found");
};
const page = server.session.currentPage().?;
const details = lp.SemanticTree.getNodeDetails(arena, node.dom, &server.node_registry, page) catch {
return server.sendError(id, .InternalError, "Failed to get node details");
};
var aw: std.Io.Writer.Allocating = .init(arena);
try std.json.Stringify.value(&details, .{}, &aw.writer);
const content = [_]protocol.TextContent([]const u8){.{ .text = aw.written() }};
try server.sendResult(id, protocol.CallToolResult([]const u8){ .content = &content });
}
fn handleInteractiveElements(server: *Server, arena: std.mem.Allocator, id: std.json.Value, arguments: ?std.json.Value) !void {
const args = try parseArgsOrDefault(UrlParams, arena, arguments, server, id);
const page = try ensurePage(server, id, args.url);