Skip to content

Commit 7f3d0bf

Browse files
committed
doc: move the extra example + elaborate
1 parent 90b5dcf commit 7f3d0bf

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

content/api/gm.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,7 @@ const id2 = GM_registerMenuCommand('Text2', onClick, { title: 'Two' })
350350
const id3 = GM_registerMenuCommand('Text3', onClick, { autoClose: false })
351351
```
352352
353-
v2.15.9 and newer returns a randomly generated id or the `id` specified in the third parameter (previously v2.12.5...2.15.8 returned an `id` equal to `caption`), which allows changing the command in-place without recreating it:
354-
```js
355-
const id = 'status';
356-
const inplace = id === GM_registerMenuCommand('Enabled', onClick, { id });
357-
// .......later:
358-
if (inplace) { // change the command in-place if supported
359-
GM_registerMenuCommand('Disabled', onClick, { id, title: 'Status' });
360-
} else { // ...or re-create it otherwise
361-
GM_unregisterMenuCommand('Enabled');
362-
GM_registerMenuCommand('Disabled', onClick);
363-
}
364-
```
353+
v2.15.9 and newer returns a randomly generated id or the `id` specified in the third parameter (previously v2.12.5...2.15.8 returned an `id` equal to `caption`), which allows changing the command in-place.
365354
366355
- <Field name="caption" type="string" />
367356
@@ -386,6 +375,24 @@ if (inplace) { // change the command in-place if supported
386375
387376
If you want to add a shortcut, please see [vm.shortcut](https://github.com/violentmonkey/vm-shortcut).
388377
378+
Here's how you can change the command in-place, thus preserving its relative position in the list of multiple commands:
379+
```js
380+
const id = 'status';
381+
const inplace = id === GM_registerMenuCommand('Enabled', onClick, { id });
382+
if (inplace) {
383+
// supported: change the command in-place using the same `id`
384+
GM_registerMenuCommand('Disabled', onClick, { id, title: 'Status' });
385+
} else {
386+
// not supported: recreate the commands
387+
GM_unregisterMenuCommand('Enabled');
388+
GM_unregisterMenuCommand('Foo');
389+
GM_unregisterMenuCommand('Bar');
390+
GM_registerMenuCommand('Disabled', onClick);
391+
GM_registerMenuCommand('Foo', onClick2);
392+
GM_registerMenuCommand('Bar', onClick3);
393+
}
394+
```
395+
389396
### GM_unregisterMenuCommand
390397

391398
Unregisters a command which has been registered to Violentmonkey popup menu.

0 commit comments

Comments
 (0)