使用web-sys crate,我想从HTMLDocument.
我想做这样的事情。确实这行不通。
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let cookie = document.cookie().unwrap();
//no method named `cookie` found for type `web_sys::features::gen_Document::Document` in the current scope
Run Code Online (Sandbox Code Playgroud)
我需要访问HTMLDocument结构而不是Document结构。
Cargo.toml 已启用功能。
~snip~
[dependencies.web-sys]
version = "0.3.4"
features = [
"WebSocket",
'Window',
'Document',
'HtmlDocument',
]
Run Code Online (Sandbox Code Playgroud)
根据API,它应该可以在以下位置访问Window,例如Document.
它似乎不适用于以下内容:
let html_document = window.html_document().unwrap();
Run Code Online (Sandbox Code Playgroud)
从文档来看,HTMLDocument应该扩展Document。
我知道 Rust 中没有继承,但我无法将其转换Document为这样:
let html_document = web_sys::HtmlDocument::from(document);
Run Code Online (Sandbox Code Playgroud)
和功能是一样的into。
HTMLDocument通过这样的方式可以访问吗?
还有其他方法可以使用 web-sys 访问 cookie 吗?
是否是正在进行中但现在不起作用的东西?
您需要的是动态转换,这是通过以下方式完成的wasm_bindgen::JsCast::dyn_into():
use wasm_bindgen::JsCast;
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let html_document = document.dyn_into::<web_sys::HtmlDocument>().unwrap();
let cookie = html_document.cookie().unwrap();
Run Code Online (Sandbox Code Playgroud)
还有一种wasm_bindgen::JsCast::dyn_ref()不消耗原始对象的变体。