mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
browser: split page start from page navigate
This commit is contained in:
@@ -214,6 +214,20 @@ pub const Page = struct {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// start js env.
|
||||||
|
pub fn start(self: *Page) !void {
|
||||||
|
// start JS env
|
||||||
|
log.debug("start js env", .{});
|
||||||
|
try self.session.env.start();
|
||||||
|
|
||||||
|
// register the module loader
|
||||||
|
try self.session.env.setModuleLoadFn(self.session, Session.fetchModule);
|
||||||
|
|
||||||
|
// add global objects
|
||||||
|
log.debug("setup global env", .{});
|
||||||
|
try self.session.env.bindGlobal(&self.session.window);
|
||||||
|
}
|
||||||
|
|
||||||
// reset js env and mem arena.
|
// reset js env and mem arena.
|
||||||
pub fn end(self: *Page) void {
|
pub fn end(self: *Page) void {
|
||||||
self.session.env.stop();
|
self.session.env.stop();
|
||||||
@@ -373,14 +387,6 @@ pub const Page = struct {
|
|||||||
|
|
||||||
// https://html.spec.whatwg.org/#read-html
|
// https://html.spec.whatwg.org/#read-html
|
||||||
|
|
||||||
// start JS env
|
|
||||||
// TODO load the js env concurrently with the HTML parsing.
|
|
||||||
log.debug("start js env", .{});
|
|
||||||
try self.session.env.start();
|
|
||||||
|
|
||||||
// register the module loader
|
|
||||||
try self.session.env.setModuleLoadFn(self.session, Session.fetchModule);
|
|
||||||
|
|
||||||
// load polyfills
|
// load polyfills
|
||||||
try polyfill.load(alloc, self.session.env);
|
try polyfill.load(alloc, self.session.env);
|
||||||
|
|
||||||
@@ -395,10 +401,6 @@ pub const Page = struct {
|
|||||||
.httpClient = &self.session.httpClient,
|
.httpClient = &self.session.httpClient,
|
||||||
});
|
});
|
||||||
|
|
||||||
// add global objects
|
|
||||||
log.debug("setup global env", .{});
|
|
||||||
try self.session.env.bindGlobal(&self.session.window);
|
|
||||||
|
|
||||||
// browse the DOM tree to retrieve scripts
|
// browse the DOM tree to retrieve scripts
|
||||||
// TODO execute the synchronous scripts during the HTL parsing.
|
// TODO execute the synchronous scripts during the HTL parsing.
|
||||||
// TODO fetch the script resources concurrently but execute them in the
|
// TODO fetch the script resources concurrently but execute them in the
|
||||||
|
|||||||
@@ -331,8 +331,9 @@ fn navigate(
|
|||||||
// TODO: noop event, we have no env context at this point, is it necesarry?
|
// TODO: noop event, we have no env context at this point, is it necesarry?
|
||||||
try sendEvent(alloc, ctx, "Runtime.executionContextsCleared", void, {}, input.sessionId);
|
try sendEvent(alloc, ctx, "Runtime.executionContextsCleared", void, {}, input.sessionId);
|
||||||
|
|
||||||
// Launch navigate
|
// Launch navigate, the page must have been created by a
|
||||||
const p = try ctx.browser.session.createPage();
|
// target.createTarget.
|
||||||
|
var p = ctx.browser.session.page orelse return error.NoPage;
|
||||||
ctx.state.executionContextId += 1;
|
ctx.state.executionContextId += 1;
|
||||||
const auxData = try std.fmt.allocPrint(
|
const auxData = try std.fmt.allocPrint(
|
||||||
alloc,
|
alloc,
|
||||||
|
|||||||
@@ -344,6 +344,14 @@ fn createTarget(
|
|||||||
ctx.state.loaderID = LoaderID;
|
ctx.state.loaderID = LoaderID;
|
||||||
ctx.state.sessionID = msg.sessionId;
|
ctx.state.sessionID = msg.sessionId;
|
||||||
|
|
||||||
|
// TODO stop the previous page instead?
|
||||||
|
if (ctx.browser.session.page != null) return error.pageAlreadyExists;
|
||||||
|
|
||||||
|
// create the page
|
||||||
|
const p = try ctx.browser.session.createPage();
|
||||||
|
// start the js env
|
||||||
|
try p.start();
|
||||||
|
|
||||||
// send targetCreated event
|
// send targetCreated event
|
||||||
const created = TargetCreated{
|
const created = TargetCreated{
|
||||||
.sessionId = cdp.ContextSessionID,
|
.sessionId = cdp.ContextSessionID,
|
||||||
@@ -440,6 +448,8 @@ fn closeTarget(
|
|||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (ctx.browser.session.page != null) ctx.browser.session.page.?.end();
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -326,6 +326,8 @@ pub fn main() !void {
|
|||||||
|
|
||||||
// page
|
// page
|
||||||
const page = try browser.session.createPage();
|
const page = try browser.session.createPage();
|
||||||
|
try page.start();
|
||||||
|
defer page.end();
|
||||||
|
|
||||||
_ = page.navigate(opts.url, null) catch |err| switch (err) {
|
_ = page.navigate(opts.url, null) catch |err| switch (err) {
|
||||||
error.UnsupportedUriScheme, error.UriMissingHost => {
|
error.UnsupportedUriScheme, error.UriMissingHost => {
|
||||||
|
|||||||
Reference in New Issue
Block a user