web_extensions_sys/
tabs.rs

1// https://developer.chrome.com/docs/extensions/reference/tabs/
2
3use crate::EventTarget;
4use js_sys::Object;
5use wasm_bindgen::prelude::*;
6
7/// The tab's ID.
8///
9/// Tab IDs are unique within a browser session.
10type TabId = i32; // `TAB_ID_NONE` has value `-1` so we have to use i32
11
12/// The ID of the window that hosts a tab.
13type WindowId = i32;
14
15/// The ID of the group that the tab belongs to.
16type GroupId = i32;
17
18/// Zero-based index of the tab within its window.
19type TabIndex = u32;
20
21#[wasm_bindgen]
22extern "C" {
23    // https://developer.chrome.com/docs/extensions/reference/tabs/#type-onActivated-callback-activeInfo
24    #[derive(Debug)]
25    pub type TabActiveInfo;
26
27    #[wasm_bindgen(method, getter, js_name = previousTabId)]
28    pub fn previous_tab_id(this: &TabActiveInfo) -> Option<TabId>;
29
30    #[wasm_bindgen(method, getter, js_name = tabId)]
31    pub fn tab_id(this: &TabActiveInfo) -> TabId;
32
33    #[wasm_bindgen(method, getter, js_name = windowId)]
34    pub fn window_id(this: &TabActiveInfo) -> WindowId;
35}
36
37#[wasm_bindgen]
38extern "C" {
39    // https://developer.chrome.com/docs/extensions/reference/tabs/#type-onDetached-callback-detachInfo
40    #[derive(Debug)]
41    pub type TabDetachInfo;
42
43    #[wasm_bindgen(method, getter, js_name = oldWindowId)]
44    pub fn old_window_id(this: &TabDetachInfo) -> WindowId;
45
46    #[wasm_bindgen(method, getter, js_name = oldPosition)]
47    pub fn old_position(this: &TabDetachInfo) -> TabIndex;
48}
49
50#[wasm_bindgen]
51extern "C" {
52    // https://developer.chrome.com/docs/extensions/reference/tabs/#type-onAttached-callback-attachInfo
53    #[derive(Debug)]
54    pub type TabAttachInfo;
55
56    #[wasm_bindgen(method, getter, js_name = newWindowId)]
57    pub fn new_window_id(this: &TabAttachInfo) -> WindowId;
58
59    #[wasm_bindgen(method, getter, js_name = newPosition)]
60    pub fn new_position(this: &TabAttachInfo) -> TabIndex;
61}
62
63#[wasm_bindgen]
64extern "C" {
65    // https://developer.chrome.com/docs/extensions/reference/tabs/#type-onMoved-callback-moveInfo
66    #[derive(Debug)]
67    pub type TabMoveInfo;
68
69    #[wasm_bindgen(method, getter, js_name = windowId)]
70    pub fn window_id(this: &TabMoveInfo) -> WindowId;
71
72    #[wasm_bindgen(method, getter, js_name = fromIndex)]
73    pub fn from_index(this: &TabMoveInfo) -> TabIndex;
74
75    #[wasm_bindgen(method, getter, js_name = toIndex)]
76    pub fn to_index(this: &TabMoveInfo) -> TabIndex;
77}
78
79#[wasm_bindgen]
80extern "C" {
81    // https://developer.chrome.com/docs/extensions/reference/tabs/#type-onRemoved-callback-removeInfo
82    #[derive(Debug)]
83    pub type TabRemoveInfo;
84
85    #[wasm_bindgen(method, getter, js_name = windowId)]
86    pub fn window_id(this: &TabRemoveInfo) -> WindowId;
87
88    #[wasm_bindgen(method, getter, js_name = isWindowClosing)]
89    pub fn is_window_closing(this: &TabRemoveInfo) -> bool;
90}
91
92#[wasm_bindgen]
93extern "C" {
94    // https://developer.chrome.com/docs/extensions/reference/tabs/#type-MutedInfo
95    #[derive(Debug)]
96    pub type TabMutedInfo;
97
98    #[wasm_bindgen(method, getter)]
99    pub fn muted(this: &TabMutedInfo) -> bool;
100
101    #[wasm_bindgen(method, getter, js_name = extensionId)]
102    pub fn extension_id(this: &TabMutedInfo) -> Option<String>;
103
104    #[wasm_bindgen(method, getter)]
105    pub fn reason(this: &TabMutedInfo) -> Option<String>;
106}
107
108#[wasm_bindgen]
109extern "C" {
110    // https://developer.chrome.com/docs/extensions/reference/tabs/#type-Tab
111    #[derive(Debug, Clone)]
112    pub type Tab;
113
114    #[wasm_bindgen(method, getter)]
115    pub fn active(this: &Tab) -> bool;
116
117    #[wasm_bindgen(method, getter)]
118    pub fn audible(this: &Tab) -> Option<bool>;
119
120    #[cfg(not(feature = "firefox"))]
121    #[wasm_bindgen(method, getter, js_name = autoDiscardable)]
122    pub fn auto_discardable(this: &Tab) -> bool;
123
124    #[cfg(feature = "firefox")]
125    #[wasm_bindgen(method, getter, js_name = autoDiscardable)]
126    pub fn auto_discardable(this: &Tab) -> Option<bool>;
127
128    #[cfg(not(feature = "firefox"))]
129    #[wasm_bindgen(method, getter)]
130    pub fn discarded(this: &Tab) -> bool;
131
132    #[cfg(feature = "firefox")]
133    #[wasm_bindgen(method, getter)]
134    pub fn discarded(this: &Tab) -> Option<bool>;
135
136    #[wasm_bindgen(method, getter, js_name = favIconUrl)]
137    pub fn fav_icon_url(this: &Tab) -> Option<String>;
138
139    #[cfg(not(feature = "firefox"))]
140    #[wasm_bindgen(method, getter, js_name = groupId)]
141    pub fn group_id(this: &Tab) -> GroupId;
142
143    #[cfg(feature = "firefox")]
144    #[wasm_bindgen(method, getter, js_name = groupId)]
145    pub fn group_id(this: &Tab) -> Option<GroupId>;
146
147    #[wasm_bindgen(method, getter)]
148    pub fn height(this: &Tab) -> Option<u32>;
149
150    #[wasm_bindgen(method, getter)]
151    pub fn highlighted(this: &Tab) -> bool;
152
153    #[wasm_bindgen(method, getter)]
154    pub fn id(this: &Tab) -> Option<TabId>;
155
156    #[wasm_bindgen(method, getter)]
157    pub fn incognito(this: &Tab) -> bool;
158
159    #[wasm_bindgen(method, getter)]
160    pub fn index(this: &Tab) -> TabIndex;
161
162    #[cfg(not(feature = "firefox"))]
163    #[wasm_bindgen(method, getter, js_name = mutedInfo)]
164    pub fn muted_info(this: &Tab) -> Option<TabMutedInfo>;
165
166    #[cfg(feature = "firefox")]
167    #[wasm_bindgen(method, getter, js_name = mutedInfo)]
168    pub fn muted_info(this: &Tab) -> TabMutedInfo;
169
170    #[wasm_bindgen(method, getter, js_name = openerTabId)]
171    pub fn opener_tab_id(this: &Tab) -> Option<TabId>;
172
173    #[wasm_bindgen(method, getter, js_name = pendingUrl)]
174    pub fn pending_url(this: &Tab) -> Option<String>;
175
176    #[wasm_bindgen(method, getter)]
177    pub fn pinned(this: &Tab) -> bool;
178
179    #[wasm_bindgen(method, getter, js_name = sessionId)]
180    pub fn session_id(this: &Tab) -> Option<String>;
181
182    #[wasm_bindgen(method, getter)]
183    pub fn status(this: &Tab) -> Option<String>;
184
185    #[wasm_bindgen(method, getter)]
186    pub fn title(this: &Tab) -> Option<String>;
187
188    #[wasm_bindgen(method, getter)]
189    pub fn url(this: &Tab) -> Option<String>;
190
191    #[wasm_bindgen(method, getter)]
192    pub fn width(this: &Tab) -> Option<u32>;
193
194    #[wasm_bindgen(method, getter, js_name = windowId)]
195    pub fn window_id(this: &Tab) -> WindowId;
196
197    // --- Firefox only --- //
198
199    #[cfg(feature = "firefox")]
200    #[wasm_bindgen(method, getter)]
201    pub fn attention(this: &Tab) -> Option<bool>;
202
203    #[cfg(feature = "firefox")]
204    #[wasm_bindgen(method, getter, js_name = cookieStoreId)]
205    pub fn cookie_store_id(this: &Tab) -> Option<String>;
206
207    #[cfg(feature = "firefox")]
208    #[wasm_bindgen(method, getter)]
209    pub fn hidden(this: &Tab) -> bool;
210
211    #[cfg(feature = "firefox")]
212    #[wasm_bindgen(method, getter, js_name = isArticle)]
213    pub fn is_article(this: &Tab) -> bool;
214
215    #[cfg(feature = "firefox")]
216    #[wasm_bindgen(method, getter, js_name = isInReaderMode)]
217    pub fn is_in_reader_mode(this: &Tab) -> bool;
218
219    #[cfg(feature = "firefox")]
220    #[wasm_bindgen(method, getter, js_name = lastAccessed)]
221    pub fn last_accessed(this: &Tab) -> f64;
222
223    #[cfg(feature = "firefox")]
224    #[wasm_bindgen(method, getter, js_name = successorId)]
225    pub fn successor_id(this: &Tab) -> Option<TabId>;
226
227}
228
229#[wasm_bindgen]
230extern "C" {
231    // https://developer.chrome.com/docs/extensions/reference/tabs/#method
232    pub type Tabs;
233
234    #[wasm_bindgen(method, getter, js_name = TAB_ID_NONE)]
235    pub fn tab_id_none(this: &Tabs) -> TabId;
236
237    #[wasm_bindgen(catch, method, js_name = captureTab)]
238    pub async fn capture_tab(
239        this: &Tabs,
240        tab_id: Option<TabId>,
241        info: Option<&Object>,
242    ) -> Result<JsValue, JsValue>;
243
244    #[wasm_bindgen(catch, method, js_name = captureVisibleTab)]
245    pub async fn capture_visible_tab(
246        this: &Tabs,
247        window_id: Option<WindowId>,
248        info: Option<&Object>,
249    ) -> Result<JsValue, JsValue>;
250
251    #[wasm_bindgen(catch, method)]
252    pub async fn connect(
253        this: &Tabs,
254        tab_id: TabId,
255        info: Option<&Object>,
256    ) -> Result<JsValue, JsValue>;
257
258    #[wasm_bindgen(catch, method)]
259    pub async fn create(this: &Tabs, info: &Object) -> Result<JsValue, JsValue>;
260
261    #[wasm_bindgen(catch, method)]
262    pub async fn discard(this: &Tabs, tab_ids: &JsValue) -> Result<JsValue, JsValue>;
263
264    #[wasm_bindgen(catch, method)]
265    pub async fn duplicate(this: &Tabs, tab_id: TabId) -> Result<JsValue, JsValue>;
266
267    #[wasm_bindgen(catch, method)]
268    pub async fn get(this: &Tabs, tab_id: TabId) -> Result<JsValue, JsValue>;
269
270    #[wasm_bindgen(catch, method, js_name = getCurrent)]
271    pub async fn get_current(this: &Tabs) -> Result<JsValue, JsValue>;
272
273    #[wasm_bindgen(catch, method, js_name = getZoom)]
274    pub async fn get_zoom(this: &Tabs, tab_id: Option<TabId>) -> Result<JsValue, JsValue>;
275
276    #[wasm_bindgen(catch, method, js_name = getZoomSettings)]
277    pub async fn get_zoom_settings(this: &Tabs, tab_id: Option<TabId>) -> Result<JsValue, JsValue>;
278
279    #[wasm_bindgen(catch, method)]
280    pub async fn hide(this: &Tabs, tab_ids: &JsValue) -> Result<JsValue, JsValue>;
281
282    #[wasm_bindgen(catch, method)]
283    pub async fn highlight(this: &Tabs, info: &Object) -> Result<JsValue, JsValue>;
284
285    #[wasm_bindgen(catch, method, js_name = insertCSS)]
286    pub async fn insert_css(
287        this: &Tabs,
288        tab_id: Option<TabId>,
289        info: &Object,
290    ) -> Result<JsValue, JsValue>;
291
292    #[wasm_bindgen(catch, method, js_name = move)]
293    pub async fn move_(this: &Tabs, tab_ids: &JsValue, info: &Object) -> Result<JsValue, JsValue>;
294
295    #[wasm_bindgen(catch, method, js_name = moveInSuccession)]
296    pub async fn move_in_succession(
297        this: &Tabs,
298        tab_ids: &JsValue,
299        tab_id: Option<TabId>,
300        info: Option<&Object>,
301    ) -> Result<JsValue, JsValue>;
302
303    #[wasm_bindgen(method)]
304    pub fn print(this: &Tabs);
305
306    #[wasm_bindgen(catch, method, js_name = printPreview)]
307    pub async fn print_preview(this: &Tabs) -> Result<JsValue, JsValue>;
308
309    #[wasm_bindgen(catch, method)]
310    pub async fn query(this: &Tabs, info: &Object) -> Result<JsValue, JsValue>;
311
312    #[wasm_bindgen(catch, method)]
313    pub async fn reload(
314        this: &Tabs,
315        tab_id: Option<TabId>,
316        info: Option<&Object>,
317    ) -> Result<JsValue, JsValue>;
318
319    #[wasm_bindgen(catch, method)]
320    pub async fn remove(this: &Tabs, tab_ids: &JsValue) -> Result<JsValue, JsValue>;
321
322    #[wasm_bindgen(catch, method, js_name = removeCSS)]
323    pub async fn remove_css(
324        this: &Tabs,
325        tab_id: Option<TabId>,
326        info: &Object,
327    ) -> Result<JsValue, JsValue>;
328
329    #[wasm_bindgen(catch, method, js_name = saveAsPDF)]
330    pub async fn save_as_pdf(this: &Tabs, info: &Object) -> Result<JsValue, JsValue>;
331
332    #[wasm_bindgen(catch, method, js_name = sendMessage)]
333    pub async fn send_message(
334        this: &Tabs,
335        tab_id: TabId,
336        message: &JsValue,
337        info: Option<&Object>,
338    ) -> Result<JsValue, JsValue>;
339
340    #[wasm_bindgen(catch, method, js_name = setZoom)]
341    pub async fn set_zoom(
342        this: &Tabs,
343        tab_id: Option<TabId>,
344        zoom_factor: f64,
345    ) -> Result<JsValue, JsValue>;
346
347    #[wasm_bindgen(catch, method, js_name = setZoomSettings)]
348    pub async fn set_zoom_settings(
349        this: &Tabs,
350        tab_id: Option<TabId>,
351        info: &Object,
352    ) -> Result<JsValue, JsValue>;
353
354    #[wasm_bindgen(catch, method)]
355    pub async fn show(this: &Tabs, tab_ids: &JsValue) -> Result<JsValue, JsValue>;
356
357    #[wasm_bindgen(catch, method, js_name = toggleReaderMode)]
358    pub async fn toggle_reader_mode(this: &Tabs, tab_id: Option<TabId>)
359        -> Result<JsValue, JsValue>;
360
361    #[wasm_bindgen(catch, method)]
362    pub async fn update(
363        this: &Tabs,
364        tab_id: Option<TabId>,
365        info: &Object,
366    ) -> Result<JsValue, JsValue>;
367
368    #[wasm_bindgen(catch, method, js_name = detectLanguage)]
369    pub async fn detect_language(this: &Tabs, tab_id: Option<TabId>) -> Result<JsValue, JsValue>;
370
371    #[wasm_bindgen(method, getter, js_name = onActivated)]
372    pub fn on_activated(this: &Tabs) -> EventTarget;
373
374    #[wasm_bindgen(method, getter, js_name = onAttached)]
375    pub fn on_attached(this: &Tabs) -> EventTarget;
376
377    #[wasm_bindgen(method, getter, js_name = onCreated)]
378    pub fn on_created(this: &Tabs) -> EventTarget;
379
380    #[wasm_bindgen(method, getter, js_name = onDetached)]
381    pub fn on_detached(this: &Tabs) -> EventTarget;
382
383    #[wasm_bindgen(method, getter, js_name = onHighlighted)]
384    pub fn on_highlighted(this: &Tabs) -> EventTarget;
385
386    #[wasm_bindgen(method, getter, js_name = onMoved)]
387    pub fn on_moved(this: &Tabs) -> EventTarget;
388
389    #[wasm_bindgen(method, getter, js_name = onRemoved)]
390    pub fn on_removed(this: &Tabs) -> EventTarget;
391
392    #[wasm_bindgen(method, getter, js_name = onReplaced)]
393    pub fn on_replaced(this: &Tabs) -> EventTarget;
394
395    #[wasm_bindgen(method, getter, js_name = onUpdated)]
396    pub fn on_updated(this: &Tabs) -> EventTarget;
397
398    #[wasm_bindgen(method, getter, js_name = onZoomChange)]
399    pub fn on_zoom_change(this: &Tabs) -> EventTarget;
400}
401
402#[wasm_bindgen]
403extern "C" {
404    // https://developer.chrome.com/docs/extensions/reference/tabs/#type-onUpdated-callback-changeInfo
405    #[derive(Debug)]
406    pub type TabChangeInfo;
407
408    // The tab's new audible state.
409    #[wasm_bindgen(method, getter)]
410    pub fn audible(this: &TabChangeInfo) -> Option<bool>;
411
412    // The tab's new auto-discardable state.
413    #[wasm_bindgen(method, getter, js_name = autoDiscardable)]
414    pub fn auto_discardable(this: &TabChangeInfo) -> Option<bool>;
415
416    // The tab's new discarded state.
417    #[wasm_bindgen(method, getter)]
418    pub fn discarded(this: &TabChangeInfo) -> Option<bool>;
419
420    // The tab's new favicon URL.
421    #[wasm_bindgen(method, getter, js_name = favIconUrl)]
422    pub fn fav_icon_url(this: &TabChangeInfo) -> Option<String>;
423
424    // The tab's new group.
425    #[wasm_bindgen(method, getter, js_name = groupId)]
426    pub fn group_id(this: &TabChangeInfo) -> Option<GroupId>;
427
428    // The tab's new muted state and the reason for the change.
429    #[wasm_bindgen(method, getter, js_name = mutedInfo)]
430    pub fn muted_info(this: &TabChangeInfo) -> Option<TabMutedInfo>;
431
432    // The tab's new pinned state.
433    #[wasm_bindgen(method, getter)]
434    pub fn pinned(this: &TabChangeInfo) -> Option<bool>;
435
436    // The tab's loading status.
437    #[wasm_bindgen(method, getter)]
438    pub fn status(this: &TabChangeInfo) -> Option<String>;
439
440    // The tab's new title.
441    #[wasm_bindgen(method, getter)]
442    pub fn title(this: &TabChangeInfo) -> Option<String>;
443
444    // The tab's URL if it has changed.
445    #[wasm_bindgen(method, getter)]
446    pub fn url(this: &TabChangeInfo) -> Option<String>;
447}
448
449#[wasm_bindgen]
450extern "C" {
451    // https://developer.chrome.com/docs/extensions/reference/tabs/#type-onHighlighted-callback-highlightInfo
452    #[derive(Debug)]
453    pub type TabHighlightInfo;
454
455    // All highlighted tabs in the window.
456    #[wasm_bindgen(method, getter, js_name = tabIds)]
457    pub fn tab_ids(this: &TabHighlightInfo) -> JsValue;
458
459    // The window whose tabs changed.
460    #[wasm_bindgen(method, getter, js_name = windowId)]
461    pub fn window_id(this: &TabHighlightInfo) -> WindowId;
462}
463
464#[wasm_bindgen]
465extern "C" {
466    // https://developer.chrome.com/docs/extensions/reference/tabs/#type-onZoomChange-callback-ZoomChangeInfo
467    #[derive(Debug)]
468    pub type TabZoomChangeInfo;
469
470    #[wasm_bindgen(method, getter, js_name = newZoomFactor)]
471    pub fn new_zoom_factor(this: &TabZoomChangeInfo) -> f64;
472
473    #[wasm_bindgen(method, getter, js_name = oldZoomFactor)]
474    pub fn old_zoom_factor(this: &TabZoomChangeInfo) -> f64;
475
476    #[wasm_bindgen(method, getter, js_name = tabId)]
477    pub fn tab_id(this: &TabZoomChangeInfo) -> TabId;
478
479    #[wasm_bindgen(method, getter, js_name = zoomSettings)]
480    pub fn zoom_settings(this: &TabZoomChangeInfo) -> JsValue;
481}