-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
help mapping chrome.tabs.query callback #1910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This happens because we don't currently support pub fn query(query_info: &QueryInfo, callback: &dyn Fn(&js_sys::Array)); Then after this PR is accepted, you'll be able to use |
Thanks! I reread https://rustwasm.github.io/docs/wasm-bindgen/reference/arbitrary-data-with-serde.html and it looks like I can go to and from a JsValue using serde_wasm_bindgen, such as use serde::{Serialize, Deserialize};
use wasm_bindgen::prelude::*;
#[derive(Serialize, Debug)]
pub struct QueryInfo {
pub active: Option<bool>
}
#[derive(Deserialize, Debug)]
pub struct Tab {
pub active: bool,
pub id: u32,
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = "chrome.tabs")]
pub fn query(query_info: &JsValue, callback: &dyn Fn(JsValue));
} |
It took me a while to figure out the lifetimes with the closures, but I'm happy with the result. Great job so far on this library! Usage chrome_tabs::query(&chrome_tabs::QueryInfo{active: None}, &|tabs: Vec<Tab>|
web_sys::console::log_1(&format!("tabs: {:#?}", tabs).into()));
use serde::{Serialize, Deserialize};
use wasm_bindgen::prelude::*;
#[derive(Serialize, Debug)]
pub struct QueryInfo {
pub active: Option<bool>
}
#[derive(Deserialize, Debug)]
pub struct Tab {
pub active: bool,
pub id: u32,
}
#[wasm_bindgen]
extern "C" {
type Tabs;
#[wasm_bindgen(method)]
fn query(this: &Tabs, query_info: &JsValue, callback: JsValue);
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = chrome)]
static tabs: Tabs;
}
pub fn query(query_info: &QueryInfo, callback: &'static dyn Fn(Vec<Tab>)){
tabs.query(&serde_wasm_bindgen::to_value(query_info).unwrap(), Closure::once_into_js(move |v: JsValue|
callback(serde_wasm_bindgen::from_value(v).unwrap())
));
} I'm deferring the error handling until later. #1742 looks like a good idea. There is too many |
How would I call [chrome.tabs.query] to get back an array of tabs. I'm struggling to write the interop for this.
https://developer.chrome.com/extensions/tabs#method-query
http://definitelytyped.org/docs/chrome--chrome/interfaces/chrome.tabs.tab.html
I created a module named
chrome_tabs.rs
and it currently has:Errors with:
I'm using nightly because I was looking at
wasm-bindgen/tests/wasm/closures.rs
Lines 475 to 491 in 8ba0142
If I do not make Tab a reference, then I get this error:
The text was updated successfully, but these errors were encountered: