Clone in a terminal:
Commit 67885fc
Johan Mattsson - 2026-06-08T18:48:02+00:00
67885fc9d502a87faf9cf1a9dd7204d7a2dfe67bjohan.mattsson.m@gmail.com

Cookie and input value Some helpers.

Changed files (3)

Diff

diff --git a/Cargo.toml b/Cargo.tomlindex cfa88b7..19d3b79 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -9,7 +9,7 @@ gloo-console = "0.4.0" kaja-html-macro = "1.0.3" kaja-callback-macro = { version = "0.1.0", path = "../kaja-callback-macro" } wasm-bindgen = "0.2.122"-web-sys = { version = "0.3.99", features = ["Element", "HtmlElement", "NodeList"] }+web-sys = { version = "0.3.99", features = ["Document", "HtmlDocument", "Element", "HtmlElement", "HtmlInputElement", "NodeList"] } serde = { version = "1.0", features = ["derive"] } serde-wasm-bindgen = "0.5" inventory = "0.3.24"diff --git a/examples/callbacks/src/lib.rs b/examples/callbacks/src/lib.rsindex 761d52a..664d507 100644--- a/examples/callbacks/src/lib.rs+++ b/examples/callbacks/src/lib.rs@@ -30,6 +30,7 @@ fn update() {         </button>          <br />+        <br />         <textarea onkeydown="processInput(event)">Key down events?</textarea>     }}; diff --git a/src/lib.rs b/src/lib.rsindex 917268e..5fdfa28 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -1,6 +1,6 @@ use gloo_console::error; use wasm_bindgen::JsCast;-use web_sys::{Element, HtmlElement, NodeList};+use web_sys::{window, Element, HtmlDocument, HtmlElement, HtmlInputElement, NodeList};  pub mod prelude; @@ -29,7 +29,7 @@ inventory::collect!(CallbackRegistration); ///     init_callbacks(); /// } ///-/// The Rust functions must be marked with the `#[callback]` attribute to be registered.+/// The Rust functions is marked with the `#[callback]` attribut. /// ```rust /// #[callback(test)] /// pub fn test() {@@ -125,7 +125,7 @@ pub fn inner_html(selector: &str, html: &str) -> Result<()> { }  /// Sets the inner text of the elements matching the given selector.-/// Returns an error if no element is found.+/// Returns an error if no element are found. pub fn inner_text(selector: &str, text: &str) -> Result<()> {     let window = web_sys::window().ok_or(Error)?;     let document = window.document().ok_or(Error)?;@@ -147,6 +147,23 @@ pub fn inner_text(selector: &str, text: &str) -> Result<()> {     Ok(()) } +/// Get value from input tag+pub fn get_value(selector: &str) -> Option<String> {+    let element = get_element(selector)?;++    if let Ok(input) = element.dyn_into::<HtmlInputElement>() {+        return Some(input.value());+    }++    None+}++/// Get value from input tag, or return an empty string if no value is found.+pub fn try_get_value(selector: &str) -> String {+    let value = get_value(selector);+    return value.unwrap_or_default();+}+ /// Sets the inner HTML of the first element matching the given selector, if one exists. pub fn try_inner_html(selector: &str, html: &str) {     let _ = inner_html(selector, html);@@ -163,3 +180,25 @@ fn node_list_to_vec(list: NodeList) -> Vec<HtmlElement> {         .filter_map(|node| node.dyn_into::<HtmlElement>().ok())         .collect() }++/// Get a value set in any cookie.+pub fn get_cookie_value(name: &str) -> Option<String> {+    let window = web_sys::window().ok_or(Error).ok()?;+    let document = window.document().ok_or(Error).ok()?;++    let cookies = if let Ok(html_doc) = document.dyn_into::<HtmlDocument>() {+        html_doc.cookie().unwrap_or_default()+    } else {+        String::new()+    };++    for cookie in cookies.split(';') {+        let cookie = cookie.trim();+        if let Some((key, value)) = cookie.split_once('=') {+            if key == name {+                return Some(value.to_string());+            }+        }+    }+    None+}

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112