Skip to content

Commit 5941dd8

Browse files
Clarify runAtComplete docs
1 parent 518d892 commit 5941dd8

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

content/docs/develop/userscripts/about-userscripts.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,22 @@ document.querySelector(".sa-find-bar").placeholder = msg("find");
125125
126126
{{< admonition info >}}
127127
For more information about localizing userscripts, see [this page](/docs/localization/localizing-addons/).
128-
{{< /admonition >}}
128+
{{</admonition >}}
129129
130130
131131
## Technical details
132132
133133
Each userscript file is a JavaScript module that exports a function. Scratch Addons only imports the module if needed, and executes it after the page has fully loaded.
134134
135-
<!-- TODO: explain runAtComplete -->
136-
137135
Userscripts are JavaScript modules, so they always run on ["strict mode"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode). This also means that userscripts may use [top-level imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) to import other JavaScript files.
138136
137+
The order in which userscripts run may vary on each page load. After page load, the user might dynamically enable some addons in a custom order, so order of execution is never guaranteed. Some APIs like [`addon.tab.appendToSharedSpace`](addon.tab.appendtosharedspace) attempt to fix any potential race conditions and unexpected behavior when dynamically enabling addons.
138+
139+
### runAtComplete
140+
141+
Userscripts may opt-in into being executed before the page has fully loaded by specifying `"runAtComplete": false` in the addon manifest, once for each userscript.
142+
143+
As of now, only `document.head` is guaranteed to exist when running a userscript early. In the future, `document.body` will also be guaranteed to exist, so no userscripts will ever run before the HTML document loaded enough to reach `</head> <body>`.
144+
145+
139146
<!-- TODO: explain execution order isn't guaranteed -->

content/docs/reference/addon-manifest.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ Matches that allow the userscript/userstyle to run on. Values can be a URL match
263263
| Available in userscripts | ✔️ |
264264
| Available in userstyles ||
265265

266-
Specifies whether the userscript should run after the page has loaded (after the window load event). If unspecified, `true` is assumed.
267-
If set to `false`, the userscript is only guaranteed to run after the \<head> element of the document has loaded.
266+
Specifies whether the userscript should run after the page has loaded. If unspecified, `true` is assumed.
267+
268+
See [about userscripts](/docs/develop/userscripts/about-userscripts/#runatcomplete) for more information.
268269

269270
### `if`
270271

0 commit comments

Comments
 (0)