Skip to content

Commit 07f10be

Browse files
committed
doc: document-body + info for other modes, fix #58
1 parent 587d656 commit 07f10be

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

content/api/metadata-block.md

+22-11
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,30 @@ Decide when the script will execute.
137137

138138
Several values can be set for `@run-at`:
139139

140-
- `document-end` <Label name="default" />
140+
- `document-start`
141141

142-
The script executes when `DOMContentLoaded` is fired. At this time, the basic HTML of the page is ready and other resources like images might still be on the way.
142+
The userscript executes as early as possible, `document.documentElement` is present, but may be without either `document.head` or `document.body` or both. Other scripts in the page may run earlier, see the note below.
143143

144-
- `document-start`
144+
- `document-body` *(since v2.12.10)*
145145

146-
The script executes as soon as possible. There is no guarantee for the script to execute before other scripts in the page.
146+
The userscript executes after `document.body` appears, possibly with some child elements inside, because detection is asynchronous (using a one-time MutationObserver).
147+
148+
- `document-end` <Label name="default" />
147149

148-
Note: in Greasemonkey v3, the script may be ensured to execute even before HTML is loaded, but this is impossible for Violentmonkey as a web extension.
150+
The userscript executes when `DOMContentLoaded` is fired synchronously. At this time, the basic HTML of the page is ready and other resources like images might still be on the way.
149151

150152
- `document-idle`
151153

152-
The script executes after `DOMContentLoaded` is fired.
154+
The userscript executes after `DOMContentLoaded` is fired asynchronously, i.e. after yielding to the previously scheduled callbacks or urgent tasks like rendering. Prefer this mode for scripts that take more than a couple of milliseconds to compile and run (you can see it in devtools performance profiler), so that they don't delay the moment the page becomes usable.
155+
156+
When using `document-start` in Violentmonkey ManifestV2 there's a limited method of ensuring the userscript runs before other scripts in the page:
157+
158+
* the userscript must use the default [`page` injection mode](#inject-into)
159+
* the user must enable `Synchronous page mode` (Chrome/Firefox) or `Alternative page mode` (Firefox only, enabled by default) in Violentmonkey's advanced settings;
160+
* the user didn't change the injection mode to `content` in script's editor or script's settings or in Violentmonkey's settings;
161+
* the cookies are not explicitly blocked for the site;
162+
* it's not the incognito mode;
163+
* Firefox-only: the site doesn't block script elements via its CSP;
153164

154165
### @noframes
155166

@@ -174,16 +185,16 @@ If no `@grant` is present, `@grant none` is assumed.
174185
```
175186

176187
Sandbox is disabled in this mode, meaning the script can add/modify globals directly without the need to use `unsafeWindow`.
177-
188+
178189
* In case any special API is used, it must be explicitly granted
179190

180191
```js
181192
// @grant GM_getValue
182193
// @grant GM_setValue
183194
```
184-
195+
185196
…or for the new `GM.*` API methods *(Since VM2.12.10)*:
186-
197+
187198
```js
188199
// @grant GM.getValue
189200
// @grant GM.setValue
@@ -193,12 +204,12 @@ In addition to [GM API](../gm/) the following privileges may be granted:
193204

194205
* `// @grant window.close`
195206

196-
*Since VM2.6.2*
207+
*Since VM2.6.2*<br/>
197208
Allows closing the tab via `window.close()`
198209

199210
* `// @grant window.focus`
200211

201-
*Since VM2.12.10*
212+
*Since VM2.12.10*<br/>
202213
Allows focusing the tab via `window.focus()` even if the user didn't interact with it first.
203214
204215
### @inject-into

0 commit comments

Comments
 (0)