WebviewOwebview — OCaml bindings for webview 0.12.
Open a native window backed by the operating system's web engine, load HTML/CSS/JS, and call back and forth between the page's JavaScript and OCaml — no Electron, no bundler.
let () =
let w = Webview.create () in
Webview.set_title w "Hello";
Webview.set_size w ~width:480 ~height:320 Webview.Hint_none;
Webview.set_html w "<h1>Hello from OCaml</h1>";
Webview.run w;
Webview.destroy wval create : ?debug:bool -> unit -> tcreate ?debug () creates a new webview. When debug is true the developer tools are enabled (default false).
val destroy : t -> unitDestroy the webview and free associated resources.
val run : t -> unitRun the main loop. Blocks the calling thread until the window is closed and must be called on the main thread. The OCaml runtime lock is released for the duration so other threads keep running.
val set_title : t -> string -> unitset_title w title sets the window title.
set_size w ~width ~height hint sets the window size in pixels; hint controls how the size is interpreted (see hint).
Navigate to a URL (supports http://, https://, file://, data:).
val set_html : t -> string -> unitLoad the given HTML string as the document.
val init : t -> string -> unitInject JS to be run on every page load, before page scripts.
val eval : t -> string -> unitEvaluate JS in the current page.
Expose OCaml functions to the page as window.<name>(...) and resolve the JavaScript promises they return.
val bind : t -> string -> (string -> string -> unit) -> unitbind w name f exposes a JS function window.name(...) that calls back into f id req, where req is a JSON array string of the JS arguments. The callback must eventually answer with return (using id).
The closure is kept alive as a GC root until the binding is removed with unbind or the webview is destroyed. Raises Failure if a binding with the same name already exists.
val unbind : t -> string -> unitunbind w name removes the binding name created with bind, releasing the closure's GC root. Raises Failure if no such binding exists.
val return : t -> string -> error:bool -> result:string -> unitreturn w id ~error ~result resolves (or rejects, if error) the JS promise associated with the call id. result must be a JSON value.
type version_info = {major : int;minor : int;patch : int;version_number : string;SemVer "MAJOR.MINOR.PATCH" string
pre_release : string;SemVer pre-release labels, or ""
build_metadata : string;SemVer build metadata, or ""
}The library's version information, as returned by version.
val version : unit -> version_infoThe webview library's version information.
The kind of native handle to retrieve with get_native_handle, mirrors WEBVIEW_NATIVE_HANDLE_KIND_*.
val get_window : t -> nativeintget_window w returns the native top-level window handle as a pointer (0n if unavailable). Interpret it with platform-specific FFI.
val get_native_handle : t -> native_handle_kind -> nativeintget_native_handle w kind returns the requested native handle as a pointer (0n if unavailable).