pub struct ScriptDocument { /* private fields */ }Expand description
A Document which executes the JavaScript contained in the document’s
<script> tags, exposing DOM APIs backed by blitz-dom to the scripts.
Construct with ScriptDocument::from_html, then call
execute_scripts (this also happens
automatically on the first poll). UI events pushed via
handle_ui_event are dispatched to JavaScript
event listeners before Blitz’s default actions run.
Implementations§
Source§impl ScriptDocument
impl ScriptDocument
Sourcepub fn from_html(html: &str, config: DocumentConfig) -> Self
pub fn from_html(html: &str, config: DocumentConfig) -> Self
Parse HTML into a ScriptDocument.
Note: this does not execute any scripts yet. Call
execute_scripts to do so (or rely on the
first poll doing it automatically).
Sourcepub fn with_fetcher(self, fetcher: impl ScriptFetcher) -> Self
pub fn with_fetcher(self, fetcher: impl ScriptFetcher) -> Self
Override the ScriptFetcher used to load external (src="...") scripts.
The default fetcher supports file: and data: URLs.
Sourcepub fn set_ipc_handler(&mut self, handler: impl Fn(String) + 'static)
pub fn set_ipc_handler(&mut self, handler: impl Fn(String) + 'static)
Install the host callback invoked by window.ipc.postMessage(body).
The callback may forward work to another thread, but JavaScript and DOM state remain on the document’s owning thread. Replacing the callback is supported before script startup.
Sourcepub fn set_poll_hook(
&mut self,
hook: impl for<'a> FnMut(&mut ScriptDocument, Option<&TaskContext<'a>>) -> bool + 'static,
)
pub fn set_poll_hook( &mut self, hook: impl for<'a> FnMut(&mut ScriptDocument, Option<&TaskContext<'a>>) -> bool + 'static, )
Install work that an embedder needs to run on the document thread during polling.
The hook runs after document scripts and due timers. Returning true requests a redraw.
Sourcepub fn add_poll_hook(
&mut self,
hook: impl for<'a> FnMut(&mut ScriptDocument, Option<&TaskContext<'a>>) -> bool + 'static,
)
pub fn add_poll_hook( &mut self, hook: impl for<'a> FnMut(&mut ScriptDocument, Option<&TaskContext<'a>>) -> bool + 'static, )
Append document-thread work without discarding an embedder’s existing poll lifecycle.
Sourcepub fn execute_scripts(&mut self)
pub fn execute_scripts(&mut self)
Execute the document’s <script> elements in document order, then fire
the DOMContentLoaded and load events.
Does nothing if scripts have already been executed.
Sourcepub fn external_script_urls(&self) -> Vec<Url>
pub fn external_script_urls(&self) -> Vec<Url>
The resolved URLs of the document’s external (<script src="...">) scripts,
in document order.
The ScriptFetcher API is synchronous, so embedders with asynchronous
networking can use this to prefetch script sources before calling
execute_scripts, and then serve them from memory
via a custom fetcher (see with_fetcher).
Sourcepub fn eval(&mut self, code: &str)
pub fn eval(&mut self, code: &str)
Evaluate arbitrary JavaScript code in the document’s script context
Sourcepub fn eval_json(&mut self, code: &str) -> Result<Value, String>
pub fn eval_json(&mut self, code: &str) -> Result<Value, String>
Evaluate JavaScript and convert its result to JSON.
Embedders use this for APIs that return an evaluation result, such as Tauri’s
eval_script_with_callback. JavaScript exceptions are recorded in the runtime diagnostics
and returned as an error.
Sourcepub fn current_url(&self) -> Option<&Url>
pub fn current_url(&self) -> Option<&Url>
Current document URL for automation and diagnostics.
Sourcepub fn page_source(&self) -> String
pub fn page_source(&self) -> String
Serialize the current document tree as HTML.
Sourcepub fn dispatch_dom_event(&mut self, event: DomEvent)
pub fn dispatch_dom_event(&mut self, event: DomEvent)
Dispatch a synthetic DOM event (e.g. a click created with
Node::synthetic_click_event)
through the document’s event driver. The event is exposed to JavaScript
event listeners, and Blitz’s default actions run unless prevented.
Trait Implementations§
Source§impl Document for ScriptDocument
impl Document for ScriptDocument
fn inner(&self) -> DocGuard<'_>
fn inner_mut(&mut self) -> DocGuardMut<'_>
Source§fn handle_ui_event(&mut self, event: UiEvent)
fn handle_ui_event(&mut self, event: UiEvent)
Source§fn poll(&mut self, task_context: Option<TaskContext<'_>>) -> bool
fn poll(&mut self, task_context: Option<TaskContext<'_>>) -> bool
BaseDocumentAuto Trait Implementations§
impl !RefUnwindSafe for ScriptDocument
impl !Send for ScriptDocument
impl !Sync for ScriptDocument
impl !UnwindSafe for ScriptDocument
impl Freeze for ScriptDocument
impl Unpin for ScriptDocument
impl UnsafeUnpin for ScriptDocument
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> MaybeBoxed<Box<T>> for T
impl<T> MaybeBoxed<Box<T>> for T
Source§fn maybe_boxed(self) -> Box<T>
fn maybe_boxed(self) -> Box<T>
Source§impl<T> MaybeBoxed<T> for T
impl<T> MaybeBoxed<T> for T
Source§fn maybe_boxed(self) -> T
fn maybe_boxed(self) -> T
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.