mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-30 15:41:48 +00:00
get: start work on get command
This commit is contained in:
@@ -10,6 +10,7 @@ const TPL = jsruntime.TPL;
|
|||||||
|
|
||||||
const apiweb = @import("../apiweb.zig");
|
const apiweb = @import("../apiweb.zig");
|
||||||
const apis = jsruntime.compile(apiweb.Interfaces);
|
const apis = jsruntime.compile(apiweb.Interfaces);
|
||||||
|
|
||||||
const Window = @import("../nav/window.zig").Window;
|
const Window = @import("../nav/window.zig").Window;
|
||||||
|
|
||||||
const log = std.log.scoped(.lpd_browser);
|
const log = std.log.scoped(.lpd_browser);
|
||||||
@@ -65,11 +66,13 @@ pub const Session = struct {
|
|||||||
pub const Page = struct {
|
pub const Page = struct {
|
||||||
arena: std.heap.ArenaAllocator,
|
arena: std.heap.ArenaAllocator,
|
||||||
session: *Session,
|
session: *Session,
|
||||||
|
env: Env,
|
||||||
|
|
||||||
fn init(session: *Session) Page {
|
fn init(session: *Session) Page {
|
||||||
return Page{
|
return Page{
|
||||||
.session = session,
|
.session = session,
|
||||||
.arena = std.heap.ArenaAllocator.init(session.allocator),
|
.arena = std.heap.ArenaAllocator.init(session.allocator),
|
||||||
|
.env = undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,33 +96,42 @@ pub const Page = struct {
|
|||||||
|
|
||||||
if (result.body == null) return error.NoBody;
|
if (result.body == null) return error.NoBody;
|
||||||
|
|
||||||
|
// TODO check content-type
|
||||||
|
|
||||||
// TODO handle charset
|
// TODO handle charset
|
||||||
|
|
||||||
// document
|
// document
|
||||||
|
log.debug("parse html", .{});
|
||||||
const html_doc = try parser.documentHTMLParseFromStrAlloc(allocator, result.body.?);
|
const html_doc = try parser.documentHTMLParseFromStrAlloc(allocator, result.body.?);
|
||||||
const doc = parser.documentHTMLToDocument(html_doc);
|
const doc = parser.documentHTMLToDocument(html_doc);
|
||||||
|
|
||||||
// create JS env
|
log.debug("init loop", .{});
|
||||||
var loop = try Loop.init(allocator);
|
var loop = try Loop.init(allocator);
|
||||||
defer loop.deinit();
|
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
|
// load APIs in JS env
|
||||||
|
log.debug("load js apis", .{});
|
||||||
var tpls: [apis.len]TPL = undefined;
|
var tpls: [apis.len]TPL = undefined;
|
||||||
try js_env.load(apis, &tpls);
|
try self.env.load(apis, &tpls);
|
||||||
|
|
||||||
// start JS env
|
// start JS env
|
||||||
try js_env.start(allocator, apis);
|
log.debug("start js env", .{});
|
||||||
defer js_env.stop();
|
try self.env.start(allocator, apis);
|
||||||
|
defer self.env.stop();
|
||||||
|
|
||||||
// add global objects
|
// add global objects
|
||||||
|
log.debug("setup global env", .{});
|
||||||
const window = Window.create(doc, null);
|
const window = Window.create(doc, null);
|
||||||
_ = window;
|
_ = window;
|
||||||
// TODO should'nt we share the same pointer between instances of 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, "self");
|
||||||
// try js_env.addObject(apis, window, "window");
|
// 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
57
src/main_get.zig
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user