vite-userscript-plugin 1.10.0 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,14 @@
1
- // Type definitions for non-npm package Tampermonkey 4.x
2
- // Project: https://tampermonkey.net
3
- // Definitions by: Steven Wang <https://github.com/silverwzw>
4
- // Nikolay Borzov <https://github.com/nikolay-borzov>
5
- // taozhiyu <https://github.com/taozhiyu>
6
- // double-beep <https://github.com/double-beep>
7
- // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
-
9
1
  // This definition is based on the API reference of Tampermonkey
10
2
  // https://tampermonkey.net/documentation.php
11
3
  // TypeScript Version: 3.3
12
4
 
13
5
  declare namespace Tampermonkey {
6
+ /**
7
+ * @param key The key whose value has changed.
8
+ * @param oldValue The previous value of the key.
9
+ * @param newValue The new value of the key
10
+ * @param remote A boolean indicating whether the change originated from a different userscript instance.
11
+ */
14
12
  type ValueChangeListener = (
15
13
  name: string,
16
14
  oldValue: any,
@@ -30,9 +28,13 @@ declare namespace Tampermonkey {
30
28
 
31
29
  interface ResponseBase {
32
30
  readonly responseHeaders: string
31
+ /** The request's `readyState`. */
33
32
  readonly readyState: ReadyState
33
+ /** The response data as object if `details.responseType` was set. */
34
34
  readonly response: any
35
+ /** The response data as plain string. */
35
36
  readonly responseText: string
37
+ /** The response data as an XML document. */
36
38
  readonly responseXML: Document | null
37
39
  readonly status: number
38
40
  readonly statusText: string
@@ -52,6 +54,7 @@ declare namespace Tampermonkey {
52
54
  }
53
55
 
54
56
  interface Response<TContext> extends ResponseBase {
57
+ /** The final URL after all redirects from where the data was loaded. */
55
58
  readonly finalUrl: string
56
59
  readonly context: TContext
57
60
  }
@@ -72,59 +75,67 @@ declare namespace Tampermonkey {
72
75
  ) => void
73
76
 
74
77
  interface Request<TContext = object> {
75
- method?: 'GET' | 'HEAD' | 'POST' | undefined
76
- /** Destination URL */
77
- url: string
78
+ method?: 'GET' | 'HEAD' | 'POST'
79
+ /** The destination URL */
80
+ url: string | URL
78
81
  /**
79
82
  * i.e. user-agent, referer... (some special headers are not supported
80
83
  * by Safari and Android browsers)
81
84
  */
82
- headers?: RequestHeaders | undefined
83
- /** String to send via a POST request */
84
- data?: string | undefined
85
+ headers?: RequestHeaders
86
+ /** Data to send via a POST request */
87
+ data?: string | Blob | File | object | any[] | FormData | URLSearchParams
88
+ /** Controls what to happen when a redirect is detected (build 6180+, enforces fetch mode). */
89
+ redirect?: 'follow' | 'error' | 'manual'
85
90
  /** A cookie to be patched into the sent cookie set */
86
- cookie?: string | undefined
91
+ cookie?: string
92
+ /** Object containing the partition key to be used for sent and received partitioned cookies */
93
+ cookiePartition?: {
94
+ /** String representing the top frame site for partitioned cookies */
95
+ topLevelSite?: string
96
+ }
87
97
  /** Send the data string in binary mode */
88
- binary?: boolean | undefined
98
+ binary?: boolean
89
99
  /** Don't cache the resource */
90
- nocache?: boolean | undefined
100
+ nocache?: boolean
91
101
  /** Revalidate maybe cached content */
92
- revalidate?: boolean | undefined
102
+ revalidate?: boolean
93
103
  /** Timeout in ms */
94
- timeout?: number | undefined
104
+ timeout?: number
95
105
  /** Property which will be added to the response object */
96
- context?: TContext | undefined
97
- responseType?: 'arraybuffer' | 'blob' | 'json' | undefined
106
+ context?: TContext
107
+ responseType?: 'arraybuffer' | 'blob' | 'json' | 'stream'
98
108
  /** MIME type for the request */
99
- overrideMimeType?: string | undefined
100
- /** Don't send cookies with the requests (please see the fetch notes) */
101
- anonymous?: boolean | undefined
109
+ overrideMimeType?: string
110
+ /** Don't send cookies with the requests (enforces `fetch` mode) */
111
+ anonymous?: boolean
102
112
  /**
103
- * (Beta) Use a fetch instead of a xhr request(at Chrome this causes
113
+ * (Beta) Use a fetch instead of a xhr request (at Chrome this causes
104
114
  * `xhr.abort`, `details.timeout` and `xhr.onprogress` to not work and
105
115
  * makes `xhr.onreadystatechange` receive only readyState 4 events)
106
116
  */
107
- fetch?: boolean | undefined
117
+ fetch?: boolean
108
118
  /** Username for authentication */
109
- user?: string | undefined
110
- password?: string | undefined
119
+ user?: string
120
+ /** Password for authentication */
121
+ password?: string
111
122
 
112
123
  // Events
113
124
 
114
125
  /** Callback to be executed if the request was aborted */
115
126
  onabort?(): void
116
127
  /** Callback to be executed if the request ended up with an error */
117
- onerror?: RequestEventListener<ErrorResponse> | undefined
128
+ onerror?: RequestEventListener<ErrorResponse>
118
129
  /** Callback to be executed if the request started to load */
119
- onloadstart?: RequestEventListener<Response<TContext>> | undefined
130
+ onloadstart?: RequestEventListener<Response<TContext>>
120
131
  /** Callback to be executed if the request made some progress */
121
- onprogress?: RequestEventListener<ProgressResponse<TContext>> | undefined
132
+ onprogress?: RequestEventListener<ProgressResponse<TContext>>
122
133
  /** Callback to be executed if the request's ready state changed */
123
- onreadystatechange?: RequestEventListener<Response<TContext>> | undefined
134
+ onreadystatechange?: RequestEventListener<Response<TContext>>
124
135
  /** Callback to be executed if the request failed due to a timeout */
125
136
  ontimeout?(): void
126
137
  /** Callback to be executed if the request was loaded */
127
- onload?: RequestEventListener<Response<TContext>> | undefined
138
+ onload?: RequestEventListener<Response<TContext>>
128
139
  }
129
140
 
130
141
  // Download Response
@@ -153,31 +164,50 @@ declare namespace Tampermonkey {
153
164
  | 'not_supported'
154
165
  | 'not_succeeded'
155
166
  /** Detail about that error */
156
- details?: string | undefined
167
+ details?: string
157
168
  }
158
169
 
159
170
  // Download Request
160
171
 
161
172
  interface DownloadRequest {
162
- /** URL from where the data should be downloaded */
173
+ /**
174
+ * The URL of the file to download. This must be a valid URL and
175
+ * must point to a file that is accessible to the user.
176
+ */
163
177
  url: string
164
178
  /**
165
- * Filename - for security reasons the file extension needs to be
166
- * whitelisted at Tampermonkey options page
179
+ * The name to use for the downloaded file. This should include the file's extension,
180
+ * such as `.txt` or `.pdf`. For security reasons the file extension needs to be
181
+ * whitelisted at Tampermonkey's options page.
167
182
  */
168
183
  name: string
169
- headers?: RequestHeaders | undefined
170
- /** Show 'Save As' dialog */
171
- saveAs?: boolean | undefined
172
- timeout?: number | undefined
173
- /** Callback to be executed if this download ended up with an error */
174
- onerror?: RequestEventListener<DownloadErrorResponse> | undefined
175
- /** Callback to be executed if this download finished */
184
+ /**
185
+ * An object containing HTTP headers to include in the download request.
186
+ * See `GM_xmlhttpRequest` for more details.
187
+ */
188
+ headers?: RequestHeaders
189
+ /**
190
+ * A boolean value indicating whether to use the user's default download location,
191
+ * or to prompt the user to choose a different location.
192
+ * This option works in browser API mode only.
193
+ */
194
+ saveAs?: boolean
195
+ timeout?: number
196
+ /**
197
+ * A string that control what happens when a file with this name already exists.
198
+ * This option works in browser API mode only. Please check
199
+ * [this link](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/downloads/FilenameConflictAction)
200
+ * for more details.
201
+ */
202
+ conflictAction?: 'uniquify' | 'overwrite' | 'prompt'
203
+ /** A function to call if the download fails or is cancelled. */
204
+ onerror?: RequestEventListener<DownloadErrorResponse>
205
+ /** A callback to be executed if this download failed due to a timeout. */
176
206
  ontimeout?(): void
177
- /** Callback to be executed if this download finished */
207
+ /** A function to call when the download has completed successfully. */
178
208
  onload?(): void
179
209
  /** Callback to be executed if this download failed due to a timeout */
180
- onprogress?: RequestEventListener<DownloadProgressResponse> | undefined
210
+ onprogress?: RequestEventListener<DownloadProgressResponse>
181
211
  }
182
212
 
183
213
  interface AbortHandle<TReturn> {
@@ -185,12 +215,17 @@ declare namespace Tampermonkey {
185
215
  }
186
216
 
187
217
  interface OpenTabOptions {
188
- /** Decides whether the new tab should be focused */
189
- active?: boolean | undefined
190
- /** Inserts the new tab after the current one */
191
- insert?: boolean | undefined
192
- /** Makes the browser re-focus the current tab on close */
193
- setParent?: boolean | undefined
218
+ /** A boolean value indicating whether the new tab should be active (selected) or not (default is `false`). */
219
+ active?: boolean
220
+ /**
221
+ * An integer indicating the position at which the new tab should be inserted in the tab strip.
222
+ * The default is `false`, which means the new tab will be added to the end of the tab strip.
223
+ */
224
+ insert?: number | boolean
225
+ /** A boolean value indicating whether the new tab should be considered a child of the current tab (default is `false`). */
226
+ setParent?: boolean
227
+ /** A boolean value that makes the tab being opened inside a incognito mode/private mode window. */
228
+ incognito?: boolean
194
229
  }
195
230
 
196
231
  interface OpenTabObject {
@@ -210,24 +245,39 @@ declare namespace Tampermonkey {
210
245
  type NotificationOnDone = (this: NotificationThis, clicked: boolean) => void
211
246
 
212
247
  interface Notification {
213
- /** Text of the notification (optional if highlight is set) */
214
- text?: string | undefined
215
- /** Notification title. If not specified the script name is used */
216
- title?: string | undefined
217
- image?: string | undefined
218
- /** Flag whether to highlight the tab that sends the notification */
219
- highlight?: boolean | undefined
220
- /** Whether to play or not play a sound */
221
- silent?: boolean | undefined
222
- /** Time after that the notification will be hidden. `0` = disabled */
223
- timeout?: number | undefined
248
+ /** A string containing the message to display in the notification (optional if highlight is set). */
249
+ text?: string
250
+ /** The title of the notification. If not specified the script name is used. */
251
+ title?: string
252
+ /**
253
+ * This tag will be used to identify this notification. This way you can update existing notifications
254
+ * by calling `GM_notification` again and using the same tag. If you don't provide a tag,
255
+ * a new notification will be created every time.
256
+ */
257
+ tag?: string
258
+ /** The URL of an image to display in the notification. */
259
+ image?: string
260
+ /** Flag whether to highlight the tab that sends the notification. */
261
+ highlight?: boolean
262
+ /** Whether to play or not play a sound. */
263
+ silent?: boolean
264
+ /**
265
+ * The time, in milliseconds, after which the notification
266
+ * should automatically close. `0` = disabled.
267
+ */
268
+ timeout?: number
269
+ /**
270
+ * A URL to load when the user clicks on the notification. You can prevent loading the URL
271
+ * by calling `event.preventDefault()` in the `onclick` event handler.
272
+ */
273
+ url?: string
224
274
  /**
225
275
  * Called when the notification is closed (no matter if this was
226
- * triggered by a timeout or a click) or the tab was highlighted
276
+ * triggered by a timeout or a click) or the tab was highlighted.
227
277
  */
228
- onclick?: NotificationOnClick | undefined
229
- /** Called in case the user clicks the notification */
230
- ondone?: NotificationOnDone | undefined
278
+ onclick?: NotificationOnClick
279
+ /** Called in case the user clicks the notification. */
280
+ ondone?: NotificationOnDone
231
281
  }
232
282
 
233
283
  interface TextNotification extends Notification {
@@ -236,7 +286,9 @@ declare namespace Tampermonkey {
236
286
  }
237
287
 
238
288
  interface HighlightNotification extends Notification {
289
+ /** A string containing the message to display in the notification. */
239
290
  text?: undefined
291
+ /** Whether to highlight the tab that sends the notfication (required unless text is set) */
240
292
  highlight: true
241
293
  }
242
294
 
@@ -333,7 +385,7 @@ declare namespace Tampermonkey {
333
385
  blockers: string[]
334
386
 
335
387
  copyright: string | null
336
- deleted?: number | undefined
388
+ deleted?: number
337
389
  description: string | null
338
390
  description_i18n: Record<string, string> | null
339
391
  downloadURL: string | null
@@ -364,9 +416,9 @@ declare namespace Tampermonkey {
364
416
 
365
417
  supportURL: string | null
366
418
  sync?: {
367
- imported?: number | undefined
419
+ imported?: number
368
420
  }
369
- system?: boolean | undefined
421
+ system?: boolean
370
422
  unwrap: boolean
371
423
  updateURL: string | null
372
424
  uuid: string
@@ -397,23 +449,185 @@ declare namespace Tampermonkey {
397
449
  version?: string
398
450
  }
399
451
 
400
- type ContentType =
401
- | string
402
- | { type?: string | undefined; mimetype?: string | undefined }
452
+ type ContentType = string | { type?: string; mimetype?: string }
453
+
454
+ // GM_webRequest
455
+ interface WebRequestRuleParam {
456
+ /**
457
+ * Specifies the URLs for which the rule should be triggered.
458
+ * String value is shortening for `{ include: [selector] }`.
459
+ */
460
+ selector:
461
+ | string
462
+ | {
463
+ /** URLs, patterns, and regexpes for rule triggering. */
464
+ include?: string | string[]
465
+ /** URLs and patterns for rule triggering. */
466
+ match?: string | string[]
467
+ /** URLs, patterns, and regexpes for not triggering the rule. */
468
+ exclude?: string | string[]
469
+ }
470
+ /**
471
+ * Specifies to do with the request.
472
+ * String value `cancel` is shortening for `{ cancel: true }`.
473
+ */
474
+ action:
475
+ | 'cancel'
476
+ | {
477
+ /** Whether to cancel the request. */
478
+ cancel?: boolean
479
+ /**
480
+ * Redirect to some URL which must be included in any
481
+ * `@match` or `@include` header.
482
+ * When a string, redirects to a given static URL.
483
+ */
484
+ redirect?:
485
+ | string
486
+ | {
487
+ /** A RegExp to extract some parts of the URL (for example `"([^:]+)://match.me/(.*)"`). */
488
+ from: string
489
+ /** Pattern for substitution (for example `"$1://redirected.to/$2"`). */
490
+ to: string
491
+ }
492
+ }
493
+ }
494
+
495
+ type WebRequestListener = (
496
+ /** The type of the action. */
497
+ info: 'cancel' | 'redirect',
498
+ message: 'ok' | 'error',
499
+ /** Info about the request and rule. */
500
+ details: {
501
+ /** The triggered rule */
502
+ rule: WebRequestRuleParam
503
+ /** The URL of the request. */
504
+ url?: string
505
+ /** Where the request was redirected. */
506
+ redirect_url?: string
507
+ /** Error description. */
508
+ description?: string
509
+ }
510
+ ) => void
511
+
512
+ // GM_cookie.*
513
+ interface Cookie {
514
+ /** The domain of the cookie. */
515
+ domain: string
516
+ /** The first-party domain of the cookie. */
517
+ firstPartyDomain?: string
518
+ /** The partition key of the cookie. */
519
+ partitionKey?: {
520
+ /** The top frame site of the cookie. */
521
+ topLevelSite?: string
522
+ }
523
+ /** Indicates whether the cookie is a host-only cookie. */
524
+ hostOnly: boolean
525
+ /** Indicates whether the cookie is an HTTP-only cookie. */
526
+ httpOnly: boolean
527
+ /** The name of the cookie. */
528
+ name: string
529
+ /** The path of the cookie. */
530
+ path: string
531
+ /** The `SameSite` attribute of the cookie */
532
+ sameSite: string
533
+ /** Whether the cookie requires a secure connection. */
534
+ secure: boolean
535
+ /** Whether the cookie is a session cookie. */
536
+ session: boolean
537
+ /** The value of the cookie. */
538
+ value: string
539
+ /** The date the cookie expires in seconds since the Unix epoch. */
540
+ expirationDate?: number
541
+ }
542
+
543
+ interface ListCookiesDetails {
544
+ /** The URL to retrieve cookies from (defaults to current document URL). */
545
+ url?: string
546
+ /** The domain of the cookies to retrieve. */
547
+ domain?: string
548
+ /** The name of the cookies to retrieve. */
549
+ name?: string
550
+ /** The path of the cookies to retrieve. */
551
+ path?: string
552
+ /**
553
+ * Object containing the partition key to be used for sent and received partitioned cookies.
554
+ * Use an empty object to retrieve all cookies.
555
+ */
556
+ partitionKey?: {
557
+ /** String representing the top frame site of the cookies */
558
+ topLevelSite?: string
559
+ }
560
+ }
561
+
562
+ type ListCookiesCallback = (
563
+ /** An array containing the retrieved cookies. */
564
+ cookies: Cookie[],
565
+ /** An error message if an error occurred, `null` otherwise. */
566
+ error: string | null
567
+ ) => void
568
+
569
+ interface SetCookiesDetails {
570
+ /**
571
+ * The URL to associate the cookie with. If not specified,
572
+ * the cookie is associated with the current document's URL.
573
+ */
574
+ url?: string
575
+ /** The name of the cookie. */
576
+ name: string
577
+ /** The value of the cookie. */
578
+ value: string
579
+ /** The domain of the cookie. */
580
+ domain?: string
581
+ /** The first-party domain of the cookie. */
582
+ firstPartyDomain?: string
583
+ /** The partition key of the cookie. */
584
+ partitionKey?: {
585
+ /** The top frame site of the cookie. */
586
+ topLevelSite?: string
587
+ }
588
+ /** The path of the cookie. */
589
+ path?: string
590
+ /** Whether the cookie should only be sent over HTTPS. */
591
+ secure?: boolean
592
+ /** Whether the cookie should be marked as `HttpOnly`. */
593
+ httpOnly?: boolean
594
+ /**
595
+ * The expiration date of the cookie in seconds since the Unix epoch.
596
+ * If not specified, the cookie never expires.
597
+ */
598
+ expirationDate?: number
599
+ }
600
+
601
+ interface DeleteCookiesDetails {
602
+ /**
603
+ * The URL associated with the cookie. If `url` is not specified,
604
+ * the current document's URL will be used.
605
+ */
606
+ url: string
607
+ /** The name of the cookie to delete. */
608
+ name: string
609
+ /** The first party domain of the cookie to delete. */
610
+ firstPartyDomain: string
611
+ /** The partition key of the cookie to delete. */
612
+ partitionKey: {
613
+ /** The top frame site of the cookies. */
614
+ topLevelSite?: string
615
+ }
616
+ }
403
617
  }
404
618
 
405
619
  /**
406
- * The unsafeWindow object provides full access to the pages javascript
620
+ * The unsafeWindow object provides full access to the pages JavaScript
407
621
  * functions and variables
408
622
  */
409
623
  declare var unsafeWindow: Window &
410
624
  Omit<
411
625
  typeof globalThis,
626
+ | 'GM_addElement'
412
627
  | 'GM_addStyle'
413
628
  | 'GM_addValueChangeListener'
414
629
  | 'GM_deleteValue'
415
630
  | 'GM_download'
416
- | 'GM_download'
417
631
  | 'GM_getResourceText'
418
632
  | 'GM_getResourceURL'
419
633
  | 'GM_getTab'
@@ -423,7 +637,6 @@ declare var unsafeWindow: Window &
423
637
  | 'GM_listValues'
424
638
  | 'GM_log'
425
639
  | 'GM_notification'
426
- | 'GM_notification'
427
640
  | 'GM_openInTab'
428
641
  | 'GM_registerMenuCommand'
429
642
  | 'GM_removeValueChangeListener'
@@ -436,7 +649,6 @@ declare var unsafeWindow: Window &
436
649
  >
437
650
 
438
651
  /**
439
- *
440
652
  * Patched onurlchange attribute based on document {@link https://www.tampermonkey.net/documentation.php#meta:grant}
441
653
  * @url https://www.tampermonkey.net/documentation.php#meta:grant
442
654
  */
@@ -458,86 +670,222 @@ interface Window {
458
670
  ): void
459
671
  }
460
672
 
673
+ /**
674
+ * `GM_addElement` allows Tampermonkey scripts to add new elements to the page
675
+ * that Tampermonkey is running on. This can be useful for a variety of purposes,
676
+ * such as adding `script` and `img` tags if the page limits these elements
677
+ * with a content security policy (CSP).
678
+ *
679
+ * The resulting HTML element will be attached to document head or body.
680
+ *
681
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_addElement
682
+ * @param tagName Specifies the HTML element tag name.
683
+ * @param attributes Attributes that applied to the HTML element.
684
+ * For suitable `attributes`, please consult the appropriate documentation. For example:
685
+ *
686
+ * - [`script` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script)
687
+ * - [`img` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img)
688
+ * - [`style` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style)
689
+ * @returns The injected HTML element
690
+ */
691
+ declare function GM_addElement(tagName: string, attributes: object): HTMLElement
692
+
693
+ /**
694
+ * `GM_addElement` allows Tampermonkey scripts to add new elements to the page
695
+ * that Tampermonkey is running on. This can be useful for a variety of purposes,
696
+ * such as adding `script` and `img` tags if the page limits these elements
697
+ * with a content security policy (CSP).
698
+ *
699
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_addElement
700
+ * @param parentNode The node the resulting HTML element will be attached to.
701
+ * @param tagName Specifies the HTML element tag name.
702
+ * @param attributes Attributes that applied to the HTML element.
703
+ * For suitable `attributes`, please consult the appropriate documentation. For example:
704
+ *
705
+ * - [`script` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script)
706
+ * - [`img` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img)
707
+ * - [`style` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style)
708
+ *
709
+ * @returns The injected HTML element
710
+ */
711
+ declare function GM_addElement(
712
+ parentNode: Element,
713
+ tagName: string,
714
+ attributes: object
715
+ ): HTMLElement
716
+
461
717
  // Styles
462
718
 
463
719
  /**
464
- * Adds the given style to the document and returns the injected style element.
720
+ * Applies the given style to the document.
721
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_addStyle
722
+ * @param css The styles to apply.
723
+ * @returns The injected style element.
465
724
  */
466
725
  declare function GM_addStyle(css: string): HTMLStyleElement
467
726
 
468
727
  // Storage
469
728
 
470
- /** Sets the value of `name` to the storage */
729
+ /**
730
+ * Sets the value of a specific key in the userscript's storage.
731
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_setValue
732
+ * @param name A string specifying the key for which the value should be set.
733
+ * @param value The value to be set for the key.
734
+ */
471
735
  declare function GM_setValue(name: string, value: any): void
472
736
 
473
737
  /**
474
- * Adds a change listener to the storage and returns the listener ID.
475
- * The `remote` argument of the callback function shows whether this value was
476
- * modified from the instance of another tab (`true`) or within this script
477
- * instance (`false`). Therefore this functionality can be used by scripts of
478
- * different browser tabs to communicate with each other.
479
- * @param name Name of the observed variable
738
+ * Adds a listener for changes to the value of a specific key in the userscript's storage.
739
+ * This functionality can be used by scripts of different browser tabs to communicate with each other.
740
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_addValueChangeListener
741
+ * @param name A string specifying the key for which changes should be monitored.
742
+ * @param listener A callback function that will be called when the value of the key changes.
743
+ * @returns A `listenerId` value that can be used to remove the listener later using `GM_removeValueChangeListener`.
480
744
  */
481
745
  declare function GM_addValueChangeListener(
482
746
  name: string,
483
747
  listener: Tampermonkey.ValueChangeListener
484
748
  ): number
485
749
 
486
- /** Removes a change listener by its ID */
750
+ /**
751
+ * Removes a change listener by its ID.
752
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_removeValueChangeListener
753
+ */
487
754
  declare function GM_removeValueChangeListener(listenerId: number): void
488
755
 
489
- /** Gets the value of 'name' from storage */
756
+ /**
757
+ * Retrieves the value of a specific key in the extension's storage.
758
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_getValue
759
+ * @param name A string specifying the key for which the value should be retrieved.
760
+ * @param defaultValue A default value to be returned if the key does not exist in the extension's storage.
761
+ * @returns The value of the specified key from the extension's storage, or the default value if the key does not exist.
762
+ */
490
763
  declare function GM_getValue<TValue>(
491
764
  name: string,
492
765
  defaultValue?: TValue
493
766
  ): TValue
494
767
 
495
- /** Deletes 'name' from storage */
768
+ /**
769
+ * Deletes `name` from the userscript's storage.
770
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_deleteValue
771
+ * @param name A string specifying the key that should be deleted from storage.
772
+ */
496
773
  declare function GM_deleteValue(name: string): void
497
774
 
498
- /** Lists all names of the storage */
775
+ /**
776
+ * Returns a list of keys of all stored data.
777
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_listValues
778
+ */
499
779
  declare function GM_listValues(): string[]
500
780
 
501
781
  // Resources
502
782
 
503
- /** Get the content of a predefined `@resource` tag at the script header */
783
+ /**
784
+ * Retrieves the text of a resource (such as a JavaScript or CSS file)
785
+ * that has been included in a userscript via @resource.
786
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_getResourceText
787
+ * @param name The name of the resource to retrieve.
788
+ * @returns The text of the resource as a string.
789
+ */
504
790
  declare function GM_getResourceText(name: string): string
505
791
 
506
792
  /**
507
793
  * Get the base64 encoded URI of a predefined `@resource` tag at the script
508
794
  * header
795
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_getResourceURL
796
+ * @param name The name of the resource to retrieve.
797
+ * @returns The URL of the resource as a string.
509
798
  */
510
799
  declare function GM_getResourceURL(name: string): string
511
800
 
512
801
  // Menu commands
513
802
 
514
803
  /**
515
- * Register a menu to be displayed at the Tampermonkey menu at pages where this
516
- * script runs and returns a menu command ID.
804
+ * Adds a new entry to the userscript's menu in the browser,
805
+ * and specifies a function to be called when the menu item is selected.
806
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_registerMenuCommand
807
+ * @param name A string containing the text to display for the menu item.
808
+ * @param onclick A function to be called when the menu item is selected.
809
+ * The function will be passed a single parameter,
810
+ * which is the currently active tab. As of Tampermonkey 4.14,
811
+ * a `MouseEvent` or `KeyboardEvent` is passed as function argument.
812
+ * @param optionsOrAccessKey An access key or options to customize the menu item.
813
+ * @returns A menu entry ID that can be used to unregister the command.
517
814
  */
518
815
  declare function GM_registerMenuCommand(
519
816
  name: string,
520
- onClick: () => void,
521
- accessKey?: string
817
+ onClick: (event: MouseEvent | KeyboardEvent) => void,
818
+ optionsOrAccessKey?:
819
+ | string
820
+ | {
821
+ /**
822
+ * An optional number that was returned by a previous `GM_registerMenuCommand` call.
823
+ * If specified, the according menu item will be updated with the new options.
824
+ * If not specified or the menu item can't be found, a new menu item will be created.
825
+ */
826
+ id?: number | string
827
+ /**
828
+ * An optional access key for the menu item. This can be used to create a shortcut for the menu item.
829
+ * For example, if the access key is "s", the user can select the menu item by pressing "s"
830
+ * when Tampermonkey's popup-menu is open. Please note that there are browser-wide shortcuts
831
+ * configurable to open Tampermonkey's popup-menu.
832
+ */
833
+ accessKey?: string
834
+ /**
835
+ * An optional boolean parameter that specifies whether the popup menu should be closed
836
+ * after the menu item is clicked. The default value is `true`. Please note that this setting
837
+ * has no effect on the menu command section that is added to the page's context menu.
838
+ */
839
+ autoClose?: boolean
840
+ /**
841
+ * An optional string that specifies the title of the menu item. This is displayed
842
+ * as a tooltip when the user hovers the mouse over the menu item.
843
+ */
844
+ title?: string
845
+ }
522
846
  ): number
523
847
 
524
848
  /**
525
- * Unregister a menu command that was previously registered by
526
- * `GM_registerMenuCommand` or `GM.registerMenuCommand` with the given menu command ID.
849
+ * Removes an existing entry from the userscript's menu in the browser
850
+ * that was previously registered by `GM_registerMenuCommand` or `GM.registerMenuCommand`
851
+ * with the given menu command ID.
852
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_unregisterMenuCommand
853
+ * @param menuCommandId The id of the menu item to remove.
527
854
  */
528
855
  declare function GM_unregisterMenuCommand(menuCommandId: number): void
529
856
 
530
857
  // Requests
531
858
 
532
- /** Makes an xmlHttpRequest */
533
- declare function GM_xmlhttpRequest<TContext = any>(
534
- details: Tampermonkey.Request<TContext> // eslint-disable-line no-unnecessary-generics
859
+ /**
860
+ * Sends an HTTP request and handles the response.
861
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_xmlhttpRequest
862
+ * @param details An object containing the details of the request to be sent
863
+ * and the callback functions to be called when the response is received.
864
+ */
865
+ declare function GM_xmlhttpRequest<TContext = any>( // eslint-disable-line @definitelytyped/no-unnecessary-generics
866
+ details: Tampermonkey.Request<TContext>
535
867
  ): Tampermonkey.AbortHandle<void>
536
868
 
537
- /** Downloads a given URL to the local disk */
869
+ /**
870
+ * Downloads a file from a specified URL and save it to the user's local machine.
871
+ * Note: The browser might modify the desired filename. Especially a file extension might
872
+ * be added if the browser finds this to be safe to download at the current OS.
873
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_download
874
+ * @param details Details about the download.
875
+ */
538
876
  declare function GM_download(
539
877
  details: Tampermonkey.DownloadRequest
540
878
  ): Tampermonkey.AbortHandle<boolean>
879
+ /**
880
+ * Downloads a file from a specified URL and save it to the user's local machine.
881
+ * Note: The browser might modify the desired filename. Especially a file extension might
882
+ * be added if the browser finds this to be safe to download at the current OS.
883
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_download
884
+ * @param url The URL of the file to download. This must be a valid URL and must point to a file that is accessible to the user.
885
+ * @param name The name to use for the downloaded file. This should include the file's extension,
886
+ * such as `.txt` or `.pdf`. For security reasons the file extension needs to be whitelisted
887
+ * at Tampermonkey's options page
888
+ */
541
889
  declare function GM_download(
542
890
  url: string,
543
891
  name: string
@@ -545,22 +893,49 @@ declare function GM_download(
545
893
 
546
894
  // Tabs
547
895
 
548
- /** Saves the tab object to reopen it after a page unload */
549
- declare function GM_saveTab(obj: object): void
896
+ /**
897
+ * Saves information about a tab so that it can be retrieved later
898
+ * using the `GM_getTab` function.
899
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_saveTab
900
+ * @param tab An object containing the information to be saved about the tab.
901
+ * @param callback An optional callback function
902
+ */
903
+ declare function GM_saveTab(tab: object, callback?: () => void): void
550
904
 
551
- /** Gets a object that is persistent as long as this tab is open */
905
+ /**
906
+ * Gets a object that is persistent as long as this tab is open
907
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_getTab
908
+ * @param callback Function that will be called with an object that is persistent as long as this tab is open.
909
+ */
552
910
  declare function GM_getTab(callback: (obj: any) => void): void
553
911
 
554
- /** Gets all tab objects as a hash to communicate with other script instances */
912
+ /**
913
+ * Gets all tab objects as a hash to communicate with other script instances
914
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_getTabs
915
+ * @param callback A callback function that will be called with the information about the tabs..
916
+ */
555
917
  declare function GM_getTabs(
556
- callback: (tabsMap: { [tabId: number]: any }) => void
918
+ callback: (
919
+ /**
920
+ * The `tabsMap` object that is passed to the callback function contains objects,
921
+ * with each object representing the saved tab information stored by `GM_saveTab`.
922
+ */
923
+ tabsMap: { [tabId: number]: any }
924
+ ) => void
557
925
  ): void
558
926
 
559
927
  // Utils
560
928
 
929
+ /**
930
+ * Returns information about the script and Tampermonkey.
931
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_info
932
+ */
561
933
  declare var GM_info: Tampermonkey.ScriptInfo
562
934
 
563
- /** Log a message to the console */
935
+ /**
936
+ * Logs a message to the console
937
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_log
938
+ */
564
939
  declare function GM_log(...message: any[]): void
565
940
 
566
941
  /**
@@ -571,12 +946,15 @@ declare function GM_log(...message: any[]): void
571
946
  * - `setParent` makes the browser re-focus the current tab on close.
572
947
  *
573
948
  * Otherwise the new tab is just appended.
574
- * If `options` is boolean (loadInBackground) it has the opposite meaning of
949
+ * If `options` is boolean (`loadInBackground`) it has the opposite meaning of
575
950
  * active and was added to achieve Greasemonkey 3.x compatibility.
576
951
  *
577
- * If neither active nor loadInBackground is given, then the tab will not be
952
+ * If neither `active` nor `loadInBackground` is given, then the tab will not be
578
953
  * focused.
579
- * @returns Object with the function `close`, the listener `onclose` and a flag
954
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_openInTab
955
+ * @param url The URL of the page to open in the new tab.
956
+ * @param options An object that can be used to customize the behavior of the new tab.
957
+ * @returns An object with the function `close`, the listener `onclose` and a flag
580
958
  * called `closed`.
581
959
  */
582
960
  declare function GM_openInTab(
@@ -585,8 +963,16 @@ declare function GM_openInTab(
585
963
  ): Tampermonkey.OpenTabObject
586
964
 
587
965
  /**
588
- * Shows a HTML5 Desktop notification and/or highlight the current tab.
589
- * @param ondone If specified used instead of `details.ondone`
966
+ * Shows an HTML5 Desktop notification and/or highlight the current tab
967
+ * using a provided message and other optional parameters.
968
+ *
969
+ * Since v5.0, if no `url` and no `tag` is provided in `details` argument, the notification will close
970
+ * when the userscript unloads (e.g. when the page is reloaded or the tab is closed).
971
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_notification
972
+ * @param details Notification parameters.
973
+ * @param ondone A callback function that will be called when the notification is closed
974
+ * (no matter if this was triggered by a timeout or a click) or the tab was highlighted.
975
+ * If specified, used instead of `details.ondone`.
590
976
  */
591
977
  declare function GM_notification(
592
978
  details: Tampermonkey.NotificationDetails,
@@ -594,33 +980,120 @@ declare function GM_notification(
594
980
  ): void
595
981
 
596
982
  /**
597
- * Shows a HTML5 Desktop notification and/or highlight the current tab.
598
- * @param text Text of the notification
599
- * @param title Notification title. If not specified the script name is used
600
- * @param onclick Called in case the user clicks the notification
983
+ * Shows an HTML5 Desktop notification and/or highlight the current tab
984
+ * using a provided message and other optional parameters.
985
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_notification
986
+ * @param text A string containing the message to display in the notification.
987
+ * @param title The title of the notification. If not specified, the script name is used.
988
+ * @param image The URL of an image to display in the notification.
989
+ * @param onClick A callback function that will be called when the user clicks on the notification.
601
990
  */
602
991
  declare function GM_notification(
603
992
  text: string,
604
993
  title?: string,
605
994
  image?: string,
606
- onclick?: Tampermonkey.NotificationOnClick
995
+ onClick?: Tampermonkey.NotificationOnClick
607
996
  ): void
608
997
 
609
998
  /**
610
- * Copies data into the clipboard.
999
+ * Sets the text of the clipboard to a specified value.
611
1000
  * The parameter 'info' can be an object like
612
1001
  * `{ type: 'text', mimetype: 'text/plain'}` or just a string expressing the
613
1002
  * type ("text" or "html").
1003
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_setClipboard
1004
+ * @param data The string to set as the clipboard text.
1005
+ * @param info A string expressing the type `text` or `html` or an object.
1006
+ * @param callback An optional callback function that is called when the clipboard has been set.
614
1007
  */
615
1008
  declare function GM_setClipboard(
616
1009
  data: string,
617
- info?: Tampermonkey.ContentType
1010
+ info?: Tampermonkey.ContentType,
1011
+ callback?: () => void
618
1012
  ): void
619
1013
 
1014
+ /**
1015
+ * `GM_webRequest` (re-)registers rules for web request manipulations
1016
+ * and the listener of triggered rules. If you need to just register rules
1017
+ * it's better to use `@webRequest` header. Note, `webRequest` proceeds only requests
1018
+ * with types `sub_frame`, `script`, `xhr` and `websocket`.
1019
+ * **Note: this API is experimental and might change at any time.**
1020
+ * It might also disappear or change during manifest v3 migration.
1021
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_webRequest
1022
+ * @param rules An array of rules.
1023
+ * @param listener A function called when the rule is triggered. It cannot impact on the rule action.
1024
+ * @returns An object with an `.abort()` method.
1025
+ */
1026
+ declare function GM_webRequest(
1027
+ rules: Tampermonkey.WebRequestRuleParam[],
1028
+ listener?: Tampermonkey.WebRequestListener
1029
+ ): Tampermonkey.AbortHandle<void>
1030
+
1031
+ // GM_cookie.*
1032
+
1033
+ // https://stackoverflow.com/a/59987826
1034
+ // for GM_cookie.delete()
1035
+ type AtLeastOneOf<T> = { [K in keyof T]: Pick<T, K> }[keyof T]
1036
+
1037
+ declare var GM_cookie: {
1038
+ /**
1039
+ * Retrieves all cookies whose properties match those given.
1040
+ * Tampermonkey checks if the script has `@include` or `@match`
1041
+ * access to given `details.url` arguments!
1042
+ *
1043
+ * **Note: the `GM_cookie` API is experimental and might
1044
+ * return a `not supported` error at some Tampermonkey versions.**
1045
+ * @url https://www.tampermonkey.net/documentation.php#api:GM_cookie.list
1046
+ * @param details Object containing properties of the cookies to retrieve.
1047
+ * @param callback Function to be called when the cookies have been retrieved.
1048
+ */
1049
+ list(
1050
+ details?: Tampermonkey.ListCookiesDetails,
1051
+ callback?: Tampermonkey.ListCookiesCallback
1052
+ ): void
1053
+
1054
+ /**
1055
+ * Sets a cookie with the given details. Supported properties
1056
+ * [are defined here](https://developer.chrome.com/extensions/cookies#method-set).
1057
+ *
1058
+ * **Note: the `GM_cookie` API is experimental and might
1059
+ * return a `not supported` error at some Tampermonkey versions.**
1060
+ * @url https://www.tampermonkey.net/documentation.php##api:GM_cookie.set
1061
+ * @param details An object containing the details of the cookie to be set.
1062
+ * @param callback A function to be called when the operation is complete.
1063
+ */
1064
+ set(
1065
+ details: Tampermonkey.SetCookiesDetails,
1066
+ callback?: (
1067
+ /**
1068
+ * If there was an error setting the cookie, this contains
1069
+ * an error message. Otherwise, it is `undefined`.
1070
+ */
1071
+ error?: string
1072
+ ) => void
1073
+ ): void
1074
+
1075
+ /**
1076
+ * Deletes a cookie whose properties match those given.
1077
+ *
1078
+ * **Note: the `GM_cookie` API is experimental and might
1079
+ * return a `not supported` error at some Tampermonkey versions.**
1080
+ * @url https://www.tampermonkey.net/documentation.php##api:GM_cookie.delete
1081
+ * @param details An object containing the details of the cookie to be deleted.
1082
+ * @param callback Function called when the cookie has been deleted or when an error has occurred.
1083
+ */
1084
+ delete(
1085
+ details: AtLeastOneOf<Tampermonkey.DeleteCookiesDetails>,
1086
+ callback?: (
1087
+ /** An error message, or `undefined` if the cookie was deleted successfully. */
1088
+ error?: string
1089
+ ) => void
1090
+ ): void
1091
+ }
1092
+
620
1093
  // GM.*
621
1094
 
622
1095
  /**
623
- * `GM` has all the `GM_*` apis in promisified form
1096
+ * `GM` has all the `GM_*` APIs in promisified form
624
1097
  */
625
1098
  declare var GM: Readonly<{
626
1099
  // Styles
@@ -698,7 +1171,7 @@ declare var GM: Readonly<{
698
1171
  */
699
1172
  xmlHttpRequest<TContext = any>(
700
1173
  // onload and the like still work
701
- details: Tampermonkey.Request<TContext> // eslint-disable-line no-unnecessary-generics
1174
+ details: Tampermonkey.Request<TContext> // eslint-disable-line @definitelytyped/no-unnecessary-generics
702
1175
  ): Promise<Tampermonkey.Response<TContext>>
703
1176
 
704
1177
  // GM_download has two signatures, GM.download has one