get: start work on get command

This commit is contained in:
Pierre Tachoire
2023-12-20 11:08:17 +01:00
parent 1642741027
commit df2e6dcfc0
2 changed files with 76 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ const TPL = jsruntime.TPL;
const apiweb = @import("../apiweb.zig");
const apis = jsruntime.compile(apiweb.Interfaces);
const Window = @import("../nav/window.zig").Window;
const log = std.log.scoped(.lpd_browser);
@@ -65,11 +66,13 @@ pub const Session = struct {
pub const Page = struct {
arena: std.heap.ArenaAllocator,
session: *Session,
env: Env,
fn init(session: *Session) Page {
return Page{
.session = session,
.arena = std.heap.ArenaAllocator.init(session.allocator),
.env = undefined,
};
}
@@ -93,33 +96,42 @@ pub const Page = struct {
if (result.body == null) return error.NoBody;
// TODO check content-type
// TODO handle charset
// document
log.debug("parse html", .{});
const html_doc = try parser.documentHTMLParseFromStrAlloc(allocator, result.body.?);
const doc = parser.documentHTMLToDocument(html_doc);
// create JS env
log.debug("init loop", .{});
var loop = try Loop.init(allocator);
defer loop.deinit();
var js_env = try Env.init(allocator, &loop);
defer js_env.deinit();
// create JS env
log.debug("init js env", .{});
self.env = try Env.init(allocator, &loop);
defer self.env.deinit();
// load APIs in JS env
log.debug("load js apis", .{});
var tpls: [apis.len]TPL = undefined;
try js_env.load(apis, &tpls);
try self.env.load(apis, &tpls);
// start JS env
try js_env.start(allocator, apis);
defer js_env.stop();
log.debug("start js env", .{});
try self.env.start(allocator, apis);
defer self.env.stop();
// add global objects
log.debug("setup global env", .{});
const window = Window.create(doc, null);
_ = window;
// TODO should'nt we share the same pointer between instances of window?
// try js_env.addObject(apis, window, "self");
// try js_env.addObject(apis, window, "window");
try js_env.addObject(apis, doc, "document");
try self.env.addObject(apis, doc, "document");
}
};

57
src/main_get.zig Normal file
View File

@@ -0,0 +1,57 @@
const std = @import("std");
const b = @import("browser/browser.zig");
pub const std_options = struct {
pub const log_level = .debug;
};
const usage =
\\usage: {s} [options] <url>
\\ request the url with the browser
\\
\\ -h, --help Print this help message and exit.
\\
;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const check = gpa.deinit();
if (check == .leak) {
std.log.warn("leaks detected\n", .{});
}
}
const allocator = gpa.allocator();
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();
const execname = args.next().?;
var url: []const u8 = "";
while (args.next()) |arg| {
if (std.mem.eql(u8, "-h", arg) or std.mem.eql(u8, "--help", arg)) {
try std.io.getStdErr().writer().print(usage, .{execname});
std.os.exit(0);
}
// allow only one url
if (url.len != 0) {
try std.io.getStdErr().writer().print(usage, .{execname});
std.os.exit(1);
}
url = arg;
}
if (url.len == 0) {
try std.io.getStdErr().writer().print(usage, .{execname});
std.os.exit(1);
}
var browser = b.Browser.init(allocator);
defer browser.deinit();
var page = try browser.currentSession().createPage();
defer page.deinit();
try page.navigate(url);
}