Add a --noscript option to "improve" --dump

Currently, fetch --dump includes <script> tag (either inline or with src). I
don't know what use-case this is the desired behavior. Excluding them, via the
new --noscript option has benefit that if you --dump --noscript and open the
resulting page in the browser, you don't re-execute JavaScript, which is
likely to break the page.

For example, opening a --dump of github makes it look like the page is broken
because it re-executes JavaScript that isn't meant to be re-executed.

Similarly, opening a --dump in a browser might execute JavaScript that
lightpanda browser failed to execute, making it looks like it worked better
than it did.
This commit is contained in:
Karl Seguin
2025-07-14 18:24:36 +08:00
parent d35a3eab6c
commit cfd62ac137
5 changed files with 35 additions and 14 deletions

View File

@@ -134,7 +134,7 @@ fn run(alloc: Allocator) !void {
// dump
if (opts.dump) {
try page.dump(std.io.getStdOut());
try page.dump(.{ .exclude_scripts = opts.noscript }, std.io.getStdOut());
}
},
else => unreachable,
@@ -212,6 +212,7 @@ const Command = struct {
url: []const u8,
dump: bool = false,
common: Common,
noscript: bool = false,
};
const Common = struct {
@@ -275,6 +276,7 @@ const Command = struct {
\\Options:
\\--dump Dumps document to stdout.
\\ Defaults to false.
\\--noscript Exclude <script> tags in dump. Defaults to false.
\\
++ common_options ++
\\
@@ -352,6 +354,9 @@ fn inferMode(opt: []const u8) ?App.RunMode {
if (std.mem.eql(u8, opt, "--dump")) {
return .fetch;
}
if (std.mem.eql(u8, opt, "--noscript")) {
return .fetch;
}
if (std.mem.startsWith(u8, opt, "--") == false) {
return .fetch;
}
@@ -437,6 +442,7 @@ fn parseFetchArgs(
args: *std.process.ArgIterator,
) !Command.Fetch {
var dump: bool = false;
var noscript: bool = true;
var url: ?[]const u8 = null;
var common: Command.Common = .{};
@@ -446,6 +452,11 @@ fn parseFetchArgs(
continue;
}
if (std.mem.eql(u8, "--noscript", opt)) {
noscript = true;
continue;
}
if (try parseCommonArg(allocator, opt, args, &common)) {
continue;
}
@@ -471,6 +482,7 @@ fn parseFetchArgs(
.url = url.?,
.dump = dump,
.common = common,
.noscript = noscript,
};
}