vite-userscript-plugin 1.9.2 → 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,246 +1,308 @@
1
- // Type definitions for Greasemonkey 3.x
2
- // Project: http://www.greasespot.net/
3
- // Definitions by: Kota Saito <https://github.com/kotas>
4
- // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
1
+ // This definition is based on the API reference of Greasemonkey
2
+ // https://wiki.greasespot.net/Greasemonkey_Manual:API
3
+ // TypeScript Version: 3.2
5
4
 
6
- // This definition is based on the API reference of Greasemonkey.
7
- // http://wiki.greasespot.net/Greasemonkey_Manual:API
8
-
9
- ////////////////
10
- // Global variable
11
- ////////////////
12
-
13
- /**
14
- * Window object of the content page where the user script is running on.
15
- * @see {@link http://wiki.greasespot.net/UnsafeWindow}
16
- */
17
- declare var unsafeWindow: Window
18
-
19
- /**
20
- * Meta data about the running user script.
21
- * @see {@link http://wiki.greasespot.net/GM_info}
22
- */
23
- declare var GM_info: {
24
- script: {
5
+ declare namespace GM {
6
+ interface ScriptInfo {
7
+ /** Possibly empty string. */
25
8
  description: string
26
9
  excludes: string[]
27
10
  includes: string[]
28
11
  matches: string[]
29
12
  name: string
13
+ /** Possibly empty string. */
30
14
  namespace: string
31
- resources: Object
32
- 'run-at': string
33
- unwrap: boolean
15
+ /**
16
+ * An object keyed by resource name.
17
+ * Each value is an object with keys `name` and `mimetype` and `url`
18
+ * with string values.
19
+ */
20
+ resources: {
21
+ [resourceName: string]: {
22
+ name: string
23
+ mimetype: string
24
+ url: string
25
+ }
26
+ }
27
+ /** @default 'end' */
28
+ runAt: 'start' | 'end' | 'idle'
29
+ uuid: string
30
+ /** Possibly empty string. */
34
31
  version: string
35
32
  }
36
- scriptMetaStr: string
37
- scriptWillUpdate: boolean
38
- version: string
39
- }
40
33
 
41
- ////////////////
42
- // Values
43
- ////////////////
34
+ type Value = string | boolean | number
44
35
 
45
- /**
46
- * Deletes an existing name / value pair from the script storage.
47
- * @param name a name of the pair to delete.
48
- * @see {@link http://wiki.greasespot.net/GM_deleteValue}
49
- */
50
- declare function GM_deleteValue(name: string): void
36
+ interface Response<TContext> {
37
+ readonly responseHeaders: string
38
+ readonly finalUrl: string
39
+ /** The same object passed into the original request */
40
+ readonly context?: TContext | undefined
51
41
 
52
- /**
53
- * Retrieves a value from the script storage.
54
- * @param name a name to retrieve.
55
- * @param defaultValue a value to be returned when the name does not exist.
56
- * @returns a retrieved value, or passed default value, or undefined.
57
- * @see {@link http://wiki.greasespot.net/GM_getValue}
58
- */
59
- declare function GM_getValue(name: string, defaultValue?: any): any
60
- declare function GM_getValue(name: string, defaultValue?: string): string
61
- declare function GM_getValue(name: string, defaultValue?: number): number
62
- declare function GM_getValue(name: string, defaultValue?: boolean): boolean
42
+ readonly readyState: 1 | 2 | 3 | 4
43
+ readonly response: any
44
+ readonly responseText: string
45
+ readonly responseXML: Document | false
46
+ readonly status: number
47
+ readonly statusText: string
48
+ }
63
49
 
64
- /**
65
- * Retrieves an array of names stored in the script storage.
66
- * @returns an array of names in the storage.
67
- * @see {@link http://wiki.greasespot.net/GM_listValues}
68
- */
69
- declare function GM_listValues(): string[]
50
+ interface ProgressResponse<TContext> extends Response<TContext> {
51
+ lengthComputable: boolean
52
+ loaded: number
53
+ total: number
54
+ }
70
55
 
71
- /**
72
- * Stores a name / value pair to the script storage.
73
- * @param name a name of the pair.
74
- * @param value a value to be stored.
75
- * @see {@link http://wiki.greasespot.net/GM_setValue}
76
- */
77
- declare function GM_setValue(name: string, value: string): void
78
- declare function GM_setValue(name: string, value: boolean): void
79
- declare function GM_setValue(name: string, value: number): void
56
+ interface Request<TContext = any> {
57
+ // Fields
80
58
 
81
- ////////////////
82
- // Resources
83
- ////////////////
59
+ /**
60
+ * The URL to make the request to. Must be an absolute URL, beginning
61
+ * with the scheme. May be relative to the current page.
62
+ */
63
+ url: string
64
+ /** String type of HTTP request to make (E.G. "GET", "POST") */
65
+ method:
66
+ | 'GET'
67
+ | 'POST'
68
+ | 'PUT'
69
+ | 'DELETE'
70
+ | 'PATCH'
71
+ | 'HEAD'
72
+ | 'TRACE'
73
+ | 'OPTIONS'
74
+ | 'CONNECT'
75
+ /**
76
+ * When true, the data is sent as a Blob
77
+ * @default false
78
+ */
79
+ binary?: boolean | undefined
80
+ /**
81
+ * Any object (Compatibility: 1.10+). This object will also be the
82
+ * context property of the Response Object.
83
+ */
84
+ context?: TContext | undefined
85
+ /**
86
+ * Data to send in the request body. Usually for POST method requests.
87
+ * If the data field contains form-encoded data, you usually must also
88
+ * set the header `'Content-Type': 'application/x-www-form-urlencoded'`
89
+ * in the `headers` field.
90
+ */
91
+ data?: string | undefined
92
+ /** A set of headers to include in the request */
93
+ headers?:
94
+ | {
95
+ [header: string]: string
96
+ }
97
+ | undefined
98
+ /**
99
+ * A MIME type to specify with the request (e.g.
100
+ * "text/html; charset=ISO-8859-1")
101
+ */
102
+ overrideMimeType?: string | undefined
103
+ /** User name to use for authentication purposes. */
104
+ user?: string | undefined
105
+ /** Password to use for authentication purposes */
106
+ password?: string | undefined
107
+ /** Decode the response as specified type. Default value is "text" */
108
+ responseType?: XMLHttpRequestResponseType | undefined
109
+ /**
110
+ * When `true`, this is a synchronous request.
111
+ * Be careful: The entire Firefox UI will be locked and frozen until the
112
+ * request completes.In this mode, more data will be available in the
113
+ * return value.
114
+ */
115
+ synchronous?: boolean | undefined
116
+ /**
117
+ * The number of milliseconds to wait before terminating the call. Zero
118
+ * (the default) means wait forever.
119
+ */
120
+ timeout?: number | undefined
121
+ /**
122
+ * Object containing optional function callbacks to monitor the upload
123
+ * of data.
124
+ */
125
+ upload?:
126
+ | {
127
+ onabort?(response: Response<TContext>): void
128
+ onerror?(response: Response<TContext>): void
129
+ onload?(response: Response<TContext>): void
130
+ onprogress?(response: ProgressResponse<TContext>): void
131
+ }
132
+ | undefined
84
133
 
85
- /**
86
- * Gets a content of a resouce defined by {@link http://wiki.greasespot.net/Metadata_Block#.40resource|@resource}.
87
- * @param resourceName a name of the resource to get.
88
- * @returns the content of the resource.
89
- * @see {@link http://wiki.greasespot.net/GM_getResourceText}
90
- */
91
- declare function GM_getResourceText(resourceName: string): string
134
+ // Event handlers
135
+
136
+ /** Will be called when the request is aborted */
137
+ onabort?(response: Response<TContext>): void
138
+ /** Will be called if an error occurs while processing the request */
139
+ onerror?(response: Response<TContext>): void
140
+ /** Will be called when the request has completed successfully */
141
+ onload?(response: Response<TContext>): void
142
+ /** Will be called when the request progress changes */
143
+ onprogress?(response: ProgressResponse<TContext>): void
144
+ /** Will be called repeatedly while the request is in progress */
145
+ onreadystatechange?(response: Response<TContext>): void
146
+ /** Will be called if/when the request times out */
147
+ ontimeout?(response: Response<TContext>): void
148
+ }
149
+ }
92
150
 
93
151
  /**
94
- * Gets a URL of a resource defined by {@link http://wiki.greasespot.net/Metadata_Block#.40resource|@resource}.
95
- * @param resourceName a name of the resource.
96
- * @returns a URL that returns the content of the resource.
97
- * @see {@link http://wiki.greasespot.net/GM_getResourceURL}
152
+ * Window object of the content page where the user script is running on.
153
+ * @see {@link http://wiki.greasespot.net/UnsafeWindow}
98
154
  */
99
- declare function GM_getResourceURL(resourceName: string): string
155
+ declare var unsafeWindow: Window
100
156
 
101
- ////////////////
102
- // Utilities
103
- ////////////////
157
+ declare var GM: {
158
+ // Headers
104
159
 
105
- /**
106
- * Adds CSS to the content page.
107
- * @param css a CSS string. It can have multiple style definitions.
108
- * @see {@link http://wiki.greasespot.net/GM_addStyle}
109
- */
110
- declare function GM_addStyle(css: string): void
160
+ /**
161
+ * Meta data about the running user script.
162
+ * @see {@link https://wiki.greasespot.net/GM.info}
163
+ */
164
+ info: {
165
+ /** An object containing data about the currently running script */
166
+ script: GM.ScriptInfo
167
+ /**
168
+ * A string, the entire literal Metadata Block (without the delimiters)
169
+ * for the currently running script
170
+ */
171
+ scriptMetaStr: string
172
+ /**
173
+ * The name of the user script engine handling this script's execution.
174
+ * The string `Greasemonkey`
175
+ */
176
+ scriptHandler: string
177
+ /** The version of Greasemonkey, a string e.g. `4.0` */
178
+ version: string
179
+ }
111
180
 
112
- /**
113
- * Writes a message as a log to the console with the script identifier.
114
- * @param message a message to be written.
115
- * @see {@link http://wiki.greasespot.net/GM_log}
116
- */
117
- declare function GM_log(message: any): void
181
+ // Values
118
182
 
119
- /**
120
- * Opens a URL in a new tab.
121
- * @param url a URL to open.
122
- * @returns window object of the opened tab.
123
- * @see {@link http://wiki.greasespot.net/GM_openInTab}
124
- */
125
- declare function GM_openInTab(url: string): Window
183
+ /**
184
+ * Allows user script authors to persist simple values across page loads and
185
+ * across origins.
186
+ * Strings, booleans, and integers are currently the only allowed data types.
187
+ * @see {@link https://wiki.greasespot.net/GM.setValue}
188
+ * @param name The unique (within this script) name for this value.
189
+ * Should be restricted to valid Javascript identifier characters.
190
+ * @param value Any valid value of these types. Any other type may cause
191
+ * undefined behavior, including crashes
192
+ * @returns A Promise, resolved successfully with no value on success,
193
+ * rejected with no value on failure
194
+ */
195
+ setValue(name: string, value: GM.Value): Promise<void>
126
196
 
127
- /**
128
- * Registers an item as a submenu of User Script Commands.
129
- * @param caption a caption of the menu item.
130
- * @param commandFunc a function to be invoked when the item has been selected.
131
- * @param accessKey a single character that can be used to select the item by keyboard.
132
- * It should be a letter in the caption.
133
- * @see {@link http://wiki.greasespot.net/GM_registerMenuCommand}
134
- */
135
- declare function GM_registerMenuCommand(
136
- caption: string,
137
- commandFunc: Function,
138
- accessKey?: string
139
- ): void
197
+ /**
198
+ * Retrieves a value that was set with `GM.setValue`
199
+ * @see {@link https://wiki.greasespot.net/GM.getValue}
200
+ * @param name The property name to get
201
+ * @param defaultValue The default value to be returned when none has
202
+ * previously been set
203
+ * @returns A Promise, rejected in case of error and otherwise resolved with:
204
+ * - When this name has been set - `string`, `integer` or `boolean` as
205
+ * previously set
206
+ * - When this name has not been set, and default is provided - The value
207
+ * passed as default
208
+ * - When this name has not been set, and default is not provided -
209
+ * `undefined`
210
+ * @example
211
+ * // Retrieving the value associated with the name 'timezoneOffset' with a default value defined:
212
+ * const timezoneOffset = await GM.getValue("timezoneOffset", -5)
213
+ * @example
214
+ * // For structured data used `JSON.stringify()` to place an object into storage and then `JSON.parse()` to convert it back
215
+ * const storedObject = JSON.parse(await GM.getValue('foo', '{}'));
216
+ */
217
+ getValue(name: string): Promise<GM.Value | undefined>
218
+ getValue<TValue = GM.Value>(
219
+ name: string,
220
+ defaultValue: TValue
221
+ ): Promise<TValue>
140
222
 
141
- /**
142
- * Sets a text to the clipboard of the opeating system.
143
- * @param text a text to be set to the clipboard.
144
- * @see {@link http://wiki.greasespot.net/GM_setClipboard}
145
- */
146
- declare function GM_setClipboard(text: string): void
223
+ /**
224
+ * Deletes an existing name / value pair from storage.
225
+ * @see {@link https://wiki.greasespot.net/GM.deleteValue}
226
+ * @param name Property name to delete
227
+ * @returns A Promise, resolved successfully with no value on success,
228
+ * rejected with no value on failure.
229
+ */
230
+ deleteValue(name: string): Promise<void>
147
231
 
148
- ////////////////
149
- // XMLHttpRequest
150
- ////////////////
232
+ /**
233
+ * Retrieves an array of preference names that this script has stored
234
+ * @see {@link https://wiki.greasespot.net/GM.listValues}
235
+ * @returns A Promise, rejected in case of error and otherwise resolved with
236
+ * an string[] for previously set values
237
+ */
238
+ listValues(): Promise<string[]>
151
239
 
152
- /**
153
- * Request options for {@link GM_xmlhttpRequest}.
154
- * @see {@link http://wiki.greasespot.net/GM_xmlhttpRequest#Arguments}
155
- */
156
- interface GMXMLHttpRequestOptions {
157
- binary?: boolean | undefined
158
- context?: any
159
- data?: string | undefined
160
- headers?: Object | undefined
161
- method: string
162
- onabort?: ((response: GMXMLHttpRequestResponse) => any) | undefined
163
- onerror?: ((response: GMXMLHttpRequestResponse) => any) | undefined
164
- onload?: ((response: GMXMLHttpRequestResponse) => any) | undefined
165
- onprogress?: ((response: GMXMLHttpRequestProgressResponse) => any) | undefined
166
- onreadystatechange?: ((response: GMXMLHttpRequestResponse) => any) | undefined
167
- ontimeout?: ((response: GMXMLHttpRequestResponse) => any) | undefined
168
- overrideMimeType?: string | undefined
169
- password?: string | undefined
170
- synchronous?: boolean | undefined
171
- timeout?: number | undefined
172
- upload?:
173
- | {
174
- onabort?: ((response: GMXMLHttpRequestResponse) => any) | undefined
175
- onerror?: ((response: GMXMLHttpRequestResponse) => any) | undefined
176
- onload?: ((response: GMXMLHttpRequestResponse) => any) | undefined
177
- onprogress?:
178
- | ((response: GMXMLHttpRequestProgressResponse) => any)
179
- | undefined
180
- }
181
- | undefined
182
- url: string
183
- user?: string | undefined
184
- }
240
+ // Resources
185
241
 
186
- /**
187
- * Response object for general events of {@link GM_xmlhttpRequest}.
188
- * @see {@link http://wiki.greasespot.net/GM_xmlhttpRequest#Response_Object}
189
- */
190
- interface GMXMLHttpRequestResponse {
191
- readyState: number
192
- responseHeaders: string
193
- responseText: string
194
- status: number
195
- statusText: string
196
- context: any
197
- finalUrl: string
198
- }
242
+ /**
243
+ * Given a defined `@resource`, this method returns it as a URL
244
+ * @see {@link https://wiki.greasespot.net/GM.getResourceUrl}
245
+ * @param resourceName The name provided when the `@resource` was defined
246
+ * @returns A Promise, rejected on failure and resolved with a string URL on
247
+ * success.
248
+ * Treat the result as opaque string. It will work where you need a URL
249
+ * (for a `<link>` or `<style>` for CSS, for an `<img>` tag, or similar).
250
+ */
251
+ getResourceUrl(resourceName: string): Promise<string>
199
252
 
200
- /**
201
- * Response object for onprogress event of {@link GM_xmlhttpRequest}.
202
- */
203
- interface GMXMLHttpRequestProgressResponse extends GMXMLHttpRequestResponse {
204
- lengthComputable: boolean
205
- loaded: number
206
- total: number
207
- }
253
+ // Other
208
254
 
209
- /**
210
- * Returned object by {@link GM_xmlhttpRequest} in asynchronous mode.
211
- */
212
- interface GMXMLHttpRequestAsyncResult {
213
- abort(): void
214
- }
255
+ /**
256
+ * Displays a notification to the user, using the underlying operating
257
+ * system's notification mechanism
258
+ * @see {@link https://wiki.greasespot.net/GM.notification}
259
+ * @param text The main notification text
260
+ * @param title The title of the notification
261
+ * @param image The URL for an image to display in the dialog. If not
262
+ * provided, the Greasemonkey logo by default.
263
+ * @param onClick Callback, triggered when the notification's button is
264
+ * clicked.
265
+ */
266
+ notification(
267
+ text: string,
268
+ title: string,
269
+ image?: string,
270
+ onClick?: () => void
271
+ ): void
215
272
 
216
- /**
217
- * Returned object by {@link GM_xmlhttpRequest} in synchronouse mode.
218
- */
219
- interface GMXMLHttpRequestSyncResult {
220
- abort(): void
221
- finalUrl: string
222
- readyState: number
223
- responseHeaders: string
224
- responseText: string
225
- status: number
226
- statusText: string
227
- }
273
+ /**
274
+ * Opens the specified URL in a new tab.
275
+ * @see {@link https://wiki.greasespot.net/GM.openInTab}
276
+ * @param url The URL to navigate the new tab to
277
+ * @param openInBackground Force tab to/to not open in a background tab.
278
+ * Default (unspecified) behavior honors Firefox configuration.
279
+ */
280
+ openInTab(url: string, openInBackground?: boolean): void
228
281
 
229
- /**
230
- * Returned object by {@link GM_xmlhttpRequest}.
231
- * @see {@link http://wiki.greasespot.net/GM_xmlhttpRequest#Returns}
232
- */
233
- interface GMXMLHttpRequestResult
234
- extends GMXMLHttpRequestAsyncResult,
235
- GMXMLHttpRequestSyncResult {}
282
+ /**
283
+ * Adds an item to the User Script Commands menu.
284
+ * @param caption The caption to display on the menu item.
285
+ * @param commandFunc The function to call when the menu item is selected.
286
+ * @param accessKey A single character that can be used to select the
287
+ * item when the menu is open. It should be a letter in the caption.
288
+ * @see {@link https://wiki.greasespot.net/GM.registerMenuCommand}
289
+ */
290
+ registerMenuCommand(
291
+ caption: string,
292
+ commandFunc: () => void,
293
+ accessKey?: string
294
+ ): void
236
295
 
237
- /**
238
- * Sends a HTTP request to a URL.
239
- * @param options options and callbacks for HTTP request.
240
- * @returns an object which can abort the request.
241
- * If the request is sent in the synchronous mode, it also contains the response information.
242
- * @see {@link http://wiki.greasespot.net/GM_setClipboard}
243
- */
244
- declare function GM_xmlhttpRequest(
245
- options: GMXMLHttpRequestOptions
246
- ): GMXMLHttpRequestResult
296
+ /**
297
+ * Sets the current contents of the operating system's clipboard
298
+ * @see {@link https://wiki.greasespot.net/GM.setClipboard}
299
+ */
300
+ setClipboard(text: string): void
301
+
302
+ /**
303
+ * Performs a similar function to the standard XMLHttpRequest object, but
304
+ * allows these requests to cross the [same origin policy]{@link https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy} boundaries.
305
+ * @see {@link https://wiki.greasespot.net/GM.xmlHttpRequest}
306
+ */
307
+ xmlHttpRequest(details: GM.Request): void
308
+ }