web_extensions/tabs/
mod.rs

1//! Wrapper for the [`chrome.tabs` API](https://developer.chrome.com/docs/extensions/reference/tabs/).
2
3pub(crate) mod prelude {
4    pub(crate) use crate::util::{js_from_serde, object_from_js, serde_from_js_result};
5    pub use crate::{event_listener::EventListener, tabs::TabId, Error};
6    pub use serde::{Deserialize, Serialize};
7    pub use wasm_bindgen::closure::Closure;
8    pub use web_extensions_sys as sys;
9
10    pub fn tabs() -> sys::Tabs {
11        // Currently we assume a chrome browser and Manifest V3.
12        //
13        // Once MV3 is supported by FireFox, we need to check if we can use the same namespace,
14        // a shim or our own implementation.
15        sys::chrome().tabs()
16    }
17}
18
19use self::prelude::*;
20
21/// The ID of the tab.
22///
23/// Tab IDs are unique within a browser session.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
25pub struct TabId(i32);
26
27impl From<i32> for TabId {
28    fn from(id: i32) -> Self {
29        Self(id)
30    }
31}
32
33mod on_activated;
34mod on_attached;
35mod on_created;
36mod on_detached;
37mod on_highlighted;
38mod on_moved;
39mod on_removed;
40mod on_replaced;
41mod on_updated;
42mod on_zoom_change;
43
44mod muted_info;
45mod query_details;
46mod status;
47mod tab;
48mod window_type;
49
50pub use self::{
51    muted_info::*, on_activated::*, on_attached::*, on_created::*, on_detached::*,
52    on_highlighted::*, on_moved::*, on_removed::*, on_replaced::*, on_updated::*,
53    on_zoom_change::*, query_details::*, status::*, tab::*, window_type::*,
54};
55
56/// <https://developer.chrome.com/docs/extensions/reference/tabs/#method-get>
57pub async fn get(tab_id: TabId) -> Result<Tab, Error> {
58    let result = tabs().get(tab_id.0).await;
59    serde_from_js_result(result)
60}
61
62/// <https://developer.chrome.com/docs/extensions/reference/tabs/#method-query>
63pub async fn query(details: &QueryDetails<'_>) -> Result<Vec<Tab>, Error> {
64    let js_details = js_from_serde(details)?;
65    let result = tabs().query(object_from_js(&js_details)?).await;
66    serde_from_js_result(result)
67}
68
69/// <https://developer.chrome.com/docs/extensions/reference/tabs/#method-sendMessage>
70pub async fn send_message<T>(tab_id: TabId, message: &T) -> Result<(), Error>
71where
72    T: Serialize,
73{
74    let js_message = js_from_serde(message)?;
75    let options = None;
76    tabs()
77        .send_message(tab_id.0, &js_message, options)
78        .await
79        .map(|_| ())?;
80    Ok(())
81}
82
83/// <https://developer.chrome.com/docs/extensions/reference/tabs/#method-create>
84pub async fn create(props: CreateProperties<'_>) -> Result<Tab, Error> {
85    let js_props = js_from_serde(&props)?;
86    let result = tabs().create(object_from_js(&js_props)?).await;
87    serde_from_js_result(result)
88}
89
90/// Information necessary to open a new tab.
91#[derive(Debug, Serialize, Deserialize)]
92pub struct CreateProperties<'a> {
93    pub active: bool,
94    pub url: &'a str,
95}