Skip to content

Commit c30c7ca

Browse files
authored
Merge pull request #14 from crashmax-dev/gm-promisify
feat: GM dot alias, GM promised
2 parents 5a2841c + 0072ce4 commit c30c7ca

File tree

7 files changed

+93
-50
lines changed

7 files changed

+93
-50
lines changed

examples/basic/vite.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export default defineConfig({
1010
name,
1111
version,
1212
match: [
13-
'https://example.com',
14-
'https://example.org',
15-
'https://example.edu'
13+
'*://example.com',
14+
'*://example.org',
15+
'*://example.edu'
1616
]
1717
}
1818
})

examples/jsx/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default defineConfig({
1111
header: {
1212
name,
1313
version,
14-
match: 'https://example.com'
14+
match: '*://example.com'
1515
}
1616
})
1717
]

src/constants.ts

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
1+
import type { Grants } from './types.js'
2+
13
export const regexpScripts = new RegExp(/.(t|j)sx?/)
24
export const regexpStyles = new RegExp(/\.(s?css|sass)$/)
35

4-
export const grants = [
6+
export const GM = [
7+
'setValue',
8+
'getValue',
9+
'deleteValue',
10+
'listValues',
11+
'setClipboard',
12+
'addStyle',
13+
'addElement',
14+
'addValueChangeListener',
15+
'removeValueChangeListener',
16+
'registerMenuCommand',
17+
'unregisterMenuCommand',
18+
'download',
19+
'getTab',
20+
'getTabs',
21+
'saveTab',
22+
'openInTab',
23+
'notification',
24+
'getResourceURL',
25+
'getResourceText',
26+
'xmlhttpRequest',
27+
'log',
28+
'info'
29+
] as const
30+
31+
export const GMwindow = [
532
'unsafeWindow',
633
'window.onurlchange',
734
'window.focus',
8-
'window.close',
9-
'GM_setValue',
10-
'GM_getValue',
11-
'GM_deleteValue',
12-
'GM_listValues',
13-
'GM_setClipboard',
14-
'GM_addStyle',
15-
'GM_addElement',
16-
'GM_addValueChangeListener',
17-
'GM_removeValueChangeListener',
18-
'GM_registerMenuCommand',
19-
'GM_unregisterMenuCommand',
20-
'GM_download',
21-
'GM_getTab',
22-
'GM_getTabs',
23-
'GM_saveTab',
24-
'GM_openInTab',
25-
'GM_notification',
26-
'GM_getResourceURL',
27-
'GM_getResourceText',
28-
'GM_xmlhttpRequest',
29-
'GM_webRequest',
30-
'GM_log',
31-
'GM_info'
35+
'window.close'
3236
] as const
37+
38+
export const grants = GM.map<Grants[]>((grant) => [
39+
`GM_${grant}`,
40+
`GM.${grant}`
41+
]).flat()
42+
43+
grants.push(...GMwindow)

src/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default function UserscriptPlugin(
2626
let isBuildWatch: boolean
2727
let socketConnection: connection | null = null
2828

29+
const workdir = dirname(fileURLToPath(import.meta.url))
2930
const logger = createLogger('info', {
3031
prefix: '[vite-userscript-plugin]',
3132
allowClearScreen: true
@@ -120,7 +121,7 @@ export default function UserscriptPlugin(
120121
const proxyFilePath = resolve(rootDir, outDir, proxyFilename)
121122
const metaFilePath = resolve(rootDir, outDir, metaFilename)
122123
const hotReloadPath = resolve(
123-
dirname(fileURLToPath(import.meta.url)),
124+
workdir,
124125
`hot-reload-${config.header.name}.js`
125126
)
126127

@@ -141,10 +142,7 @@ export default function UserscriptPlugin(
141142

142143
if (isBuildWatch) {
143144
const hotReloadFile = readFileSync(
144-
resolve(
145-
dirname(fileURLToPath(import.meta.url)),
146-
'hot-reload.js'
147-
),
145+
resolve(workdir, 'hot-reload.js'),
148146
'utf8'
149147
)
150148

src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { grants } from './constants.js'
1+
import { GM, GMwindow } from './constants.js'
22

33
export interface Transform {
44
file: string
@@ -13,7 +13,9 @@ export type RunAt =
1313
| 'document-idle'
1414
| 'context-menu'
1515

16-
export type Grants = typeof grants[number]
16+
export type GMLiterals<T extends string> = [`GM_${T}` | `GM.${T}`]
17+
export type GMWindow = typeof GMwindow[number]
18+
export type Grants = GMWindow | GMLiterals<typeof GM[number]>[number]
1719

1820
export type HeaderConfig = {
1921
[property: string]: string | boolean | number | string[] | undefined

test/__snapshots__/banner.test.ts.snap

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,54 @@ exports[`banner default snapshot 1`] = `
2121
// @updateURL https://vitest.dev
2222
// @resource https://vitest.dev
2323
// @require https://example.com/index.js
24-
// @grant unsafeWindow
25-
// @grant window.onurlchange
26-
// @grant window.focus
27-
// @grant window.close
2824
// @grant GM_setValue
25+
// @grant GM.setValue
2926
// @grant GM_getValue
27+
// @grant GM.getValue
3028
// @grant GM_deleteValue
29+
// @grant GM.deleteValue
3130
// @grant GM_listValues
31+
// @grant GM.listValues
3232
// @grant GM_setClipboard
33+
// @grant GM.setClipboard
3334
// @grant GM_addStyle
35+
// @grant GM.addStyle
3436
// @grant GM_addElement
37+
// @grant GM.addElement
3538
// @grant GM_addValueChangeListener
39+
// @grant GM.addValueChangeListener
3640
// @grant GM_removeValueChangeListener
41+
// @grant GM.removeValueChangeListener
3742
// @grant GM_registerMenuCommand
43+
// @grant GM.registerMenuCommand
3844
// @grant GM_unregisterMenuCommand
45+
// @grant GM.unregisterMenuCommand
3946
// @grant GM_download
47+
// @grant GM.download
4048
// @grant GM_getTab
49+
// @grant GM.getTab
4150
// @grant GM_getTabs
51+
// @grant GM.getTabs
4252
// @grant GM_saveTab
53+
// @grant GM.saveTab
4354
// @grant GM_openInTab
55+
// @grant GM.openInTab
4456
// @grant GM_notification
57+
// @grant GM.notification
4558
// @grant GM_getResourceURL
59+
// @grant GM.getResourceURL
4660
// @grant GM_getResourceText
61+
// @grant GM.getResourceText
4762
// @grant GM_xmlhttpRequest
48-
// @grant GM_webRequest
63+
// @grant GM.xmlhttpRequest
4964
// @grant GM_log
65+
// @grant GM.log
5066
// @grant GM_info
67+
// @grant GM.info
68+
// @grant unsafeWindow
69+
// @grant window.onurlchange
70+
// @grant window.focus
71+
// @grant window.close
5172
// @match https://vitest.dev
5273
// @run-at document-start
5374
// ==/UserScript=="

tsup.config.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import { defineConfig } from 'tsup'
22

3-
export default defineConfig((option) => ({
4-
entry: ['src/index.ts', 'src/hot-reload.ts'],
5-
format: ['cjs', 'esm'],
6-
external: ['vite'],
7-
dts: true,
8-
clean: true,
9-
minify: true,
10-
watch: option.watch
11-
}))
3+
export default defineConfig((option) => {
4+
return [
5+
{
6+
entry: ['src/index.ts'],
7+
format: ['cjs', 'esm'],
8+
external: ['vite'],
9+
dts: true,
10+
clean: true,
11+
minify: true,
12+
watch: option.watch
13+
},
14+
{
15+
entry: ['src/hot-reload.ts'],
16+
format: ['esm'],
17+
clean: true,
18+
minify: true,
19+
watch: option.watch
20+
}
21+
]
22+
})

0 commit comments

Comments
 (0)