chrome_wasm_bindgen/
runtime.rs

1use crate::events::Event;
2use serde::{Deserialize, Serialize};
3use wasm_bindgen::prelude::*;
4
5#[wasm_bindgen]
6extern "C" {
7    #[wasm_bindgen(extends = js_sys::Object)]
8    #[doc = "Use the chrome.runtime API to retrieve the background page, return details about the manifest, and listen for and respond to events in the app or extension lifecycle. You can also use this API to convert the relative path of URLs to fully-qualified URLs."]
9    #[doc = ""]
10    #[doc = "@since Chrome 22"]
11    pub type Runtime;
12
13    #[wasm_bindgen(method, js_name = connect)]
14    #[doc = "Attempts to connect to connect listeners within an extension/app (such as the background page), or other extensions/apps. This is useful for content scripts connecting to their extension processes, inter-app/extension communication, and web messaging. Note that this does not connect to any listeners in a content script. Extensions may connect to content scripts embedded in tabs via tabs.connect."]
15    #[doc = ""]
16    #[doc = "@since Chrome 26."]
17    pub fn connect_with_connect_info(this: &Runtime, connect_info: JsValue) -> Port;
18}
19
20#[derive(Serialize, Deserialize)]
21pub struct ConnectInfo<'a> {
22    pub name: Option<&'a str>,
23}
24
25impl From<ConnectInfo<'_>> for JsValue {
26    fn from(value: ConnectInfo) -> Self {
27        serde_wasm_bindgen::to_value(&value).unwrap()
28    }
29}
30
31#[wasm_bindgen]
32extern "C" {
33    #[wasm_bindgen(extends = js_sys::Object)]
34    #[doc = "An object which allows two way communication with other pages."]
35    #[doc = ""]
36    #[doc = "@since Chrome 26."]
37    pub type Port;
38
39    #[wasm_bindgen(method, js_name = postMessage)]
40    pub fn post_message(this: &Port, message: JsValue);
41
42    #[wasm_bindgen(method, getter, js_name = onMessage)]
43    #[doc = "An object which allows the addition and removal of listeners for a Chrome event."]
44    pub fn on_message(this: &Port) -> Event;
45}