racket-webview
| (require racket-webview/racket-webview) | |
| package: racket-webview | |
Higher-level interface built on top of racket-webview/racket-webview-qt.
This module provides a structured programming model around the lower-level webview bindings. It introduces contexts, a local HTTPS server, JSON-based event handling, and DOM and JavaScript utilities.
1 Architecture
The module builds on the lower-level bindings from racket-webview/racket-webview-qt. It adds:
structured Racket values instead of raw strings
JSON decoding of events and JavaScript results
a local HTTPS server per context
convenience functions for DOM manipulation
A context encapsulates a native webview context together with a local HTTPS server. Windows are created within a context and communicate through events and JavaScript calls. When an HTML file is served, the context’s CSS boilerplate is injected immediately before the closing </head> tag.
2 Contexts
The function:
starts a local HTTPS server on a dynamically chosen port
generates a self-signed certificate
creates a native context
installs the provided JavaScript boilerplate in the native context
stores the provided CSS boilerplate in the context
The file-getter procedure maps request paths to files. The #:boilerplate-js argument provides JavaScript support code passed to the native context constructor. The javascript is injected by the native QtWebEngine software. The #:boilerplate-css argument provides CSS boilerplate that is stored in the context and injected into served HTML documents.
Certificate files are removed automatically when the context is garbage collected.
This URL can be used to construct URLs from relative path information.
3 Windows
procedure
(webview-create context url-path event-callback [ #:parent parent]) → wv-win? context : wv-context? url-path : string? event-callback : procedure? parent : (or/c wv-win? #f) = #f
a native window is created
an event handler is installed
the context security token is applied
navigation is performed via the local server
The callback is invoked as:
(event-callback wv evt)
where evt is a parsed event hash table.
If parent is provided, the new window becomes a child of that window.
4 Navigation and Content
X-expressions are converted to strings before being passed to JavaScript.
The menu is converted to JSON using wv-menu->json and then passed to the browser by evaluating JavaScript through webview-run-js.
The result is the symbol returned by webview-run-js.
5 JavaScript
If the lower-level call fails or the result does not match the expected structure, an exception is raised.
6 Dialogs
Dialog functions return immediately. Results are delivered asynchronously via events.
procedure
(webview-messagebox wv type title message [ #:sub submessage]) → symbol? wv : wv-win? type : symbol? title : string? message : string? submessage : string? = ""
The user response is delivered asynchronously via events. If dismissed or canceled, this is indicated through the event.
procedure
wv : wv-win? title : string? base-dir : (or/c path? string?)
The selected directory is delivered asynchronously via events. If canceled, this is indicated through the event.
procedure
(webview-file-open wv title base-dir permitted-exts) → symbol? wv : wv-win? title : string? base-dir : (or/c path? string?) permitted-exts : (or/c wv-permitted-exts? wv-list-of-permitted-exts?)
The selected file is delivered asynchronously via events. If canceled, this is indicated through the event.
procedure
(webview-file-save wv title base-dir permitted-exts) → symbol? wv : wv-win? title : string? base-dir : (or/c path? string?) permitted-exts : (or/c wv-permitted-exts? wv-list-of-permitted-exts?)
The file to be saved is delivered asynchronously via events. If canceled, this is indicated through the event.
7 DOM Interaction
7.1 Selectors and Element Identifiers
Many functions accept either an element identifier or a CSS selector.
If a symbol is provided, it is interpreted as an element id. The symbol is converted to a string and prefixed with "#".
If a string is provided, it is used directly as a CSS selector.
a symbol refers to a single element by id
a string may refer to one or more elements
CSS selector semantics are those of the browser engine
Functions apply their effect to all matched elements.
7.2 Event Binding
procedure
wv : wv-win? selector : (or/c symbol? string?) event : (or/c symbol? list-of-symbol?)
If selector is a symbol, it is interpreted as an element id and converted to a CSS selector by prefixing "#".
The result is a list describing the installed bindings.
procedure
wv : wv-win? selector : (or/c symbol? string?) event : (or/c symbol? list-of-symbol?)
Selector handling is the same as for webview-bind!. The result is a list describing the removed bindings.
7.3 DOM Values
procedure
wv : wv-win? id : symbol?
val :
(or/c symbol? string? number? boolean? g:date? g:time? g:datetime? rgba?)
For checkboxes and radio buttons, the checked state is updated. Other elements receive a string value. Dates, times, datetimes, and colors are converted to strings before being passed to JavaScript.
For checkboxes and radio buttons, the result reflects the checked state. Other elements return their value property.
7.4 Classes, Styles
procedure
wv : wv-win? selector : (or/c symbol? string?) class : (or/c symbol? string? list?)
If selector is a symbol, it is treated as an element id. If it is a string, it is used directly as a CSS selector.
The effect is applied to all matched elements.
procedure
wv : wv-win? selector : (or/c symbol? string?) class : (or/c symbol? string? list?)
Selector handling is the same as for webview-add-class!.
procedure
(webview-set-style! wv selector style-entries) → hash? wv : wv-win? selector : (or/c symbol? string?) style-entries : (or/c kv? list-of-kv?)
The effect is applied to all elements matched by selector.
procedure
(webview-unset-style! wv selector style-entries) → hash? wv : wv-win? selector : (or/c symbol? string?) style-entries : (or/c symbol? list-of-symbol?)
The effect is applied to all elements matched by selector.
procedure
wv : wv-win? selector : (or/c symbol? string?) styles : (or/c symbol? list-of-symbol?)
If selector is a symbol, the result for that single element is returned directly. Otherwise the result covers all matched elements.
7.5 Attributes
procedure
wv : wv-win? selector : (or/c symbol? string?) attr-entries : (or/c kv? list-of-kv?)
Dates, times, datetimes, and colors are converted to strings before being passed to JavaScript. The effect is applied to all elements matched by selector.
procedure
wv : wv-win? id : symbol? attr : (or/c symbol? string?)
If the JavaScript result is null, the function returns #f.
procedure
wv : wv-win? id : symbol? attr : (or/c symbol? string?)
procedure
wv : wv-win? id : symbol? attr : (or/c symbol? string?)
procedure
wv : wv-win? id : symbol? attr : (or/c symbol? string?)
procedure
wv : wv-win? id : symbol? attr : (or/c symbol? string?)
procedure
wv : wv-win? id : symbol? attr : (or/c symbol? string?)
procedure
wv : wv-win? id : symbol? attr : (or/c symbol? string?)
procedure
wv : wv-win? id : symbol? attr : (or/c symbol? string?)
7.6 Inner HTML
procedure
wv : wv-win? id : symbol? html : (or/c string? xexpr?)
Returns 'oke if the injected JavaScript yields a true value, and 'failed otherwise.
8 File Filters
struct
(struct wv-permitted-exts (name exts))
name : string? exts : (listof symbol?)
procedure
name : string? exts : (listof symbol?)
9 Utilities
The result is constructed by concatenating the contents of js/*.js. If an additional procedure is supplied, its returned string is appended to that JavaScript.
The result is constructed by concatenating the contents of js/*.css. If an additional procedure is supplied, its returned string is appended to that CSS.
procedure
(webview-standard-file-getter base-path #:not-exist f) → procedure? base-path : path-string? f : procedure?
10 Diagnostics
The native log file path can be obtained via webview-info.