chrome_wasm_bindgen/
devtools.rs

1use serde::{Deserialize, Serialize};
2use wasm_bindgen::prelude::*;
3
4#[wasm_bindgen]
5extern "C" {
6    #[wasm_bindgen(extends = js_sys::Object)]
7    pub type Devtools;
8
9    #[wasm_bindgen(method, getter, js_name = inspectedWindow)]
10    pub fn inspected_window(this: &Devtools) -> InspectedWindow;
11}
12
13#[wasm_bindgen]
14extern "C" {
15    #[wasm_bindgen(extends = js_sys::Object)]
16    #[doc = "Use the chrome.devtools.inspectedWindow API to interact with the inspected window: obtain the tab ID for the inspected page, evaluate the code in the context of the inspected window, reload the page, or obtain the list of resources within the page."]
17    #[doc = ""]
18    #[doc = "Availability: Since Chrome 18."]
19    pub type InspectedWindow;
20
21    #[wasm_bindgen(method, getter, js_name = tabId)]
22    #[doc = "The ID of the tab being inspected. This ID may be used with chrome.tabs.* API."]
23    pub fn tab_id(this: &InspectedWindow) -> u32;
24
25    #[wasm_bindgen(method)]
26    #[doc = "Reloads the inspected page."]
27    pub fn reload(this: &InspectedWindow);
28
29    #[wasm_bindgen(method, js_name = reload)]
30    #[doc = "Reloads the inspected page."]
31    pub fn reload_with_options(this: &InspectedWindow, options: JsValue);
32
33    #[wasm_bindgen(method, js_name = eval)]
34    #[doc = "Evaluates a JavaScript expression in the context of the main frame of the inspected page. The expression must evaluate to a JSON-compliant object, otherwise an exception is thrown. The eval function can report either a DevTools-side error or a JavaScript exception that occurs during evaluation. In either case, the result parameter of the callback is undefined. In the case of a DevTools-side error, the isException parameter is non-null and has isError set to true and code set to an error code. In the case of a JavaScript error, isException is set to true and value is set to the string value of thrown object."]
35    #[doc = ""]
36    #[doc = "@param expression An expression to evaluate."]
37    pub fn eval_with_expression(this: &InspectedWindow, expression: &str);
38
39    #[wasm_bindgen(method, js_name = eval)]
40    #[doc = "Evaluates a JavaScript expression in the context of the main frame of the inspected page. The expression must evaluate to a JSON-compliant object, otherwise an exception is thrown. The eval function can report either a DevTools-side error or a JavaScript exception that occurs during evaluation. In either case, the result parameter of the callback is undefined. In the case of a DevTools-side error, the isException parameter is non-null and has isError set to true and code set to an error code. In the case of a JavaScript error, isException is set to true and value is set to the string value of thrown object."]
41    #[doc = ""]
42    #[doc = "@param expression An expression to evaluate."]
43    #[doc = ""]
44    #[doc = "@param callback A function called when evaluation completes."]
45    #[doc = ""]
46    #[doc = "Parameter result: The result of evaluation."]
47    #[doc = ""]
48    #[doc = "Parameter exceptionInfo: An object providing details if an exception occurred while evaluating the expression."]
49    pub fn eval_with_expression_and_callback(this: &InspectedWindow, callback: js_sys::Function);
50
51    #[wasm_bindgen(method, js_name = eval)]
52    #[doc = "Evaluates a JavaScript expression in the context of the main frame of the inspected page. The expression must evaluate to a JSON-compliant object, otherwise an exception is thrown. The eval function can report either a DevTools-side error or a JavaScript exception that occurs during evaluation. In either case, the result parameter of the callback is undefined. In the case of a DevTools-side error, the isException parameter is non-null and has isError set to true and code set to an error code. In the case of a JavaScript error, isException is set to true and value is set to the string value of thrown object."]
53    #[doc = ""]
54    #[doc = "@param expression An expression to evaluate."]
55    #[doc = ""]
56    #[doc = "@param options The options parameter can contain one or more options."]
57    pub fn eval_with_expression_and_options(this: &InspectedWindow, options: JsValue);
58
59    #[wasm_bindgen(method, js_name = eval)]
60    #[doc = "Evaluates a JavaScript expression in the context of the main frame of the inspected page. The expression must evaluate to a JSON-compliant object, otherwise an exception is thrown. The eval function can report either a DevTools-side error or a JavaScript exception that occurs during evaluation. In either case, the result parameter of the callback is undefined. In the case of a DevTools-side error, the isException parameter is non-null and has isError set to true and code set to an error code. In the case of a JavaScript error, isException is set to true and value is set to the string value of thrown object."]
61    #[doc = ""]
62    #[doc = "@param expression An expression to evaluate."]
63    #[doc = ""]
64    #[doc = "@param options The options parameter can contain one or more options."]
65    #[doc = ""]
66    #[doc = "@param callback A function called when evaluation completes."]
67    #[doc = ""]
68    #[doc = "Parameter result: The result of evaluation."]
69    #[doc = ""]
70    #[doc = "Parameter exceptionInfo: An object providing details if an exception occurred while evaluating the expression."]
71    pub fn eval_with_expression_and_options_and_callback(
72        this: &InspectedWindow,
73        options: JsValue,
74        callback: js_sys::Function,
75    );
76
77    #[wasm_bindgen(method, js_name = getResources)]
78    #[doc = "Retrieves the list of resources from the inspected page."]
79    #[doc = ""]
80    #[doc = "@param callback A function that receives the list of resources when the request completes."]
81    pub fn get_resources(this: &InspectedWindow, callback: js_sys::Function);
82
83    #[wasm_bindgen(method, setter, js_name = onResourceAdded)]
84    #[doc = "Fired when a new resource is added to the inspected page."]
85    pub fn on_resource_added(this: &InspectedWindow, value: js_sys::Function);
86
87    #[wasm_bindgen(method, setter, js_name = onResourceContentCommitted)]
88    #[doc = "Fired when a new revision of the resource is committed (e.g. user saves an edited version of the resource in the Developer Tools)."]
89    pub fn on_resource_content_committed(this: &InspectedWindow, value: js_sys::Function);
90}
91
92#[derive(Serialize, Deserialize)]
93struct ReloadOptions<'a> {
94    #[serde(alias = "userAgent")]
95    /// Optional. If specified, the string will override the value of the User-Agent HTTP header that's sent while loading the resources of the inspected page. The string will also override the value of the navigator.userAgent property that's returned to any scripts that are running within the inspected page.
96    user_agent: Option<&'a str>,
97    #[serde(alias = "ignoreCache")]
98    /// Optional. When true, the loader will ignore the cache for all inspected page resources loaded before the load event is fired. The effect is similar to pressing Ctrl+Shift+R in the inspected window or within the Developer Tools window.
99    ignore_cache: Option<bool>,
100    #[serde(alias = "injectedScript")]
101    /// Optional. If specified, the script will be injected into every frame of the inspected page immediately upon load, before any of the frame's scripts. The script will not be injected after subsequent reloads—for example, if the user presses Ctrl+R.
102    injected_script: Option<&'a str>,
103    #[serde(alias = "preprocessorScript")]
104    /**
105     * Optional.
106     * If specified, this script evaluates into a function that accepts three string arguments: the source to preprocess, the URL of the source, and a function name if the source is an DOM event handler. The preprocessorerScript function should return a string to be compiled by Chrome in place of the input source. In the case that the source is a DOM event handler, the returned source must compile to a single JS function.
107     * @deprecated Deprecated since Chrome 41. Please avoid using this parameter, it will be removed soon.
108     */
109    preprocessor_script: Option<&'a str>,
110}
111
112impl From<ReloadOptions<'_>> for JsValue {
113    fn from(value: ReloadOptions) -> Self {
114        serde_wasm_bindgen::to_value(&value).unwrap()
115    }
116}
117
118#[derive(Serialize, Deserialize)]
119struct EvalOptions<'a> {
120    #[serde(alias = "frameURL")]
121    /// If specified, the expression is evaluated on the iframe whose URL matches the one specified. By default, the expression is evaluated in the top frame of the inspected page.
122    frame_url: Option<&'a str>,
123    #[serde(alias = "useContentScriptContext")]
124    /// Evaluate the expression in the context of the content script of the calling extension, provided that the content script is already injected into the inspected page. If not, the expression is not evaluated and the callback is invoked with the exception parameter set to an object that has the isError field set to true and the code field set to E_NOTFOUND.
125    use_content_script_context: Option<bool>,
126    #[serde(alias = "contextSecurityOrigin")]
127    /// Evaluate the expression in the context of a content script of an extension that matches the specified origin. If given, contextSecurityOrigin overrides the 'true' setting on userContentScriptContext.
128    context_security_origin: Option<&'a str>,
129}
130
131impl From<EvalOptions<'_>> for JsValue {
132    fn from(value: EvalOptions) -> Self {
133        serde_wasm_bindgen::to_value(&value).unwrap()
134    }
135}