Executes JavaScript in the window that the devtools are attached to.
This is somewhat like using 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: for example, using "$0" to refer to the element currently 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. 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()
.
The script is evaluated by default in the main frame of the page. The script must evaluate to a value that can be represented as JSON (meaning that, for example, it may not evaluate to a function or an object that contains any functions). By default, the script does not see any content scripts attached to the page.
You can't call eval()
on privileged browser windows such as "about:addons".
You can optionally provide an options
parameter, which includes options to evaluate the script in a different frame or in the context of attached content scripts. Note that Firefox does not yet support the options
parameter.
The eval()
function returns a Promise
that resolves to the evaluated result of the script or to an error.