You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Bug-2011234: remove the ability to dynamically execute code in moz-extension: documents (mdn#43108)
* Bug-2011234: remove the ability to dynamically execute code in `moz-extension:` documents
* Apply suggestions from linter
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
* Apply suggestions from review
Co-authored-by: Rob Wu <rob@robwu.nl>
* Apply suggestions from review
Co-authored-by: Rob Wu <rob@robwu.nl>
* Apply suggestion from @Rob--W
Co-authored-by: Rob Wu <rob@robwu.nl>
* Apply suggestion from @Rob--W
Co-authored-by: Rob Wu <rob@robwu.nl>
* Further clarifications of dynamic code execution in extension documents
* Apply suggestions from review
Co-authored-by: Rob Wu <rob@robwu.nl>
* Further feedback related changes
* typo
* Further feedback changes
* Apply suggestions from @Rob--W review
Co-authored-by: Rob Wu <rob@robwu.nl>
* Minor corrections
* Reapply deletion
---------
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Rob Wu <rob@robwu.nl>
Copy file name to clipboardExpand all lines: files/en-us/mozilla/add-ons/webextensions/api/runtime/onmessage/index.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ Some example use cases are:
14
14
-**in a background script** to listen for messages from a content script.
15
15
-**in an [options page or popup](/en-US/docs/Mozilla/Add-ons/WebExtensions/Anatomy_of_a_WebExtension#sidebars_popups_and_options_pages) script** to listen for messages from a background script.
16
16
-**in a background script** to listen for messages from an options page or popup script.
17
+
-**in a script in an [extension page](/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Extension_pages)** to listen for messages requesting the execution of code in the page's scripts.
17
18
18
19
To send a message that is received by the `onMessage()` listener, use {{WebExtAPIRef("runtime.sendMessage()")}} or (to send a message to a content script) {{WebExtAPIRef("tabs.sendMessage()")}}.
Copy file name to clipboardExpand all lines: files/en-us/mozilla/add-ons/webextensions/api/scripting/executescript/index.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,13 +11,13 @@ Injects a script into a target context. The script is run at `document_idle` by
11
11
> [!NOTE]
12
12
> This method is available in Manifest V3 or higher in Chrome and Firefox 101. In Safari and Firefox 102+, this method is also available in Manifest V2.
13
13
14
-
To use this API you must have the `"scripting"`[permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions) and permission for the target's URL, either explicitly as a [host permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#host_permissions) or using the [activeTab permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#activetab_permission). Note that some special pages do not allow this permission, including reader view, view-source, and PDF viewer pages.
14
+
To use this API you must have the `"scripting"`[permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions) and permission for the target's URL, either explicitly as a [host permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#host_permissions) or using the [activeTab permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#activetab_permission). Note that some special pages do not allow this permission, including reader view, view-source, PDF viewer, and other built-in browser UI pages.
15
15
16
16
In Firefox and Safari, partial lack of host permissions can result in a successful execution (with the partial results in the resolved promise). In Chrome, any missing permission prevents any execution from happening (see [Issue 1325114](https://crbug.com/1325114)).
17
17
18
18
The scripts you inject are called [content scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts).
19
19
20
-
This is an asynchronous function that returns a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
20
+
Extensions cannot run content scripts in [extension pages](/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Extension_pages). If an extension wants to run code in an extension page dynamically, it can include a script in the document. This script contains the code to run and registers a {{WebExtAPIRef("runtime.onMessage")}} listener that implements a way to execute the code. The extension can then send a message to the listener to trigger the code's execution.
Copy file name to clipboardExpand all lines: files/en-us/mozilla/add-ons/webextensions/api/tabs/executescript/index.md
+17-28Lines changed: 17 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,28 +11,17 @@ Injects JavaScript code into a page.
11
11
> [!NOTE]
12
12
> When using Manifest V3 or higher, use {{WebExtAPIRef("scripting.executeScript()")}} to execute scripts.
13
13
14
-
You can inject code into pages whose URL can be expressed using a [match pattern](/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns). To do so, its scheme must be one of: `http`, `https`, or `file`.
14
+
You can inject code into pages whose URL you can express using a [match pattern](/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns). To do so, its scheme must be one of: `http`, `https`, or `file`.
15
15
16
-
You must have the permission for the page's URL—either explicitly, as a [host permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#host_permissions)—or, via the [activeTab permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#activetab_permission). Note that some special pages do not allow this permission, including reader view, view-source, and PDF viewer pages.
16
+
You must have the permission for the page's URLeither explicitly, as a [host permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#host_permissions), or using the [activeTab permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#activetab_permission). Note that some special pages do not allow this permission, including reader view, view-source, PDF viewer, and other built-in browser UI pages.
17
17
18
-
You can also inject code into pages packaged with your own extension:
18
+
Extensions cannot run content scripts in [extension pages](/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Extension_pages). If an extension wants to run code in an extension page dynamically, it can include a script in the document. This script contains the code to run and registers a {{WebExtAPIRef("runtime.onMessage")}} listener that implements a way to execute the code. The extension can then send a message to the listener to trigger the code's execution.
You don't need any special permissions to do this.
29
-
30
-
You _cannot_ inject code into any of the browser's built-in pages, such as: `about:debugging`, `about:addons`, or the page that opens when you open a new empty tab.
20
+
> [!NOTE]
21
+
> The ability to inject code into pages packaged with your extension was deprecated in Firefox 149 and removed in Firefox 152.
31
22
32
23
The scripts you inject are called [content scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts).
33
24
34
-
This is an asynchronous function that returns a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
35
-
36
25
## Syntax
37
26
38
27
```js-nolint
@@ -54,11 +43,11 @@ let executing = browser.tabs.executeScript(
54
43
55
44
It contains the following properties:
56
45
-`allFrames` {{optional_inline}}
57
-
- : `boolean`. If `true`, the code will be injected into all frames of the current page.
46
+
- : `boolean`. If `true`, the code is injected into all frames of the current page.
58
47
59
-
If `true` and `frameId` is set, then it will raise an error. (`frameId` and `allFrames` are mutually exclusive.)
48
+
If set to `true` and `frameId` is set, it raises an error. (`frameId` and `allFrames` are mutually exclusive.)
60
49
61
-
If it is `false`, code is only injected into the top frame.
50
+
If it is `false`, the code is injected only into the top frame.
62
51
63
52
Defaults to `false`.
64
53
@@ -81,38 +70,38 @@ let executing = browser.tabs.executeScript(
81
70
Defaults to `0` (the top-level frame).
82
71
83
72
-`matchAboutBlank` {{optional_inline}}
84
-
- : `boolean`. If `true`, the code will be injected into embedded `about:blank` and `about:srcdoc` frames if your extension has access to their parent document. The code cannot be inserted in top-level `about:` frames.
73
+
- : `boolean`. If `true`, the code is injected into embedded `about:blank` and `about:srcdoc` frames if your extension has access to their parent document. The code cannot be inserted in top-level `about:` frames.
85
74
86
75
Defaults to `false`.
87
76
88
77
-`runAt` {{optional_inline}}
89
-
- : {{WebExtAPIRef('extensionTypes.RunAt')}}. The soonest that the code will be injected into the tab.
78
+
- : {{WebExtAPIRef('extensionTypes.RunAt')}}. The earliest that the code is injected into the tab.
90
79
91
80
Defaults to `"document_idle"`.
92
81
93
82
### Return value
94
83
95
-
A [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that will resolve to an array of objects. The array's values represent the result of the scriptin every injected frame.
84
+
A [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that resolves to an array of objects. The array's values represent the script's result in each injected frame.
96
85
97
-
The result of the script is the last evaluated statement, which is similar to what would be output (the results, not any `console.log()` output) if you executed the script in the [Web Console](https://firefox-source-docs.mozilla.org/devtools-user/web_console/index.html). For example, consider a script like this:
86
+
The result of the script is the last evaluated statement, which is similar to the output (the results, not any `console.log()` output) if you executed the script in the [Web Console](https://firefox-source-docs.mozilla.org/devtools-user/web_console/index.html). For example, consider a script like this:
98
87
99
88
```js
100
89
let foo ="my result";
101
90
foo;
102
91
```
103
92
104
-
Here the results array will contain the string `"my result"` as an element.
93
+
Here, the results array contains the string `"my result"` as an element.
105
94
106
95
The result values must be [structured cloneable](/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) (see [Data cloning algorithm](/en-US/docs/Mozilla/Add-ons/WebExtensions/Chrome_incompatibilities#data_cloning_algorithm)).
107
96
108
97
> [!NOTE]
109
-
> The last statement may be also a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), but this feature is unsupported by [webextension-polyfill](https://github.com/mozilla/webextension-polyfill#tabsexecutescript) library.
98
+
> The last statement can also a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), but this feature is unsupported by [webextension-polyfill](https://github.com/mozilla/webextension-polyfill#tabsexecutescript) library.
110
99
111
-
If any error occurs, the promise will be rejected with an error message.
100
+
If any error occurs, the promise is rejected with an error message.
112
101
113
102
## Examples
114
103
115
-
This example executes a one-line code snippet in the currently active tab:
104
+
This example executes a one-line code snippet in the active tab:
This example executes a script from a file (packaged with the extension) called `"content-script.js"`. The script is executed in the currently active tab. The script is executed in subframes as well as the main document:
123
+
This example executes a script from a file (packaged with the extension) called `"content-script.js"`. The script is executed in the active tab. The script is executed in subframes as well as the main document:
Copy file name to clipboardExpand all lines: files/en-us/mozilla/add-ons/webextensions/content_scripts/index.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,10 +25,6 @@ You can load a content script into a web page:
25
25
26
26
There is only one global scope _per frame, per extension_. This means that variables from a content script can be accessed by any other content scripts, regardless of how the content script was loaded.
27
27
28
-
Using methods (1) and (2), you can only load scripts into pages whose URLs can be represented using a [match pattern](/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns).
29
-
30
-
Using method (3), you can also load scripts into pages packaged with your extension, but you can't load scripts into privileged browser pages (like `about:debugging` or `about:addons`).
31
-
32
28
> [!NOTE]
33
29
> [Dynamic JS module imports](/en-US/docs/Web/JavaScript/Guide/Modules#dynamic_module_loading) are now working in content scripts. For more details, see [Firefox bug 1536094](https://bugzil.la/1536094).
34
30
> Only URLs with the _moz-extension_ scheme are allowed, which excludes data URLs ([Firefox bug 1587336](https://bugzil.la/1587336)).
@@ -79,7 +75,11 @@ The set of domains can be restricted further through enterprise policies: Firefo
79
75
80
76
### Limitations
81
77
82
-
Whole tabs or frames may be loaded using [`data:` URI](/en-US/docs/Web/URI/Reference/Schemes/data), {{DOMxRef("URL.createObjectURL_static", "Blob")}} objects, and other similar techniques. Support of content scripts injection into such special documents varies across browsers, see the Firefox [bug #1411641 comment 41](https://bugzil.la/1411641#c41) for some details.
78
+
By default, content scripts do not run in `about:blank`, `about:srcdoc`, `data:`, and `blob:` pages. To enable their execution, use the [`match_origin_as_fallback`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts#match_origin_as_fallback) option in the `content_scripts` manifest key or the [`matchOriginAsFallback`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/RegisteredContentScript#matchoriginasfallback) option in the `scripting` API.
79
+
80
+
Extensions cannot inject content scripts into privileged browser UI pages (such as `about:debugging`, `about:addons`, reader view, view-source, or the PDF viewer) or [extension pages](/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Extension_pages).
81
+
82
+
If an extension wants to run code in an extension page dynamically, it can include a script in the page. This script contains the code to run and registers a {{WebExtAPIRef("runtime.onMessage")}} listener that implements a way to execute the code. The extension can then send a message to the listener to trigger the code's execution.
where `60a20a9b-1ad4-af49-9b6c-c64c98c37920` is the extension's internal ID. The extension can get this URL programmatically by calling {{webextAPIref("extension/getURL", "extension.getURL()")}}:
0 commit comments