Trying to take screenshot with webkit failing

main.zig

const std = @import("std");
const mem = std.mem;

const c = @import("std").c;

const WebKit = @cImport({
    @cInclude("webkit2/webkit2.h");
    @cInclude("glib-2.0/glib.h");
});

pub fn main() void {
    WebKit.gtk_init(null, null);

    const webview = WebKit.webkit_web_view_new();
    if (webview == null) {
        std.debug.print("Failed to create web view.\n", .{});
        return;
    }

    const cookieManager = WebKit.webkit_web_context_get_cookie_manager(WebKit.webkit_web_view_get_context(webview));
    const cookie = "sessionid=; path=/; domain=www.tradingview.com;";
    WebKit.webkit_cookie_manager_set_cookie(cookieManager, cookie);

    const url = "https://www.tradingview.com/";
    WebKit.webkit_web_view_load_uri(webview, url);

    std.debug.print("Navigating to URL: {}\n", .{url});

    const screenshot = WebKit.webkit_web_view_get_snapshot(webview, WebKit.WebKitSnapshotRegionFull);
    if (screenshot != null) {
        std.debug.print("Screenshot captured.\n", .{});
    } else {
        std.debug.print("Failed to capture screenshot.\n", .{});
    }

    WebKit.gtk_main_quit();
}

build.zig

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    // Define the executable
    const exe = b.addExecutable(.{
        .name = "web_scraper",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });
    exe.linkLibC();
    exe.addIncludePath(.{ .cwd_relative = "/usr/include/gtk-4.0" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/include/glib-2.0" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/include/pango-1.0" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/include/gdk-pixbuf-2.0" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/include/atk-1.0" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/include/harfbuzz" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/include/cairo" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/lib/x86_64-linux-gnu/glib-2.0/include" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/include/webkitgtk-4.0" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/include/webkitgtk-4.0/webkit2/" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/include/graphene-1.0" });
    exe.addIncludePath(.{ .cwd_relative = "/usr/lib/x86_64-linux-gnu/graphene-1.0/include" });

    exe.linkSystemLibrary("libsoup-2.4");
    exe.linkSystemLibrary("webkit2gtk-4.0");
    // Install the executable
    b.installArtifact(exe);

    // Add a run step for the executable
    const run_cmd = b.addRunArtifact(exe);
    b.step("run", "Run the web scraper").dependOn(&run_cmd.step);
}

error:

src/main.zig:20:107: error: expected type '[*c]cimport.struct__WebKitWebView', found '[*c]cimport.struct__GtkWidget'
    const cookieManager = WebKit.webkit_web_context_get_cookie_manager(WebKit.webkit_web_view_get_context(webview));
                                                                                                          ^~~~~~~
src/main.zig:20:107: note: pointer type child 'cimport.struct__GtkWidget' cannot cast into pointer type child 'cimport.struct__WebKitWebView'
/home/user/Documents/ZIG_SCRAPO/.zig-cache/o/1b6974f62309ff8f16d0f069fec6cb27/cimport.zig:23042:38: note: struct declared here
pub const struct__GtkWidget = extern struct {
                              ~~~~~~~^~~~~~
/home/user/Documents/ZIG_SCRAPO/.zig-cache/o/1b6974f62309ff8f16d0f069fec6cb27/cimport.zig:42082:42: note: struct declared here
pub const struct__WebKitWebView = extern struct {
                                  ~~~~~~~^~~~~~
/home/user/Documents/ZIG_SCRAPO/.zig-cache/o/1b6974f62309ff8f16d0f069fec6cb27/cimport.zig:43988:54: note: parameter type declared here
pub extern fn webkit_web_view_get_context(web_view: [*c]WebKitWebView) [*c]WebKitWebContext;
                                                    ~^~~~~~~~~~~~~~~~
referenced by:
    callMain: /snap/zig/11625/lib/std/start.zig:514:17
    callMainWithArgs: /snap/zig/11625/lib/std/start.zig:482:12
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
error: the following command failed with 1 compilation errors:
/snap/zig/11625/zig build-exe -I/usr/include/libsoup-2.4 -I/usr/include/libxml2 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lsoup-2.4 -lgmodule-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -I/usr/include/webkitgtk-4.0 -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/fribidi -I/usr/include/harfbuzz -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/x86_64-linux-gnu -I/usr/include/libsoup-2.4 -I/usr/include/libxml2 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/webkitgtk-4.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lwebkit2gtk-4.0 -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lsoup-2.4 -lgmodule-2.0 -lgio-2.0 -ljavascriptcoregtk-4.0 -lgobject-2.0 -lglib-2.0 -ODebug -I /usr/include/gtk-4.0 -I /usr/include/glib-2.0 -I /usr/include/pango-1.0 -I /usr/include/gdk-pixbuf-2.0 -I /usr/include/atk-1.0 -I /usr/include/harfbuzz -I /usr/include/cairo -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/webkitgtk-4.0 -I /usr/include/webkitgtk-4.0/webkit2 -I /usr/include/graphene-1.0 -I /usr/lib/x86_64-linux-gnu/graphene-1.0/include -Mroot=/home/user/Documents/ZIG_SCRAPO/src/main.zig -lc --cache-dir /home/user/Documents/ZIG_SCRAPO/.zig-cache --global-cache-dir /home/user/.cache/zig --name web_scraper --listen=- 
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install web_scraper transitive failure
   └─ zig build-exe web_scraper Debug native 1 errors
error: the following build command failed with exit code 1:
/home/user/Documents/ZIG_SCRAPO/.zig-cache/o/545de5ed06d903bf8b6af7010f658b0f/build /snap/zig/11625/zig /home/user/Documents/ZIG_SCRAPO /home/user/Documents/ZIG_SCRAPO/.zig-cache /home/user/.cache/zig --seed 0x6c714316 -Z5d2ba8c4bbaf411c

webkit saying is not there but when I click it takes me to it the webkit2.h correctly

I am not familiar with the WebKit API, but the error that you posted does not state, that WebKit ‘is not there’.

You are getting a type error, saying that the WebKit.webkit_web_view_get_context function expects a pointer to type WebKitWebView, but you pass a pointer to a GtkWidget (because this is what the WebKit.webkit_web_view_new() function returns).

Maybe you can use @ptrCast to cast to the appropriate type, but again, I am not familiar with the Gtk or Webkit to say, if this will work, maybe there is some other function call missing, that turns the GtkWidget to a WebKitWebView.

2 Likes