Skip to content
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

Complete devtools.inspectedWindow #28131

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Executes JavaScript in the window that the devtools are attached to.

This is somewhat like using {{WebExtAPIRef("tabs.executeScript()")}} to attach a content script, but with two main differences:

First, the JavaScript can use a set of [special commands that browsers typically provide in their devtools console implementation](#helpers): for example, using "$0" to refer to the element currently selected in the Inspector.
First, the JavaScript can use a set of [special commands that browsers typically provide in their devtools console implementation](#helpers): for example, using "$0" to refer to the element selected in the Inspector.

Second, the JavaScript you execute can see any changes made to the page by scripts that the page loaded. This is in contrast to content scripts, which see the page [as it would exist if no page scripts were loaded](/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#dom_access). However, note that the isolation provided by content scripts is a deliberate security feature, intended to make it harder for malicious or uncooperative web pages to confuse or subvert WebExtensions APIs by redefining DOM functions and properties. This means you need to be very careful if you waive this protection by using `eval()`, and should use content scripts unless you need to use `eval()`.

Expand Down Expand Up @@ -49,14 +49,16 @@ let evaluating = browser.devtools.inspectedWindow.eval(
- : `string`. The JavaScript expression to evaluate. The string must evaluate to an object that can be represented as JSON, or an exception will be thrown. For example, `expression` must not evaluate to a function.
- `options` {{optional_inline}}

- : `object`. Options for the function (Note that Firefox does not yet support this options), as follows:
- : `object`. Options for the function, as follows:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like these properties are still labeled as unsupported in Firefox: https://searchfox.org/mozilla-central/source/browser/components/extensions/schemas/devtools_inspected_window.json#107-123. I also tested in Firefox Nightly 129.0a1 (2024-06-28) (64-bit) and Firefox Stable 127.0 (64-bit) and observed that they threw an "unsupported" error.

Is the intent of this change to move notes about what is (not) supported to the compat tables?


- `contextSecurityOrigin` {{optional_inline}}
- : `string`. Evaluate the expression in the context of a content script attached by a different extension, whose origin matches the value given here. This overrides `useContentScriptContext`.
- `frameURL` {{optional_inline}}
- : `string`. The URL of the frame in which to evaluate the expression. If this is omitted, the expression is evaluated in the main frame of the window.
- `scriptExecutionContext` {{optional_inline}}
- : `string`. Evaluate the expression in the context of a content script of an extension that matches the specified origin. If given, `scriptExecutionContext` overrides the `true` setting on `useContentScriptContext`.
- `useContentScriptContext` {{optional_inline}}
- : `boolean`. If `true`, evaluate the expression in the context of any content scripts that this extension has attached to the page. If you set this option, then you must have actually attached some content scripts to the page, or a Devtools error will be thrown.
- `contextSecurityOrigin` {{optional_inline}}
- : `string`. Evaluate the expression in the context of a content script attached by a different extension, whose origin matches the value given here. This overrides `useContentScriptContext`.

### Return value

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: devtools.inspectedWindow.getResources()
slug: Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/getResources
page-type: webextension-api-function
browser-compat: webextensions.api.devtools.inspectedWindow.getResources
---

{{AddonSidebar()}}

Retrieves the list of resources from the inspected page.

## Syntax

```js-nolint
let gettingResources = browser.devtools.inspectedWindow.getResources()
```

### Parameters

This function takes no parameters.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Chrome's documentation, this function takes a callback parameter and does not return a promise. https://developer.chrome.com/docs/extensions/reference/api/devtools/inspectedWindow#method-getResources

I tested and verified this behavior in Chrome 126.0.6478.127 (Official Build) (x86_64).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On MDN we are using the conventional of omitting the callback in favor of specifying a Promise value. But because we don't support this in Firefox and Chrome doesn't support Promises in their devtools API either, I wouldn't mind being explicit with the callback here.

Probably makes sense to explicitly document that Chrome doesn't support Promise in devtools namespace, which is a deviation from all other MV3 APIs that now support Promises: https://issues.chromium.org/issues/41483013

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rob--W, yesterday I didn't realize that Safari supports this method and returns a Promise (when called without a callback argument). Since Chrome still doesn't support Promises on DevTools APIs, would it make more sense to explicitly document the callback, to stick with the current convention, or something else?

Copy link
Member

@Rob--W Rob--W Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick to the Promise convention.

Note that the APIs are documented with the browser namespace which Chrome doesn't support yet. So the webextension-polyfill might work here.


### Return value

A [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) fulfilled with an array of {{WebExtAPIRef("devtools.inspectedWindow.Resource")}}. If the request fails, the promise is rejected with an error message.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chrome returns undefined.


{{WebExtExamples("h2")}}

## Browser compatibility

{{Compat}}

<!--
// Copyright 2015 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ The `devtools.inspectedWindow` API lets a devtools extension interact with the w

Like all the `devtools` APIs, this API is only available to code running in the document defined in the [devtools_page](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/devtools_page) manifest.json key, or in other devtools documents created by the extension (such as the document hosted by a panel the extension created). See [Extending the developer tools](/en-US/docs/Mozilla/Add-ons/WebExtensions/Extending_the_developer_tools) for more.

## Types

- [`devtools.inspectedWindow.Resource`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/Resource)
- : A resource within an inspected page, such as a document, script, or image.

## Properties

- [`devtools.inspectedWindow.tabId`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/tabId)
Expand All @@ -23,9 +28,18 @@ Like all the `devtools` APIs, this API is only available to code running in the

- [`devtools.inspectedWindow.eval()`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/eval)
- : Evaluate some JavaScript in the target window.
- [`devtools.inspectedWindow.getResources()`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/getResources)
- : Retrieves the list of resources from the inspected page.
- [`devtools.inspectedWindow.reload()`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/reload)
- : Reload the target window's document.

## Events

- [`devtools.onResourceAdded`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/onResourceAdded)
- : Fired when a resource is added to the inspected page.
- [`devtools.onResourceContentCommitted`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/onResourceContentCommitted)
- : Fired when a revision of the resource is committed (e.g. the user saves an edited version of the resource in the Developer Tools).

{{WebExtExamples("h2")}}

## Browser compatibility
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: devtools.inspectedWindow.onResourceAdded
slug: Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/onResourceAdded
page-type: webextension-api-event
browser-compat: webextensions.api.devtools.inspectedWindow.onResourceAdded
---

{{AddonSidebar()}}

Fired when a resource is added to the inspected page.

## Syntax

```js-nolint
browser.devtools.inspectedWindow.onResourceAdded.addListener(listener)
browser.devtools.inspectedWindow.onResourceAdded.removeListener(listener)
browser.devtools.inspectedWindow.onResourceAdded.hasListener(listener)
```

Events have three functions:

- `addListener(listener)`
- : Adds a listener to this event.
- `removeListener(listener)`
- : Stop listening to this event. The `listener` argument is the listener to remove.
- `hasListener(listener)`
- : Check whether `listener` is registered for this event. Returns `true` if it is listening, `false` otherwise.

## addListener syntax

### Parameters

- `listener`

- : The function called when this event occurs. The function is passed this argument:

- `resource`
- : {{WebExtAPIRef('devtools.inspectedWindow.Resource')}}. The resource that was added.

{{WebExtExamples("h2")}}

## Browser compatibility

{{Compat}}

<!--
// Copyright 2015 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
title: devtools.inspectedWindow.onResourceContentCommitted
slug: Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/onResourceContentCommitted
page-type: webextension-api-event
browser-compat: webextensions.api.devtools.inspectedWindow.onResourceContentCommitted
---

{{AddonSidebar()}}

Fired when a revision of the resource is committed. For example, user saves an edited version of the resource in the Developer Tools.

## Syntax

```js-nolint
browser.devtools.inspectedWindow.onResourceContentCommitted.addListener(listener)
browser.devtools.inspectedWindow.onResourceContentCommitted.removeListener(listener)
browser.devtools.inspectedWindow.onResourceContentCommitted.hasListener(listener)
```

Events have three functions:

- `addListener(listener)`
- : Adds a listener to this event.
- `removeListener(listener)`
- : Stop listening to this event. The `listener` argument is the listener to remove.
- `hasListener(listener)`
- : Check whether `listener` is registered for this event. Returns `true` if it is listening, `false` otherwise.

## addListener syntax

### Parameters

- `listener`

- : The function called when this event occurs. The function is passed this argument:

- `content`
- : `string` The content of the resource.
- `resource`
- : {{WebExtAPIRef('devtools.inspectedWindow.Resource')}}. The resource that was committed.

{{WebExtExamples("h2")}}

## Browser compatibility

{{Compat}}

<!--
// Copyright 2015 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: devtools.inspectedWindow.Resource
slug: Mozilla/Add-ons/WebExtensions/API/devtools/inspectedWindow/Resource
page-type: webextension-api-type
browser-compat: webextensions.api.devtools.inspectedWindow.Resource
---

{{AddonSidebar()}}

A resource within the inspected page, such as a document, a script, or an image. This object is returned from {{WebExtAPIRef('devtools.inspectedWindow.getResources()')}}, {{WebExtAPIRef('devtools.inspectedWindow.onResourceAdded')}}, and {{WebExtAPIRef('devtools.inspectedWindow.onResourceContentCommitted')}}.

## Type

Values of this type are objects. They contain these properties:

- `url`
- : `string`. The URL of the resource.
- `getContent`
- : `function`. Gets the content of the resource. This function takes no parameters, and returns a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) fulfilled with an object containing:
- `content`
- : `string`. Content of the resource, which could be encoded.
- `encoding`
- : `string`. Empty if the content is not encoded, otherwise the encoding name.
- `setContent`
- : `function`. Sets the content of the resource. This function takes these parameters:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
- : `function`. Sets the content of the resource. This function takes these parameters:
- : `function`. Sets the content of the resource. This function takes these parameters:

- `content`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
- `content`
- `content`

- : `string`. The new content of the resource. Only resources with the text type are supported.
- `commit`
- : `boolean`. True if the user has finished editing the resource, and the new content of the resource should be persisted; false if this is a minor change sent in progress of the user editing the resource.

Returns a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) fulfilled with an error object set to undefined if the resource content was set successfully; otherwise it describes the error.

{{WebExtExamples("h2")}}

## Browser compatibility

{{Compat}}
Loading