Lightpanda: the headless browser designed for AI and automation
cdp: send Target.targetInfoChanged when a page's URL or title changes
Adrià Arrufat07e68aea8518
diff --git a/src/server/cdp/domains/page.zig b/src/server/cdp/domains/page.zigindex c0fa2fb58..6ab038a61 100644--- a/src/server/cdp/domains/page.zig+++ b/src/server/cdp/domains/page.zig@@ -22,6 +22,7 @@ const lp = @import("lightpanda");const id = @import("../id.zig");const CDP = @import("../CDP.zig");+const target = @import("target.zig");const js = @import("../../../browser/js/js.zig");const URL = @import("../../../browser/URL.zig");@@ -755,6 +756,7 @@ pub fn frameNavigated(arena: Allocator, bc: *CDP.BrowserContext, event: *const N.type = "Navigation",.frame = FrameWriter{ .bc = bc, .frame = frame },}, .{ .session_id = session_id });+ try target.sendTargetInfoChanged(bc, event.frame_id);{const aux_data = try arena.print("{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\",\"loaderId\":\"{s}\"}}", .{ frame_id, loader_id });@@ -854,6 +856,7 @@ pub fn frameNavigatedWithinDocument(bc: anytype, event: *const Notification.Fram.url = event.url,.navigationType = @tagName(event.navigation_type),}, .{ .session_id = session_id });+ try target.sendTargetInfoChanged(bc, event.frame_id);}pub fn frameDOMContentLoaded(bc: anytype, event: *const Notification.FrameDOMContentLoaded) !void {@@ -891,6 +894,8 @@ pub fn frameLoaded(bc: anytype, event: *const Notification.FrameLoaded) !void {.{ .timestamp = timestamp },.{ .session_id = session_id },);+ // The title is usually known by now, not at navigation.+ try target.sendTargetInfoChanged(bc, event.frame_id);if (bc.page_life_cycle_events) {const loader_id = &id.toLoaderId(event.loader_id);diff --git a/src/server/cdp/domains/target.zig b/src/server/cdp/domains/target.zigindex 7af5c83cb..d554913ac 100644--- a/src/server/cdp/domains/target.zig+++ b/src/server/cdp/domains/target.zig@@ -71,22 +71,31 @@ fn getTargets(cmd: *CDP.Command) !void {}, .{});};- const target_id = &(bc.target_id orelse {+ const info = pageTargetInfo(bc) orelse {return cmd.sendResult(.{.targetInfos = [_]TargetInfo{},}, .{});- });+ };+ return cmd.sendResult(.{ .targetInfos = [_]TargetInfo{info} }, .{});+}- return cmd.sendResult(.{- .targetInfos = [_]TargetInfo{.{- .targetId = target_id,- .type = "page",- .title = bc.getTitle() orelse "",- .url = bc.getURL() orelse "about:blank",- .attached = true,- .canAccessOpener = false,- }},- }, .{});+fn pageTargetInfo(bc: *const CDP.BrowserContext) ?TargetInfo {+ const target_id = if (bc.target_id) |*tid| tid else return null;+ return .{+ .targetId = target_id,+ .title = bc.getTitle() orelse "",+ .url = bc.getURL() orelse "about:blank",+ .browserContextId = bc.id,+ };+}++/// Clients such as Browser Use track a tab's URL and title only through this+/// event, as Chrome sends it whenever either changes.+pub fn sendTargetInfoChanged(bc: *const CDP.BrowserContext, frame_id: u32) !void {+ const main = bc.mainFrame() orelse return;+ if (main._frame_id != frame_id) return;+ const info = pageTargetInfo(bc) orelse return;+ try bc.cdp.sendEvent("Target.targetInfoChanged", .{ .targetInfo = info }, .{});}fn getBrowserContexts(cmd: *CDP.Command) !void {@@ -331,21 +340,11 @@ fn getTargetInfo(cmd: *CDP.Command) !void {if (params.targetId) |param_target_id| {const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;- const target_id = &(bc.target_id orelse return error.TargetNotLoaded);- if (std.mem.eql(u8, target_id, param_target_id) == false) {+ const info = pageTargetInfo(bc) orelse return error.TargetNotLoaded;+ if (std.mem.eql(u8, info.targetId, param_target_id) == false) {return error.UnknownTargetId;}-- return cmd.sendResult(.{- .targetInfo = TargetInfo{- .targetId = target_id,- .type = "page",- .title = bc.getTitle() orelse "",- .url = bc.getURL() orelse "about:blank",- .attached = true,- .canAccessOpener = false,- },- }, .{});+ return cmd.sendResult(.{ .targetInfo = info }, .{});}return cmd.sendResult(.{@@ -1128,3 +1127,16 @@ test "cdp.target: setAutoAttach false sends detachedFromTarget" {try testing.expectEqual(null, bc.session_id);try ctx.expectSentResult(null, .{ .id = 12 });}++test "cdp.target: targetInfoChanged reports the page's URL and title" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{ .id = "BID-TIC", .url = "cdp/accname.html", .target_id = "FID-000000000I".* });+ try ctx.expectSentEvent("Target.targetInfoChanged", .{ .targetInfo = .{+ .targetId = &bc.target_id.?,+ .url = bc.getURL().?,+ .title = "AccName Fixture",+ .browserContextId = "BID-TIC",+ } }, .{});+}