Created
June 9, 2022 02:06
-
-
Save AceofSpades5757/88975a727fe62a169b5fc04271500ad1 to your computer and use it in GitHub Desktop.
In a browser, set the clipboard (WASM)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "set-web-clipboard" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
# JavaScript in Rust | |
wasm-bindgen = "0.2" # May not be needed | |
web-sys = { version = "0.3", features = ["Clipboard"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! Reference: <https://docs.rs/web-sys/0.3.57/web_sys/struct.Clipboard.html> | |
#[cfg(web_sys_unstable_apis)] | |
fn set_clipboard(text: String) { | |
use wasm_bindgen::prelude::*; | |
#[wasm_bindgen] | |
extern "C" { | |
#[wasm_bindgen(js_namespace = ["navigator", "clipboard"])] | |
fn writeText(s: &str); | |
} | |
writeText(text.as_str()); | |
} | |
#[cfg(not(web_sys_unstable_apis))] | |
fn set_clipboard(text: String) { | |
// This is an unstable Web API. This package needs to be compiled as such. | |
console::log("Package compiled without unstable APIs."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to the Yew community for helping with this, quickly and politely. I couldn't find a straightforward solution, so I created this Gist for others.
Feel free to leave comments to anyone with questions.