vite-userscript-plugin 1.3.0 → 1.5.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.
@@ -0,0 +1,376 @@
1
+ declare const unsafeWindow: Window
2
+
3
+ declare type VMScriptRunAt =
4
+ | 'document-start'
5
+ | 'document-body'
6
+ | 'document-end'
7
+ | 'document-idle'
8
+
9
+ /** Injection mode of a script. */
10
+ declare type VMScriptInjectInto = 'auto' | 'page' | 'content'
11
+
12
+ declare interface VMScriptGMInfoPlatform {
13
+ arch: 'arm' | 'arm64' | 'x86-32' | 'x86-64' | 'mips' | 'mips64'
14
+ /** `chrome`, `firefox` or whatever was returned by the API. */
15
+ browserName: string
16
+ browserVersion: string
17
+ os: 'mac' | 'win' | 'android' | 'cros' | 'linux' | 'openbsd' | 'fuchsia'
18
+ }
19
+
20
+ declare interface VMScriptGMInfoScriptMeta {
21
+ description: string
22
+ excludes: string[]
23
+ includes: string[]
24
+ matches: string[]
25
+ name: string
26
+ namespace: string
27
+ resources: Array<{ name: string; url: string }>
28
+ runAt: VMScriptRunAt
29
+ version: string
30
+ }
31
+
32
+ declare interface VMScriptGMInfoObject {
33
+ /** Unique ID of the script. */
34
+ uuid: string
35
+ /** The meta block of the script. */
36
+ scriptMetaStr: string
37
+ /** Whether the script will be updated automatically. */
38
+ scriptWillUpdate: boolean
39
+ /** The name of userscript manager, which should be the string `Violentmonkey`. */
40
+ scriptHandler: string
41
+ /** Version of Violentmonkey. */
42
+ version: string
43
+ /**
44
+ * Unlike `navigator.userAgent`, which can be overriden by other extensions/userscripts
45
+ * or by devtools in device-emulation mode, `GM_info.platform` is more reliable as the
46
+ * data is obtained in the background page of Violentmonkey using a specialized
47
+ * extension API (`browser.runtime.getPlatformInfo` and `getBrowserInfo`).
48
+ */
49
+ platform: VMScriptGMInfoPlatform
50
+ /** Contains structured fields from the *Metadata Block*. */
51
+ script: VMScriptGMInfoScriptMeta
52
+ /** The injection mode of current script. */
53
+ injectInto: VMScriptInjectInto
54
+ }
55
+
56
+ /**
57
+ * An object that exposes information about the current userscript.
58
+ */
59
+ declare const GM_info: VMScriptGMInfoObject
60
+
61
+ /** Retrieves a value for current script from storage. */
62
+ declare function GM_getValue<T>(name: string, defaultValue?: T): T
63
+ /** Sets a key / value pair for current script to storage. */
64
+ declare function GM_setValue<T>(name: string, value: T): void
65
+ /** Deletes an existing key / value pair for current script from storage. */
66
+ declare function GM_deleteValue(name: string): void
67
+ /** Returns an array of keys of all available values within this script. */
68
+ declare function GM_listValues(): string[]
69
+
70
+ declare type VMScriptGMValueChangeCallback<T> = (
71
+ /** The name of the observed variable */
72
+ name: string,
73
+ /** The old value of the observed variable (`undefined` if it was created) */
74
+ oldValue: T,
75
+ /** The new value of the observed variable (`undefined` if it was deleted) */
76
+ newValue: T,
77
+ /** `true` if modified by the userscript instance of another tab or `false` for this script instance. Can be used by scripts of different browser tabs to communicate with each other. */
78
+ remote: boolean
79
+ ) => void
80
+
81
+ /** Adds a change listener to the storage and returns the listener ID. */
82
+ declare function GM_addValueChangeListener<T>(
83
+ name: string,
84
+ callback: VMScriptGMValueChangeCallback<T>
85
+ ): string
86
+ /** Removes a change listener by its ID. */
87
+ declare function GM_removeValueChangeListener(listenerId: string): void
88
+
89
+ /** Retrieves a text resource from the *Metadata Block*. */
90
+ declare function GM_getResourceText(
91
+ /** Name of a resource defined in the *Metadata Block*. */
92
+ name: string
93
+ ): string
94
+ /**
95
+ * Retrieves a `blob:` or `data:` URL of a resource from the *Metadata Block*.
96
+ *
97
+ * Note: when setting this URL as `src` or `href` of a DOM element, it may fail on some sites with a particularly strict CSP that forbids `blob:` or `data:` URLs. Such sites are rare though. The workaround in Chrome is to use `GM_addElement`, whereas in Firefox you'll have to disable CSP either globally via `about:config` or by using an additional extension that modifies HTTP headers selectively.
98
+ */
99
+ declare function GM_getResourceURL(
100
+ /** Name of a resource defined in the *Metadata Block*. */
101
+ name: string,
102
+ /**
103
+ * - If `true`, returns a `blob:` URL. It's short and cacheable, so it's good for reusing in multiple DOM elements.
104
+ * - If `false`, returns a `data:` URL. It's long so reusing it in DOM may be less performant due to the lack of caching, but it's particularly handy for direct synchronous decoding of the data on sites that forbid fetching `blob:` in their CSP.
105
+ */
106
+ isBlobUrl?: boolean
107
+ ): string
108
+
109
+ /**
110
+ * Appends and returns an element with the specified attributes.
111
+ *
112
+ * Examples:
113
+ *
114
+ * ```js
115
+ * // using a private function in `onload`
116
+ * let el = GM_addElement('script', { src: 'https://....' });
117
+ * el.onload = () => console.log('loaded', el);
118
+ *
119
+ * // same as GM_addStyle('a { color:red }')
120
+ * let el = GM_addElement('style', { textContent: 'a { color:red }' });
121
+ *
122
+ * // appending to an arbitrary node
123
+ * let el = GM_addElement(parentElement.shadowRoot, 'iframe', { src: url });
124
+ * ```
125
+ */
126
+ declare function GM_addElement(
127
+ /** A tag name like `script`. Any valid HTML tag can be used, but the only motivation for this API was to add `script`, `link`, `style` elements when they are disallowed by a strict `Content-Security-Policy` of the site e.g. github.com, twitter.com. */
128
+ tagName: string,
129
+ /** The keys are HTML attributes, not DOM properties, except `textContent` which sets DOM property `textContent`. The values are strings so if you want to assign a private function to `onload` you can do it after the element is created. */
130
+ attributes?: Record<string, string>
131
+ ): HTMLElement
132
+ declare function GM_addElement(
133
+ /**
134
+ * The parent node to which the new node will be appended.
135
+ * It can be inside ShadowDOM: `someElement.shadowRoot`.
136
+ * When omitted, it'll be determined automatically:
137
+ *
138
+ * - `document.head` (`<head>`) for `script`, `link`, `style`, `meta` tags.
139
+ * - `document.body` (`<body>`) for other tags or when there's no `<head>`.
140
+ * - `document.documentElement` (`<html>` or an XML root node) otherwise.
141
+ */
142
+ parentNode: HTMLElement,
143
+ /** A tag name like `script`. Any valid HTML tag can be used, but the only motivation for this API was to add `script`, `link`, `style` elements when they are disallowed by a strict `Content-Security-Policy` of the site e.g. github.com, twitter.com. */
144
+ tagName: string,
145
+ /** The keys are HTML attributes, not DOM properties, except `textContent` which sets DOM property `textContent`. The values are strings so if you want to assign a private function to `onload` you can do it after the element is created. */
146
+ attributes?: Record<string, string>
147
+ ): HTMLElement
148
+
149
+ /** Appends and returns a `<style>` element with the specified CSS. */
150
+ declare function GM_addStyle(css: string): HTMLStyleElement
151
+
152
+ declare interface VMScriptGMTabControl {
153
+ /** Сan be assigned to a function. If provided, it will be called when the opened tab is closed. */
154
+ onclose?: () => void
155
+ /** Whether the opened tab is closed. */
156
+ closed: boolean
157
+ /** A function to explicitly close the opened tab. */
158
+ close: () => void
159
+ }
160
+
161
+ declare interface VMScriptGMTabOptions {
162
+ /** Make the new tab active (i.e. open in foreground). Default as `true`. */
163
+ active?: boolean
164
+ /**
165
+ * Firefox only.
166
+ *
167
+ * - not specified = reuse script's tab container
168
+ * - `0` = default (main) container
169
+ * - `1`, `2`, etc. = internal container index
170
+ */
171
+ container?: number
172
+ /** Insert the new tab next to the current tab and set its `openerTab` so when it's closed the original tab will be focused automatically. When `false` or not specified, the usual browser behavior is to open the tab at the end of the tab list. Default as `true`. */
173
+ insert?: boolean
174
+ /** Pin the tab (i.e. show without a title at the beginning of the tab list). Default as `false`. */
175
+ pinned?: boolean
176
+ }
177
+
178
+ /** Opens URL in a new tab. */
179
+ declare function GM_openInTab(
180
+ /** The URL to open in a new tab. URL relative to current page is also allowed. Note: Firefox does not support data URLs. */
181
+ url: string,
182
+ options?: VMScriptGMTabOptions
183
+ ): VMScriptGMTabControl
184
+ declare function GM_openInTab(
185
+ /** The URL to open in a new tab. URL relative to current page is also allowed. Note: Firefox does not support data URLs. */
186
+ url: string,
187
+ /** Open the tab in background. Note, this is a reverse of the first usage method so for example `true` is the same as `{ active: false }`. */
188
+ openInBackground?: boolean
189
+ ): VMScriptGMTabControl
190
+
191
+ /**
192
+ * Registers a command in Violentmonkey popup menu.
193
+ * If you want to add a shortcut, please see `@violentmonkey/shortcut`.
194
+ */
195
+ declare function GM_registerMenuCommand(
196
+ /** The name to show in the popup menu. */
197
+ caption: string,
198
+ /** Callback function when the command is clicked in the menu. */
199
+ onClick: (event: MouseEvent) => void
200
+ ): string
201
+ /** Unregisters a command which has been registered to Violentmonkey popup menu. */
202
+ declare function GM_unregisterMenuCommand(
203
+ /** The name of command to unregister. */
204
+ caption: string
205
+ ): void
206
+
207
+ /**
208
+ * A control object returned by `GM_notification`.
209
+ * `control.remove()` can be used to remove the notification.
210
+ */
211
+ declare interface VMScriptGMNotificationControl {
212
+ /** Remove the notification immediately. */
213
+ remove: () => Promise<void>
214
+ }
215
+
216
+ declare interface VMScriptGMNotificationOptions {
217
+ /** Main text of the notification. */
218
+ text: string
219
+ /** Title of the notification. */
220
+ title?: string
221
+ /** URL of an image to show in the notification. */
222
+ image?: string
223
+ /** Callback when the notification is clicked by user. */
224
+ onclick?: () => void
225
+ /** Callback when the notification is closed, either by user or by system. */
226
+ ondone?: () => void
227
+ }
228
+
229
+ /** Shows an HTML5 desktop notification. */
230
+ declare function GM_notification(
231
+ options: VMScriptGMNotificationOptions
232
+ ): VMScriptGMNotificationControl
233
+ declare function GM_notification(
234
+ /** Main text of the notification. */
235
+ text: string,
236
+ /** Title of the notification. */
237
+ title?: string,
238
+ /** URL of an image to show in the notification. */
239
+ image?: string,
240
+ /** Callback when the notification is clicked by user. */
241
+ onclick?: () => void
242
+ ): VMScriptGMNotificationControl
243
+
244
+ /** Sets data to system clipboard. */
245
+ declare function GM_setClipboard(
246
+ /** The data to be copied to system clipboard. */
247
+ data: string,
248
+ /** The MIME type of data to copy. Default as `text/plain`. */
249
+ type?: string
250
+ ): void
251
+
252
+ /**
253
+ * A control object returned by `GM_xmlhttpRequest`.
254
+ * `control.abort()` can be used to abort the request.
255
+ */
256
+ declare interface VMScriptXHRControl {
257
+ abort: () => void
258
+ }
259
+
260
+ declare type VMScriptResponseType =
261
+ | 'text'
262
+ | 'json'
263
+ | 'blob'
264
+ | 'arraybuffer'
265
+ | 'document'
266
+
267
+ declare interface VMScriptResponseObject<T> {
268
+ status: number
269
+ statusText: string
270
+ readyState: number
271
+ responseHeaders: string
272
+ response: T
273
+ responseText: string | null
274
+ /** The final URL after redirection. */
275
+ finalUrl: string
276
+ /** The same `context` object you specified in `details`. */
277
+ context?: unknown
278
+ }
279
+
280
+ declare interface VMScriptGMXHRDetails<T> {
281
+ /** URL relative to current page is also allowed. */
282
+ url: string
283
+ /** HTTP method, default as `GET`. */
284
+ method?: string
285
+ /** User for authentication. */
286
+ user?: string
287
+ /** Password for authentication. */
288
+ password?: string
289
+ /** A MIME type to specify with the request. */
290
+ overrideMimeType?: string
291
+ /**
292
+ * Some special headers are also allowed:
293
+ *
294
+ * - `Cookie`
295
+ * - `Host`
296
+ * - `Origin`
297
+ * - `Referer`
298
+ * - `User-Agent`
299
+ */
300
+ headers?: Record<string, string>
301
+ /**
302
+ * One of the following:
303
+ *
304
+ * - `text` (default value)
305
+ * - `json`
306
+ * - `blob`
307
+ * - `arraybuffer`
308
+ * - `document`
309
+ */
310
+ responseType?: VMScriptResponseType
311
+ /** Time to wait for the request, none by default. */
312
+ timeout?: number
313
+ /** Data to send with the request, usually for `POST` and `PUT` requests. */
314
+ data?: string | FormData | Blob
315
+ /** Send the `data` string as a `blob`. This is for compatibility with Tampermonkey/Greasemonkey, where only `string` type is allowed in `data`. */
316
+ binary?: boolean
317
+ /** Can be an object and will be assigned to context of the response object. */
318
+ context?: unknown
319
+ /** When set to `true`, no cookie will be sent with the request and the response cookies will be ignored. The default value is `false`. */
320
+ anonymous?: boolean
321
+ onabort?: (resp: VMScriptResponseObject<T>) => void
322
+ onerror?: (resp: VMScriptResponseObject<T>) => void
323
+ onload?: (resp: VMScriptResponseObject<T>) => void
324
+ onloadend?: (resp: VMScriptResponseObject<T>) => void
325
+ onloadstart?: (resp: VMScriptResponseObject<T>) => void
326
+ onprogress?: (resp: VMScriptResponseObject<T>) => void
327
+ onreadystatechange?: (resp: VMScriptResponseObject<T>) => void
328
+ ontimeout?: (resp: VMScriptResponseObject<T>) => void
329
+ }
330
+
331
+ /** Makes a request like XMLHttpRequest, with some special capabilities, not restricted by same-origin policy. */
332
+ declare function GM_xmlhttpRequest<T>(
333
+ details: VMScriptGMXHRDetails<T>
334
+ ): VMScriptXHRControl
335
+
336
+ declare interface VMScriptGMDownloadOptions {
337
+ /** The URL to download. */
338
+ url: string
339
+ /** The filename to save as. */
340
+ name?: string
341
+ /** The function to call when download starts successfully. */
342
+ onload?: () => void
343
+ headers?: Record<string, string>
344
+ timeout?: number
345
+ onerror?: (resp: VMScriptResponseObject<Blob>) => void
346
+ onprogress?: (resp: VMScriptResponseObject<Blob>) => void
347
+ ontimeout?: (resp: VMScriptResponseObject<Blob>) => void
348
+ }
349
+
350
+ /** Downloads a URL to a local file. */
351
+ declare function GM_download(options: VMScriptGMDownloadOptions): void
352
+ declare function GM_download(
353
+ /** The URL to download. */
354
+ url: string,
355
+ /** The filename to save as. */
356
+ name?: string
357
+ ): void
358
+
359
+ declare interface VMScriptGMObject {
360
+ unsafeWindow: Window
361
+ info: typeof GM_info
362
+ getValue: <T>(name: string, defaultValue?: T) => Promise<T>
363
+ setValue: <T>(name: string, value: T) => Promise<void>
364
+ deleteValue: (name: string) => Promise<void>
365
+ listValues: () => Promise<string[]>
366
+ addStyle: typeof GM_addStyle
367
+ addElement: typeof GM_addElement
368
+ registerMenuCommand: typeof GM_registerMenuCommand
369
+ getResourceUrl: (name: string, isBlobUrl?: boolean) => Promise<string>
370
+ notification: typeof GM_notification
371
+ openInTab: typeof GM_openInTab
372
+ setClipboard: typeof GM_setClipboard
373
+ xmlHttpRequest: typeof GM_xmlhttpRequest
374
+ }
375
+
376
+ declare const GM: VMScriptGMObject