vite-userscript-plugin 0.0.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.
- package/dist/banner.d.ts +2 -0
- package/dist/banner.js +30 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +3 -0
- package/dist/css.d.ts +11 -0
- package/dist/css.js +38 -0
- package/dist/helpers.d.ts +1 -0
- package/dist/helpers.js +3 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +91 -0
- package/dist/types.d.ts +195 -0
- package/dist/types.js +1 -0
- package/package.json +18 -0
package/dist/banner.d.ts
ADDED
package/dist/banner.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function banner(config) {
|
|
2
|
+
const metadata = [];
|
|
3
|
+
const configKeys = Object.keys(config);
|
|
4
|
+
const maxKeyLength = Math.max(...configKeys.map((key) => key.length)) + 1;
|
|
5
|
+
const addSpaces = (str) => {
|
|
6
|
+
return ' '.repeat(maxKeyLength - str.length);
|
|
7
|
+
};
|
|
8
|
+
const addMetadata = (key, value) => {
|
|
9
|
+
const isBoolean = typeof value === 'boolean';
|
|
10
|
+
if (isBoolean && !value)
|
|
11
|
+
return;
|
|
12
|
+
value = !isBoolean ? addSpaces(key) + value.toString() : '';
|
|
13
|
+
metadata.push(`// @${key}${value}`);
|
|
14
|
+
};
|
|
15
|
+
for (const [key, value] of Object.entries(config)) {
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
value.forEach((value) => addMetadata(key, value));
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
if (value === undefined)
|
|
21
|
+
continue;
|
|
22
|
+
addMetadata(key, value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return [
|
|
26
|
+
'// ==UserScript==',
|
|
27
|
+
...metadata,
|
|
28
|
+
'// ==/UserScript=='
|
|
29
|
+
].join('\n');
|
|
30
|
+
}
|
package/dist/css.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ESBuildTransformResult } from 'vite';
|
|
2
|
+
declare class CSS {
|
|
3
|
+
private styles;
|
|
4
|
+
add(entry: string, code: string, path: string): {
|
|
5
|
+
code: string;
|
|
6
|
+
} | null;
|
|
7
|
+
minify(css: string, path: string): Promise<ESBuildTransformResult>;
|
|
8
|
+
inject(): string;
|
|
9
|
+
}
|
|
10
|
+
declare const _default: CSS;
|
|
11
|
+
export default _default;
|
package/dist/css.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { transformWithEsbuild } from 'vite';
|
|
2
|
+
import { regexpStyles, template } from './constants.js';
|
|
3
|
+
class CSS {
|
|
4
|
+
constructor() {
|
|
5
|
+
Object.defineProperty(this, "styles", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true,
|
|
9
|
+
value: []
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
add(entry, code, path) {
|
|
13
|
+
if (regexpStyles.test(path)) {
|
|
14
|
+
this.styles.push(code);
|
|
15
|
+
return {
|
|
16
|
+
code: ''
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
if (path.includes(entry)) {
|
|
20
|
+
return {
|
|
21
|
+
code: code + template
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
async minify(css, path) {
|
|
27
|
+
return await transformWithEsbuild(css, path, {
|
|
28
|
+
minify: true,
|
|
29
|
+
loader: 'css'
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
inject() {
|
|
33
|
+
const css = `GM_addStyle(\`${this.styles.join('')}\`)`;
|
|
34
|
+
this.styles.length = 0;
|
|
35
|
+
return css;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export default new CSS();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function removeDuplicates(arr: string | string[] | undefined): any[];
|
package/dist/helpers.js
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { transformWithEsbuild } from 'vite';
|
|
4
|
+
import { banner } from './banner.js';
|
|
5
|
+
import { regexpScripts, template } from './constants.js';
|
|
6
|
+
import css from './css.js';
|
|
7
|
+
import { removeDuplicates } from './helpers.js';
|
|
8
|
+
function UserscriptPlugin(config) {
|
|
9
|
+
let pluginConfig;
|
|
10
|
+
return {
|
|
11
|
+
name: 'vite-userscript-plugin',
|
|
12
|
+
apply: 'build',
|
|
13
|
+
config() {
|
|
14
|
+
return {
|
|
15
|
+
build: {
|
|
16
|
+
lib: {
|
|
17
|
+
entry: config.entry,
|
|
18
|
+
name: config.metadata.name,
|
|
19
|
+
formats: ['iife'],
|
|
20
|
+
fileName: () => `${config.metadata.name}.js`
|
|
21
|
+
},
|
|
22
|
+
rollupOptions: {
|
|
23
|
+
output: {
|
|
24
|
+
extend: true
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
configResolved(cfg) {
|
|
31
|
+
const { match, require, include, exclude, resource, connect, grant } = config.metadata;
|
|
32
|
+
config.metadata.match = removeDuplicates(match);
|
|
33
|
+
config.metadata.require = removeDuplicates(require);
|
|
34
|
+
config.metadata.include = removeDuplicates(include);
|
|
35
|
+
config.metadata.exclude = removeDuplicates(exclude);
|
|
36
|
+
config.metadata.resource = removeDuplicates(resource);
|
|
37
|
+
config.metadata.connect = removeDuplicates(connect);
|
|
38
|
+
config.metadata.grant = removeDuplicates([
|
|
39
|
+
...(grant ?? []),
|
|
40
|
+
'GM_addStyle',
|
|
41
|
+
'GM_info'
|
|
42
|
+
]);
|
|
43
|
+
pluginConfig = cfg;
|
|
44
|
+
},
|
|
45
|
+
async transform(code, path) {
|
|
46
|
+
const transformed = await css.minify(code, path);
|
|
47
|
+
return css.add(config.entry, transformed.code.replace('\n', ''), path);
|
|
48
|
+
},
|
|
49
|
+
async writeBundle(options, bundle) {
|
|
50
|
+
for (const [fileName] of Object.entries(bundle)) {
|
|
51
|
+
if (regexpScripts.test(fileName)) {
|
|
52
|
+
const rootDir = pluginConfig.root;
|
|
53
|
+
const outDir = pluginConfig.build.outDir;
|
|
54
|
+
const filePath = resolve(rootDir, outDir, fileName);
|
|
55
|
+
const proxyFilePath = resolve(rootDir, outDir, `${config.metadata.name}.proxy.user.js`);
|
|
56
|
+
const userFileName = resolve(rootDir, outDir, `${config.metadata.name}.user.js`);
|
|
57
|
+
try {
|
|
58
|
+
let file = readFileSync(filePath, {
|
|
59
|
+
encoding: 'utf8'
|
|
60
|
+
});
|
|
61
|
+
file = file.replace(template, `
|
|
62
|
+
${css.inject()}
|
|
63
|
+
const { script } = GM_info
|
|
64
|
+
console.group(script.name + ' / ' + script.version)
|
|
65
|
+
console.log(GM_info)
|
|
66
|
+
console.groupEnd()
|
|
67
|
+
`);
|
|
68
|
+
const { code } = await transformWithEsbuild(file, fileName, {
|
|
69
|
+
loader: 'js',
|
|
70
|
+
minify: true
|
|
71
|
+
});
|
|
72
|
+
// source
|
|
73
|
+
writeFileSync(filePath, code);
|
|
74
|
+
// production
|
|
75
|
+
writeFileSync(userFileName, `${banner(config.metadata)}\n\n${code}`);
|
|
76
|
+
// development
|
|
77
|
+
writeFileSync(proxyFilePath, banner({
|
|
78
|
+
...config.metadata,
|
|
79
|
+
require: [...config.metadata.require, 'file://' + filePath]
|
|
80
|
+
}));
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
console.log(err);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export { UserscriptPlugin };
|
|
91
|
+
export default UserscriptPlugin;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
export declare type RunAt = 'document-start' | 'document-body' | 'document-end' | 'document-idle' | 'context-menu';
|
|
2
|
+
export declare type Grants = 'unsafeWindow' | 'window.onurlchange' | 'window.focus' | 'window.close' | 'GM_setValue' | 'GM_getValue' | 'GM_deleteValue' | 'GM_listValues' | 'GM_setClipboard' | 'GM_addStyle' | 'GM_addElement' | 'GM_addValueChangeListener' | 'GM_removeValueChangeListener' | 'GM_registerMenuCommand' | 'GM_unregisterMenuCommand' | 'GM_download' | 'GM_getTab' | 'GM_getTabs' | 'GM_saveTab' | 'GM_openInTab' | 'GM_notification' | 'GM_getResourceURL' | 'GM_getResourceText' | 'GM_xmlhttpRequest' | 'GM_webRequest' | 'GM_log' | 'GM_info';
|
|
3
|
+
export declare type MetadataConfig = {
|
|
4
|
+
[property: string]: string | boolean | number | string[] | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* The name of the script.
|
|
7
|
+
* Internationalization is done by adding an appendix naming the locale.
|
|
8
|
+
*/
|
|
9
|
+
name: string;
|
|
10
|
+
/**
|
|
11
|
+
* Version of the script,
|
|
12
|
+
* it can be used to check if a script has new versions. It is composed
|
|
13
|
+
* of several parts, joined by `.` Each part must start with numbers,
|
|
14
|
+
* and can be followed by alphabetic characters.
|
|
15
|
+
*/
|
|
16
|
+
version: string;
|
|
17
|
+
/**
|
|
18
|
+
* A brief summary to describe the script.
|
|
19
|
+
*/
|
|
20
|
+
description?: string;
|
|
21
|
+
/**
|
|
22
|
+
* The scripts author.
|
|
23
|
+
*/
|
|
24
|
+
author?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The script license.
|
|
27
|
+
*/
|
|
28
|
+
license?: string;
|
|
29
|
+
/**
|
|
30
|
+
* The combination of `@namespace` and `@name` is the unique identifier for
|
|
31
|
+
* a userscript. `@namespace` can be any string, for example the homepage
|
|
32
|
+
* of a group of userscripts by the same author. If not provided
|
|
33
|
+
* the `@namespace` falls back to an empty string ('').
|
|
34
|
+
*/
|
|
35
|
+
namespace?: string;
|
|
36
|
+
/**
|
|
37
|
+
* The authors homepage that is used at the options page to link from
|
|
38
|
+
* the scripts name to the given page. Please note that if the `@namespace`
|
|
39
|
+
* tag starts with `https://` its content will be used for this too.
|
|
40
|
+
*/
|
|
41
|
+
homepage?: string;
|
|
42
|
+
/**
|
|
43
|
+
* An update URL for the userscript.
|
|
44
|
+
* Note:
|
|
45
|
+
* - a `@version` tag is required to make update checks work.
|
|
46
|
+
*/
|
|
47
|
+
updateURL?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Defines the URL where the script will be downloaded from when an update was
|
|
50
|
+
* detected. If the value none is used, then no update check will be done.
|
|
51
|
+
*/
|
|
52
|
+
downloadURL?: string;
|
|
53
|
+
/**
|
|
54
|
+
* Defines the URL where the user can report issues and get personal support.
|
|
55
|
+
*/
|
|
56
|
+
supportURL?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Specify an icon for the script. Almost any image will work,
|
|
59
|
+
* but a 32x32 pixel size is best. This value may be specified relative
|
|
60
|
+
* to the URL the script itself is downloaded from.
|
|
61
|
+
*/
|
|
62
|
+
icon?: string;
|
|
63
|
+
icon64?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Each `@include` and `@exclude` rule can be one of the following:
|
|
66
|
+
* - a normal string
|
|
67
|
+
* > If the string does not start or end with a slash (/),
|
|
68
|
+
* > it will be used as a normal string.
|
|
69
|
+
* > If there are wildcards (*), each of them matches any characters.
|
|
70
|
+
* > e.g. https://www.google.com/* matches the following:
|
|
71
|
+
* - https://www.google.com/
|
|
72
|
+
* - https://www.google.com/any/subview
|
|
73
|
+
* > but not the following:
|
|
74
|
+
* - http://www.google.com/
|
|
75
|
+
* - https://www.google.com.hk/
|
|
76
|
+
* > If there is no wildcard in the string, the rule matches the entire URL.
|
|
77
|
+
* > e.g. https://www.google.com/ matches only https://www.google.com/
|
|
78
|
+
* > but not https://www.google.com/any/subview.
|
|
79
|
+
* > The host part accepts .tld to match top level domain suffix.
|
|
80
|
+
* > e.g. https://www.google.tld/ matches both https://www.google.com/
|
|
81
|
+
* > and https://www.google.co.jp/.
|
|
82
|
+
*
|
|
83
|
+
* - a regular expression
|
|
84
|
+
* > If the string starts and ends with a slash (/),
|
|
85
|
+
* > it will be compiled as a regular expression.
|
|
86
|
+
* > e.g. /\.google\.com[\.\/]/ matches the following:
|
|
87
|
+
* - https://www.google.com/,
|
|
88
|
+
* - https://www.google.com/any/subview
|
|
89
|
+
* - http://www.google.com/
|
|
90
|
+
* - https://www.google.com.hk/
|
|
91
|
+
*/
|
|
92
|
+
include?: string[] | string;
|
|
93
|
+
exclude?: string[] | string;
|
|
94
|
+
/**
|
|
95
|
+
* More or less equal to the `@include` tag.
|
|
96
|
+
* You can get more information
|
|
97
|
+
* [here](https://developer.chrome.com/docs/extensions/mv2/match_patterns/).
|
|
98
|
+
*
|
|
99
|
+
* Note:
|
|
100
|
+
* - The `<all_urls>` statement is not yet supported and the scheme part also
|
|
101
|
+
* accepts `http*://`.
|
|
102
|
+
*/
|
|
103
|
+
match: string[] | string;
|
|
104
|
+
/**
|
|
105
|
+
* Points to a JavaScript file that is loaded and executed before the script
|
|
106
|
+
* itself starts running.
|
|
107
|
+
*
|
|
108
|
+
* Note:
|
|
109
|
+
* - The scripts loaded via `@require` and their "use strict" statements
|
|
110
|
+
* might influence the userscript's strict mode!
|
|
111
|
+
*/
|
|
112
|
+
require?: string[] | string;
|
|
113
|
+
/**
|
|
114
|
+
* Preloads resources that can by accessed
|
|
115
|
+
* via `GM_getResourceURL` and `GM_getResourceText` by the script.
|
|
116
|
+
*/
|
|
117
|
+
resource?: string[] | string;
|
|
118
|
+
/**
|
|
119
|
+
* This tag defines the domains (no top-level domains) including subdomains
|
|
120
|
+
* which are allowed to be retrieved by `GM_xmlhttpRequest`
|
|
121
|
+
*
|
|
122
|
+
* - domains like tampermonkey.net (this will also allow all sub-domains)
|
|
123
|
+
* - sub-domains i.e. safari.tampermonkey.net
|
|
124
|
+
* - self to whitelist the domain the script is currently running at
|
|
125
|
+
* - localhost to access the localhost
|
|
126
|
+
* - 1.2.3.4 to connect to an IP address
|
|
127
|
+
*
|
|
128
|
+
* If it's not possible to declare all domains a userscript might connect
|
|
129
|
+
* to then it's a good practice to do the following: ***Declare all known or
|
|
130
|
+
* at least all common domains*** that might be connected by the script.
|
|
131
|
+
* This way the confirmation dialog can be avoided for most of the users.
|
|
132
|
+
*
|
|
133
|
+
* Additionally add `@connect *` to the script. By doing so Tampermonkey will
|
|
134
|
+
* still ask the user whether the next connection to a not mentioned domain
|
|
135
|
+
* is allowed, but also ***offer a "Always allow all domains" button***.
|
|
136
|
+
* If the user clicks at this button then all future requestswill
|
|
137
|
+
* be permitted automatically.
|
|
138
|
+
*/
|
|
139
|
+
connect?: string[] | string;
|
|
140
|
+
/**
|
|
141
|
+
* This tag makes the script running on the main pages, but not at iframes.
|
|
142
|
+
* @default false
|
|
143
|
+
*/
|
|
144
|
+
noframes?: boolean;
|
|
145
|
+
/**
|
|
146
|
+
* `@grant` is used to whitelist GM_* functions, the `unsafeWindow` object and
|
|
147
|
+
* some powerful window functions. If no `@grant` tag is given TM guesses
|
|
148
|
+
* the scripts needs.
|
|
149
|
+
*/
|
|
150
|
+
grant?: Exclude<Grants, 'GM_addStyle' | 'GM_info'>[];
|
|
151
|
+
/**
|
|
152
|
+
* Defines the moment the script is injected. In opposition to other script
|
|
153
|
+
* handlers, `@run-at` defines the first possible moment a script wants to
|
|
154
|
+
* run. This means it may happen, that a script that uses the `@require` tag
|
|
155
|
+
* may be executed after the document is already loaded, cause fetching the
|
|
156
|
+
* required script took that long. Anyhow, all `DOMNodeInserted` and
|
|
157
|
+
* `DOMContentLoaded` events that happended after the given injection
|
|
158
|
+
* moment are cached and delivered to the script when it is injected.
|
|
159
|
+
*
|
|
160
|
+
* - `document-start`
|
|
161
|
+
* The script will be injected as fast as possible.
|
|
162
|
+
*
|
|
163
|
+
* - `document-body`
|
|
164
|
+
* The script will be injected if the body element exists.
|
|
165
|
+
*
|
|
166
|
+
* - `document-end`
|
|
167
|
+
* The script will be injected when or after the `DOMContentLoaded` event
|
|
168
|
+
* was dispatched.
|
|
169
|
+
*
|
|
170
|
+
* - `document-idle`
|
|
171
|
+
* The script will be injected after the DOMContentLoaded event was
|
|
172
|
+
* dispatched. This is the default value if no `@run-at` tag is given.
|
|
173
|
+
*
|
|
174
|
+
* - `context-menu`
|
|
175
|
+
* The script will be injected if it is clicked at the browser context menu
|
|
176
|
+
* (desktop Chrome-based browsers only).
|
|
177
|
+
*
|
|
178
|
+
* Note:
|
|
179
|
+
* - all `@include` and `@exclude` statements will be ignored if this value
|
|
180
|
+
* is used, but this may change in the future.
|
|
181
|
+
*
|
|
182
|
+
* @default 'document-idle'
|
|
183
|
+
*/
|
|
184
|
+
'run-at'?: RunAt;
|
|
185
|
+
};
|
|
186
|
+
export interface PluginConfig {
|
|
187
|
+
/**
|
|
188
|
+
* Path of userscript entry.
|
|
189
|
+
*/
|
|
190
|
+
entry: string;
|
|
191
|
+
/**
|
|
192
|
+
* Userscript Metadata config.
|
|
193
|
+
*/
|
|
194
|
+
metadata: MetadataConfig;
|
|
195
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-userscript-plugin",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@types/tampermonkey": "^4.0.5"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"dev": "tsc --build --watch",
|
|
15
|
+
"build": "pnpm clean && tsc --build",
|
|
16
|
+
"clean": "del-cli dist"
|
|
17
|
+
}
|
|
18
|
+
}
|