Merge pull request #231 from lightpanda-io/browser-wait

browser: extract loop wait
This commit is contained in:
Pierre Tachoire
2024-05-22 15:18:00 +02:00
committed by GitHub
2 changed files with 22 additions and 4 deletions

View File

@@ -197,6 +197,24 @@ pub const Page = struct {
try Dump.writeHTML(self.doc.?, out); try Dump.writeHTML(self.doc.?, out);
} }
pub fn wait(self: *Page) !void {
const alloc = self.arena.allocator();
var res = try self.session.env.waitTryCatch(alloc);
defer res.deinit(alloc);
if (res.success) {
log.debug("wait: {s}", .{res.result});
} else {
if (builtin.mode == .Debug and res.stack != null) {
log.info("wait: {s}", .{res.stack.?});
} else {
log.info("wait: {s}", .{res.result});
}
}
return;
}
// spec reference: https://html.spec.whatwg.org/#document-lifecycle // spec reference: https://html.spec.whatwg.org/#document-lifecycle
pub fn navigate(self: *Page, uri: []const u8) !void { pub fn navigate(self: *Page, uri: []const u8) !void {
const alloc = self.arena.allocator(); const alloc = self.arena.allocator();
@@ -448,8 +466,7 @@ pub const Page = struct {
const opt_text = try parser.nodeTextContent(parser.elementToNode(e)); const opt_text = try parser.nodeTextContent(parser.elementToNode(e));
if (opt_text) |text| { if (opt_text) |text| {
// TODO handle charset attribute // TODO handle charset attribute
var res = jsruntime.JSResult{}; var res = try self.session.env.execTryCatch(alloc, text, "");
try self.session.env.run(alloc, text, "", &res, null);
defer res.deinit(alloc); defer res.deinit(alloc);
if (res.success) { if (res.success) {
@@ -498,8 +515,7 @@ pub const Page = struct {
// check no body // check no body
if (fetchres.body == null) return FetchError.NoBody; if (fetchres.body == null) return FetchError.NoBody;
var res = jsruntime.JSResult{}; var res = try self.session.env.execTryCatch(alloc, fetchres.body.?, src);
try self.session.env.run(alloc, fetchres.body.?, src, &res, null);
defer res.deinit(alloc); defer res.deinit(alloc);
if (res.success) { if (res.success) {

View File

@@ -89,6 +89,8 @@ pub fn main() !void {
try page.navigate(url); try page.navigate(url);
defer page.end(); defer page.end();
try page.wait();
if (dump) { if (dump) {
try page.dump(std.io.getStdOut()); try page.dump(std.io.getStdOut());
} }