Skip to content

Commit 587d656

Browse files
committed
doc: async GM.xmlHttpRequest + GM.download
1 parent f277d7d commit 587d656

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

content/api/gm.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,12 +699,13 @@ Returns a control object with the following properties, same as [GM_xmlhttpReque
699699

700700
## GM.*
701701

702-
`GM` *(since VM2.12.0)* is a single variable with [Greasemonkey4-compatible](https://wiki.greasespot.net/Greasemonkey_Manual:API) aliases:
702+
`GM` *(since VM2.12.0)* is a single variable with [Greasemonkey4-compatible](https://wiki.greasespot.net/Greasemonkey_Manual:API) aliases, the `async` functions return a `Promise` that's resolved with the returned value.
703703

704704
* [GM.addStyle](#gm_addstyle)
705705
* [GM.addElement](#gm_addelement) - *since VM2.13.1*
706706
* [GM.registerMenuCommand](#gm_registermenucommand) - *since VM2.12.10, GM4.11*
707707
* [GM.deleteValue](#gm_deletevalue) *(async)*
708+
* [GM.download](#gm_download) *(async since VM2.18.3)*
708709
* [GM.getResourceUrl](#gm_getresourceurl) *(async)* - in VM2.12.0...2.13.0 it was misspelled as `GM.getResourceURL`
709710
* [GM.getValue](#gm_getvalue) *(async)*
710711
* [GM.info](#gm_info)
@@ -713,6 +714,25 @@ Returns a control object with the following properties, same as [GM_xmlhttpReque
713714
* [GM.openInTab](#gm_openintab)
714715
* [GM.setClipboard](#gm_setclipboard)
715716
* [GM.setValue](#gm_setvalue) *(async)*
716-
* [GM.xmlHttpRequest](#gm_xmlhttprequest) - note `H` is uppercase
717-
718-
The `async` functions return a `Promise` that's resolved with the returned value.
717+
* [GM.xmlHttpRequest](#gm_xmlhttprequest) *(async since VM2.18.3)*, `H` is uppercase
718+
719+
```js
720+
// compatible with multiple userscript managers
721+
GM.xmlHttpRequest({ url, onload: res => {/*....*/} });
722+
```
723+
```js
724+
// compatible with multiple userscript managers
725+
const res = await new Promise((resolve, reject) => {
726+
GM.xmlHttpRequest({ url, onload: resolve, onerror: reject });
727+
});
728+
```
729+
```js
730+
// VM2.18.3+, TM
731+
const res = await GM.xmlHttpRequest({ url });
732+
```
733+
```js
734+
// VM2.18.3+, TM
735+
const control = GM.xmlHttpRequest({ url });
736+
myButton.addEventListener('click', control.abort, { once: true });
737+
const res = await control;
738+
```

0 commit comments

Comments
 (0)