package webkit /* #cgo pkg-config: webkitgtk-6.0 #include #include */ import "C" import ( "unsafe" javascriptcore "github.com/diamondburned/gotk4-webkitgtk/pkg/javascriptcore/v6" webkit "github.com/diamondburned/gotk4-webkitgtk/pkg/webkit/v6" "github.com/diamondburned/gotk4/pkg/core/glib" ) // EvaluateJavascript executes JavaScript code in the WebView in the default world // This is a wrapper around webkit_web_view_evaluate_javascript that wasn't generated by gotk4 func EvaluateJavascript(view *webkit.WebView, script string) { EvaluateJavascriptInWorld(view, script, "") } // EvaluateJavascriptInWorld executes JavaScript code in a specific world // Pass empty string for worldName to use the main/page world func EvaluateJavascriptInWorld(view *webkit.WebView, script string, worldName string) { if view == nil { return } // Get the underlying GObject pointer from the gotk4 WebView obj := glib.BaseObject(view) if obj == nil { return } nativePtr := obj.Native() if nativePtr == 0 { return } cScript := C.CString(script) defer C.free(unsafe.Pointer(cScript)) var cWorldName *C.char if worldName != "" { cWorldName = C.CString(worldName) defer C.free(unsafe.Pointer(cWorldName)) } // Call webkit_web_view_evaluate_javascript // Parameters: web_view, script, length, world_name, source_uri, cancellable, callback, user_data C.webkit_web_view_evaluate_javascript( (*C.WebKitWebView)(unsafe.Pointer(nativePtr)), cScript, C.gssize(-1), // -1 means null-terminated string cWorldName, // world_name (NULL for main world, or specific world name) nil, // source_uri (NULL) nil, // cancellable (NULL) nil, // callback (NULL for fire-and-forget) nil, // user_data (NULL) ) } // JSCValueToString converts a JSCValue from a GTK signal to a Go string func JSCValueToString(value interface{}) string { if value == nil { return "" } // Type assert to *javascriptcore.Value from gotk4 bindings jscValue, ok := value.(*javascriptcore.Value) if !ok { return "" } // Use the gotk4 String() method to convert JSCValue to string return jscValue.String() }