Confused about web_sys + clipboard usage #3223
-
tl;drlooking for full example with Clipboard usage. I'm guessing something like this is the right approach? #[cfg(web_sys_unstable_apis)]
fn somefn(//..
) {
// ..
let window = web_sys::window().expect("window");
let nav = window.navigator();
if let Some(clipboard) = nav.clipboard() {
let _ = clipboard.write_text("testingclipboard1234");
info!("text written to clipboard maybe");
} else {
warn!("no navigator clipboard");
}
/..
} But I'm unable to setup my rust analyzer to see the clipboard method. details:I'm running a bevy wasm game and I would like to populate my users clipboard. I believe it is correct if trying to use this I need to add this to my [dependencies.web-sys]
version = "0.3"
features = ["Clipboard", "Window", "Navigator", "Permissions"]
[target.wasm32-unknown-unknown]
runner = "wasm-server-runner"
[build]
rustflags = ["--cfg=web_sys_unstable_apis"] The build flag doesn't seem to be working? I'm leaving this off as it breaks my bevy app. [lib]
crate-type = ["cdylib"] Not sure if this is needed.^ Thought I should note this because it's in every example and I'm not really sure why. I also have in my rust file:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I solved my issue: use wasm_bindgen_futures::spawn_local;
let _task = spawn_local(async move {
let window = web_sys::window().expect("window"); // { obj: val };
let nav = window.navigator().clipboard();
match nav {
Some(a) => {
let p = a.write_text("please god work");
let result = wasm_bindgen_futures::JsFuture::from(p)
.await
.expect("clipboard populated");
info!("clippyboy worked");
}
None => {
warn!("failed to copy clippyboy");
}
};
}); |
Beta Was this translation helpful? Give feedback.
I solved my issue: