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.
- package/README.md +35 -27
- package/dist/index.cjs +3 -3
- package/dist/index.js +2 -2
- package/package.json +7 -8
- package/types/README.md +21 -0
- package/types/greasemonkey.d.ts +314 -0
- package/types/tampermonkey.d.ts +711 -0
- package/types/violentmonkey.d.ts +376 -0
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# vite-userscript-plugin
|
|
2
2
|
|
|
3
|
-

|
|
4
|
-

|
|
3
|
+
[](https://npmjs.com/vite-userscript-plugin)
|
|
4
|
+
[](./LICENCE)
|
|
5
|
+
[](https://github.com/crashmax-dev/vite-userscript-template)
|
|
5
6
|
|
|
6
|
-
> ⚡️ Tampermonkey userscript developing and build plugin based on [Vite](https://vitejs.dev)
|
|
7
|
-
> 📦 Use this [template](https://github.com/crashmax-dev/vite-userscript-template) to initialize your project.
|
|
7
|
+
> ⚡️ Tampermonkey userscript developing and build plugin based on [Vite](https://vitejs.dev).
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
- 🔧 Configure Tampermonkey's Userscript header.
|
|
13
13
|
- 💨 Import all `grant` by default in development mode.
|
|
14
14
|
- 📝 Automatically add used `grant` when building for production.
|
|
15
|
+
- 📦 Built-in Tampermonkey's TypeScript type definition.
|
|
15
16
|
|
|
16
17
|
## Install
|
|
17
18
|
|
|
@@ -36,21 +37,23 @@ import { defineConfig } from 'vite'
|
|
|
36
37
|
import Userscript from 'vite-userscript-plugin'
|
|
37
38
|
import { name, version } from './package.json'
|
|
38
39
|
|
|
39
|
-
export default defineConfig({
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
40
|
+
export default defineConfig((config) => {
|
|
41
|
+
return {
|
|
42
|
+
plugins: [
|
|
43
|
+
Userscript({
|
|
44
|
+
entry: 'src/index.ts',
|
|
45
|
+
header: {
|
|
46
|
+
name,
|
|
47
|
+
version,
|
|
48
|
+
match: [
|
|
49
|
+
'*://example.com',
|
|
50
|
+
'*://example.org',
|
|
51
|
+
'*://example.edu'
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
]
|
|
56
|
+
}
|
|
54
57
|
})
|
|
55
58
|
```
|
|
56
59
|
|
|
@@ -65,6 +68,18 @@ export default defineConfig({
|
|
|
65
68
|
}
|
|
66
69
|
```
|
|
67
70
|
|
|
71
|
+
### `tsconfig.json`
|
|
72
|
+
|
|
73
|
+
```json
|
|
74
|
+
{
|
|
75
|
+
"compilerOptions": {
|
|
76
|
+
"types": [
|
|
77
|
+
"vite-userscript-plugin/tampermonkey"
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
68
83
|
## Plugin Configuration
|
|
69
84
|
|
|
70
85
|
```ts
|
|
@@ -95,17 +110,10 @@ interface UserscriptPluginConfig {
|
|
|
95
110
|
}
|
|
96
111
|
```
|
|
97
112
|
|
|
98
|
-
##
|
|
113
|
+
## Examples
|
|
99
114
|
|
|
100
115
|
See the [examples](https://github.com/crashmax-dev/vite-userscript-plugin/tree/master/examples) folder.
|
|
101
116
|
|
|
102
|
-
## Used by
|
|
103
|
-
|
|
104
|
-
- [vite-userscript-template](https://github.com/crashmax-dev/vite-userscript-template)
|
|
105
|
-
- [netangels-userscript](https://github.com/crashmax-dev/netangels-userscript)
|
|
106
|
-
- [godev-code-highlight](https://github.com/crashmax-dev/godev-code-highlight)
|
|
107
|
-
- [beautifier-userscript](https://github.com/crashmax-dev/beautifier-userscript)
|
|
108
|
-
|
|
109
117
|
## License
|
|
110
118
|
|
|
111
119
|
[MIT](./LICENCE) © [crashmax](https://github.com/crashmax-dev)
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";var Z=Object.create;var b=Object.defineProperty;var ee=Object.getOwnPropertyDescriptor;var te=Object.getOwnPropertyNames;var re=Object.getPrototypeOf,ne=Object.prototype.hasOwnProperty;var oe=(e,r)=>{for(var t in r)b(e,t,{get:r[t],enumerable:!0})},T=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of te(r))!ne.call(e,s)&&s!==t&&b(e,s,{get:()=>r[s],enumerable:!(n=ee(r,s))||n.enumerable});return e};var u=(e,r,t)=>(t=e!=null?Z(re(e)):{},T(r||!e||!e.__esModule?b(t,"default",{value:e,enumerable:!0}):t,e)),se=e=>T(b({},"__esModule",{value:!0}),e);var le={};oe(le,{default:()=>N});module.exports=se(le);var V=u(require("get-port"),1),c=require("fs"),D=require("http"),a=require("path"),K=require("url"),A=u(require("open"),1),M=u(require("picocolors"),1),H=u(require("sanitize-filename"),1),W=u(require("serve-handler"),1),q=require("vite"),B=require("websocket");var g=class{constructor(r){this.config=r;this.downloadMeta(),this.configKeys=Object.keys(this.config),this.maxKeyLength=Math.max(...this.configKeys.map(t=>t.length))+1}header=[];configKeys;maxKeyLength;downloadMeta(){let{name:r,homepage:t,updateURL:n,downloadURL:s}=this.config;t&&!n&&!s&&(this.config.updateURL=new URL(`${r}.meta.js`,t).href,this.config.downloadURL=new URL(`${r}.user.js`,t).href)}addSpaces(r){return" ".repeat(this.maxKeyLength-r.length)}addMetadata(r,t){let n=typeof t=="boolean";n&&!t||(t=n?"":`${this.addSpaces(r)}${t}`,this.header.push(`// @${r}${t}`))}generate(){for(let[r,t]of Object.entries(this.config))if(Array.isArray(t))t.forEach(n=>this.addMetadata(r,n));else{if(t===void 0)continue;this.addMetadata(r,t)}return["// ==UserScript==",...this.header,"// ==/UserScript=="].join(`
|
|
2
|
-
`)}};function E({entry:e,header:r}){return{build:{lib:{entry:e,name:r.name,formats:["iife"],fileName:()=>`${r.name}.js`},rollupOptions:{output:{extend:!0}}}}}var F=new RegExp(
|
|
3
|
-
`,"")),""}inject(){return this.styles.size?`GM_addStyle(\`${[...this.styles.values()].join("")}\`)`:void 0}merge(r){let t=[];for(let n of r){let s=this.styles.get(n);!s||t.push([n,s])}this.styles.clear(),t.forEach(n=>this.styles.set(...n))}},j=new _;var ce={};function N(e){let r,t,n=null,s=(0,a.dirname)((0,K.fileURLToPath)(ce.url))
|
|
2
|
+
`)}};function E({entry:e,header:r}){return{build:{lib:{entry:e,name:r.name,formats:["iife"],fileName:()=>`${r.name}.js`},rollupOptions:{output:{extend:!0}}}}}var F=new RegExp(/\.(t|j)sx?$/),L=new RegExp(/\.(s[ac]|c|le)ss$/),ie=["setValue","getValue","deleteValue","listValues","setClipboard","addStyle","addElement","addValueChangeListener","removeValueChangeListener","registerMenuCommand","unregisterMenuCommand","download","getTab","getTabs","saveTab","openInTab","notification","getResourceURL","getResourceText","xmlhttpRequest","log","info"],ae=["unsafeWindow","window.onurlchange","window.focus","window.close"],h=ie.map(e=>[`GM_${e}`,`GM.${e}`]).flat();h.push(...ae);var O=require("vite");function U(e){return[...new Set(Array.isArray(e)?e:e?[e]:[])]}async function y({file:e,name:r,loader:t}){let{code:n}=await(0,O.transformWithEsbuild)(e,r,{loader:t,minify:!0,sourcemap:!1,legalComments:"none"});return n}function k(e){let r=[];for(let t of h)e.indexOf(t)!==-1&&r.push(t);return r}var _=class{styles=new Map;async add(r,t){let n=await y({file:r,name:t,loader:"css"});return this.styles.set(t,n.replace(`
|
|
3
|
+
`,"")),""}inject(){return this.styles.size?`GM_addStyle(\`${[...this.styles.values()].join("")}\`)`:void 0}merge(r){let t=[];for(let n of r){let s=this.styles.get(n);!s||t.push([n,s])}this.styles.clear(),t.forEach(n=>this.styles.set(...n))}},j=new _;var ce={};function N(e){let r,t,n=null,s=(0,a.dirname)((0,K.fileURLToPath)(ce.url)),$=(0,q.createLogger)("info",{prefix:"[vite-userscript-plugin]",allowClearScreen:!0}),w=(0,D.createServer)((o,i)=>(0,W.default)(o,i,{public:r.build.outDir})),z=B.server;return new z({httpServer:w}).on("request",o=>{n=o.accept(null,o.origin)}),{name:"vite-userscript-plugin",apply:"build",config(){return E(e)},async configResolved(o){r=o,t=o.build.watch??!1,e.entry=(0,a.resolve)(o.root,e.entry),e.header.name=(0,H.default)(e.header.name),Array.from(["match","require","include","exclude","resource","connect"]).forEach(i=>{let p=e.header[i];e.header[i]=U(p)}),e.server={port:await(0,V.default)(),open:!1,...e.server}},async transform(o,i){let p=o;return L.test(i)&&(p=await j.add(o,i)),i.includes(e.entry)&&(p=o+"__STYLE__"),{code:p,map:null}},generateBundle(o,i){for(let[p,m]of Object.entries(i)){let f=Object.keys(m.modules).filter(S=>L.test(S));f.length&&j.merge(f)}},async writeBundle(o,i){let{open:p,port:m}=e.server,P=`${e.header.name}.user.js`,f=`${e.header.name}.proxy.user.js`,S=`${e.header.name}.meta.js`;for(let[d]of Object.entries(i))if(F.test(d)){let x=r.root,v=r.build.outDir,C=(0,a.resolve)(x,v,d),Y=(0,a.resolve)(x,v,P),I=(0,a.resolve)(x,v,f),J=(0,a.resolve)(x,v,S),R=(0,a.resolve)(s,`hot-reload-${e.header.name}.js`);try{let l=(0,c.readFileSync)(C,"utf8");if(l=l.replace("__STYLE__",`${j.inject()}`),l=await y({file:l,name:d,loader:"js"}),e.header.grant=U(t?h:[...k(l),...e.header.grant??[]]),t){let Q=(0,c.readFileSync)((0,a.resolve)(s,"hot-reload.js"),"utf8"),X=await y({file:Q.replace("__WS__",`ws://localhost:${m}`),name:R,loader:"js"});(0,c.writeFileSync)(R,X),(0,c.writeFileSync)(I,new g({...e.header,require:[...e.header.require,"file://"+R,"file://"+C]}).generate())}let G=new g(e.header).generate();(0,c.writeFileSync)(C,l),(0,c.writeFileSync)(J,G),(0,c.writeFileSync)(Y,`${G}
|
|
4
4
|
|
|
5
|
-
${l}`)}catch(l){console.log(l)}}if(t&&!w.listening){let d=`http://localhost:${m}`;w.listen(m,()=>{
|
|
5
|
+
${l}`)}catch(l){console.log(l)}}if(t&&!w.listening){let d=`http://localhost:${m}`;w.listen(m,()=>{$.clearScreen("info"),$.info(M.default.blue(`Running at: ${M.default.gray(d)}`))}),p&&await(0,A.default)(`${d}/${f}`)}else t||(w.close(),process.exit(0))},buildEnd(){t&&($.clearScreen("info"),n&&n.sendUTF(JSON.stringify({message:"reload"})))}}}0&&(module.exports={});
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import q from"get-port";import{readFileSync as T,writeFileSync as h}from"fs";import{createServer as B}from"http";import{dirname as N,resolve as c}from"path";import{fileURLToPath as z}from"url";import Y from"open";import E from"picocolors";import I from"sanitize-filename";import J from"serve-handler";import{createLogger as Q}from"vite";import{server as X}from"websocket";var f=class{constructor(r){this.config=r;this.downloadMeta(),this.configKeys=Object.keys(this.config),this.maxKeyLength=Math.max(...this.configKeys.map(t=>t.length))+1}header=[];configKeys;maxKeyLength;downloadMeta(){let{name:r,homepage:t,updateURL:n,downloadURL:l}=this.config;t&&!n&&!l&&(this.config.updateURL=new URL(`${r}.meta.js`,t).href,this.config.downloadURL=new URL(`${r}.user.js`,t).href)}addSpaces(r){return" ".repeat(this.maxKeyLength-r.length)}addMetadata(r,t){let n=typeof t=="boolean";n&&!t||(t=n?"":`${this.addSpaces(r)}${t}`,this.header.push(`// @${r}${t}`))}generate(){for(let[r,t]of Object.entries(this.config))if(Array.isArray(t))t.forEach(n=>this.addMetadata(r,n));else{if(t===void 0)continue;this.addMetadata(r,t)}return["// ==UserScript==",...this.header,"// ==/UserScript=="].join(`
|
|
2
|
-
`)}};function M({entry:e,header:r}){return{build:{lib:{entry:e,name:r.name,formats:["iife"],fileName:()=>`${r.name}.js`},rollupOptions:{output:{extend:!0}}}}}var P=new RegExp(
|
|
3
|
-
`,"")),""}inject(){return this.styles.size?`GM_addStyle(\`${[...this.styles.values()].join("")}\`)`:void 0}merge(r){let t=[];for(let n of r){let l=this.styles.get(n);!l||t.push([n,l])}this.styles.clear(),t.forEach(n=>this.styles.set(...n))}},v=new L;function Z(e){let r,t,n=null,l=N(z(import.meta.url)),b=Q("info",{prefix:"[vite-userscript-plugin]",allowClearScreen:!0}),y=B((o,s)=>J(o,s,{public:r.build.outDir})),F=X;return new F({httpServer:y}).on("request",o=>{n=o.accept(null,o.origin)}),{name:"vite-userscript-plugin",apply:"build",config(){return M(e)},async configResolved(o){r=o,t=o.build.watch??!1,e.entry=c(o.root,e.entry),e.header.name=I(e.header.name),Array.from(["match","require","include","exclude","resource","connect"]).forEach(s=>{let a=e.header[s];e.header[s]=R(a)}),e.server={port:await q(),open:!1,...e.server}},async transform(o,s){let a=o;return C.test(s)&&(a=await v.add(o,s)),s.includes(e.entry)&&(a=o+"__STYLE__"),{code:a,map:null}},generateBundle(o,s){for(let[a,d]of Object.entries(s)){let m=Object.keys(d.modules).filter(j=>C.test(j));m.length&&v.merge(m)}},async writeBundle(o,s){let{open:a,port:d}=e.server,U=`${e.header.name}.user.js`,m=`${e.header.name}.proxy.user.js`,j=`${e.header.name}.meta.js`;for(let[p]of Object.entries(s))if(P.test(p)){let w=r.root,x=r.build.outDir
|
|
2
|
+
`)}};function M({entry:e,header:r}){return{build:{lib:{entry:e,name:r.name,formats:["iife"],fileName:()=>`${r.name}.js`},rollupOptions:{output:{extend:!0}}}}}var P=new RegExp(/\.(t|j)sx?$/),C=new RegExp(/\.(s[ac]|c|le)ss$/),A=["setValue","getValue","deleteValue","listValues","setClipboard","addStyle","addElement","addValueChangeListener","removeValueChangeListener","registerMenuCommand","unregisterMenuCommand","download","getTab","getTabs","saveTab","openInTab","notification","getResourceURL","getResourceText","xmlhttpRequest","log","info"],H=["unsafeWindow","window.onurlchange","window.focus","window.close"],u=A.map(e=>[`GM_${e}`,`GM.${e}`]).flat();u.push(...H);import{transformWithEsbuild as W}from"vite";function R(e){return[...new Set(Array.isArray(e)?e:e?[e]:[])]}async function g({file:e,name:r,loader:t}){let{code:n}=await W(e,r,{loader:t,minify:!0,sourcemap:!1,legalComments:"none"});return n}function G(e){let r=[];for(let t of u)e.indexOf(t)!==-1&&r.push(t);return r}var L=class{styles=new Map;async add(r,t){let n=await g({file:r,name:t,loader:"css"});return this.styles.set(t,n.replace(`
|
|
3
|
+
`,"")),""}inject(){return this.styles.size?`GM_addStyle(\`${[...this.styles.values()].join("")}\`)`:void 0}merge(r){let t=[];for(let n of r){let l=this.styles.get(n);!l||t.push([n,l])}this.styles.clear(),t.forEach(n=>this.styles.set(...n))}},v=new L;function Z(e){let r,t,n=null,l=N(z(import.meta.url)),b=Q("info",{prefix:"[vite-userscript-plugin]",allowClearScreen:!0}),y=B((o,s)=>J(o,s,{public:r.build.outDir})),F=X;return new F({httpServer:y}).on("request",o=>{n=o.accept(null,o.origin)}),{name:"vite-userscript-plugin",apply:"build",config(){return M(e)},async configResolved(o){r=o,t=o.build.watch??!1,e.entry=c(o.root,e.entry),e.header.name=I(e.header.name),Array.from(["match","require","include","exclude","resource","connect"]).forEach(s=>{let a=e.header[s];e.header[s]=R(a)}),e.server={port:await q(),open:!1,...e.server}},async transform(o,s){let a=o;return C.test(s)&&(a=await v.add(o,s)),s.includes(e.entry)&&(a=o+"__STYLE__"),{code:a,map:null}},generateBundle(o,s){for(let[a,d]of Object.entries(s)){let m=Object.keys(d.modules).filter(j=>C.test(j));m.length&&v.merge(m)}},async writeBundle(o,s){let{open:a,port:d}=e.server,U=`${e.header.name}.user.js`,m=`${e.header.name}.proxy.user.js`,j=`${e.header.name}.meta.js`;for(let[p]of Object.entries(s))if(P.test(p)){let w=r.root,x=r.build.outDir,$=c(w,x,p),O=c(w,x,U),k=c(w,x,m),V=c(w,x,j),S=c(l,`hot-reload-${e.header.name}.js`);try{let i=T($,"utf8");if(i=i.replace("__STYLE__",`${v.inject()}`),i=await g({file:i,name:p,loader:"js"}),e.header.grant=R(t?u:[...G(i),...e.header.grant??[]]),t){let D=T(c(l,"hot-reload.js"),"utf8"),K=await g({file:D.replace("__WS__",`ws://localhost:${d}`),name:S,loader:"js"});h(S,K),h(k,new f({...e.header,require:[...e.header.require,"file://"+S,"file://"+$]}).generate())}let _=new f(e.header).generate();h($,i),h(V,_),h(O,`${_}
|
|
4
4
|
|
|
5
5
|
${i}`)}catch(i){console.log(i)}}if(t&&!y.listening){let p=`http://localhost:${d}`;y.listen(d,()=>{b.clearScreen("info"),b.info(E.blue(`Running at: ${E.gray(p)}`))}),a&&await Y(`${p}/${m}`)}else t||(y.close(),process.exit(0))},buildEnd(){t&&(b.clearScreen("info"),n&&n.sendUTF(JSON.stringify({message:"reload"})))}}}export{Z as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-userscript-plugin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"import": "./dist/index.js"
|
|
11
11
|
},
|
|
12
12
|
"files": [
|
|
13
|
-
"dist"
|
|
13
|
+
"dist",
|
|
14
|
+
"types"
|
|
14
15
|
],
|
|
15
16
|
"repository": {
|
|
16
17
|
"type": "git",
|
|
@@ -34,7 +35,6 @@
|
|
|
34
35
|
"url": "https://github.com/crashmax-dev/vite-userscript-plugin/issues"
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
|
-
"@types/tampermonkey": "^4.0.5",
|
|
38
38
|
"get-port": "^6.1.2",
|
|
39
39
|
"open": "^8.4.0",
|
|
40
40
|
"picocolors": "^1.0.0",
|
|
@@ -48,16 +48,15 @@
|
|
|
48
48
|
"@types/node": "^18.7.18",
|
|
49
49
|
"@types/serve-handler": "^6.1.1",
|
|
50
50
|
"@types/websocket": "^1.0.5",
|
|
51
|
-
"@vitest/ui": "^0.23.
|
|
51
|
+
"@vitest/ui": "^0.23.4",
|
|
52
52
|
"tsup": "^6.2.3",
|
|
53
|
-
"turbo": "^1.4.
|
|
53
|
+
"turbo": "^1.4.7",
|
|
54
54
|
"typescript": "^4.8.3",
|
|
55
|
-
"vite": "^3.1.
|
|
55
|
+
"vite": "^3.1.2",
|
|
56
56
|
"vite-plugin-dts": "^1.5.0",
|
|
57
|
-
"vitest": "^0.23.
|
|
57
|
+
"vitest": "^0.23.4"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"@types/tampermonkey": ">=4.0.0",
|
|
61
60
|
"vite": ">=2.9.0"
|
|
62
61
|
},
|
|
63
62
|
"scripts": {
|
package/types/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Userscript type definitions
|
|
2
|
+
|
|
3
|
+
[](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/tampermonkey)
|
|
4
|
+
[](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/greasemonkey)
|
|
5
|
+
[](https://github.com/violentmonkey/types)
|
|
6
|
+
|
|
7
|
+
> Type declaration for `GM_*`, `GM.*` APIs in Tampermonkey, Greasemonkey and Violentmonkey.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install --save @types/tampermonkey
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install --save @types/greasemonkey
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install --save @violentmonkey/types
|
|
21
|
+
```
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
// Type definitions for Greasemonkey 4.x
|
|
2
|
+
// Project: http://www.greasespot.net/
|
|
3
|
+
// Definitions by: Kota Saito <https://github.com/kotas>
|
|
4
|
+
// Nikolay Borzov <https://github.com/nikolay-borzov>
|
|
5
|
+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
6
|
+
|
|
7
|
+
// This definition is based on the API reference of Greasemonkey
|
|
8
|
+
// https://wiki.greasespot.net/Greasemonkey_Manual:API
|
|
9
|
+
// TypeScript Version: 3.2
|
|
10
|
+
|
|
11
|
+
declare namespace GM {
|
|
12
|
+
interface ScriptInfo {
|
|
13
|
+
/** Possibly empty string. */
|
|
14
|
+
description: string
|
|
15
|
+
excludes: string[]
|
|
16
|
+
includes: string[]
|
|
17
|
+
matches: string[]
|
|
18
|
+
name: string
|
|
19
|
+
/** Possibly empty string. */
|
|
20
|
+
namespace: string
|
|
21
|
+
/**
|
|
22
|
+
* An object keyed by resource name.
|
|
23
|
+
* Each value is an object with keys `name` and `mimetype` and `url`
|
|
24
|
+
* with string values.
|
|
25
|
+
*/
|
|
26
|
+
resources: {
|
|
27
|
+
[resourceName: string]: {
|
|
28
|
+
name: string
|
|
29
|
+
mimetype: string
|
|
30
|
+
url: string
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** @default 'end' */
|
|
34
|
+
runAt: 'start' | 'end' | 'idle'
|
|
35
|
+
uuid: string
|
|
36
|
+
/** Possibly empty string. */
|
|
37
|
+
version: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type Value = string | boolean | number
|
|
41
|
+
|
|
42
|
+
interface Response<TContext> {
|
|
43
|
+
readonly responseHeaders: string
|
|
44
|
+
readonly finalUrl: string
|
|
45
|
+
/** The same object passed into the original request */
|
|
46
|
+
readonly context?: TContext | undefined
|
|
47
|
+
|
|
48
|
+
readonly readyState: 1 | 2 | 3 | 4
|
|
49
|
+
readonly response: any
|
|
50
|
+
readonly responseText: string
|
|
51
|
+
readonly responseXML: Document | false
|
|
52
|
+
readonly status: number
|
|
53
|
+
readonly statusText: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface ProgressResponse<TContext> extends Response<TContext> {
|
|
57
|
+
lengthComputable: boolean
|
|
58
|
+
loaded: number
|
|
59
|
+
total: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface Request<TContext = any> {
|
|
63
|
+
// Fields
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The URL to make the request to. Must be an absolute URL, beginning
|
|
67
|
+
* with the scheme. May be relative to the current page.
|
|
68
|
+
*/
|
|
69
|
+
url: string
|
|
70
|
+
/** String type of HTTP request to make (E.G. "GET", "POST") */
|
|
71
|
+
method:
|
|
72
|
+
| 'GET'
|
|
73
|
+
| 'POST'
|
|
74
|
+
| 'PUT'
|
|
75
|
+
| 'DELETE'
|
|
76
|
+
| 'PATCH'
|
|
77
|
+
| 'HEAD'
|
|
78
|
+
| 'TRACE'
|
|
79
|
+
| 'OPTIONS'
|
|
80
|
+
| 'CONNECT'
|
|
81
|
+
/**
|
|
82
|
+
* When true, the data is sent as a Blob
|
|
83
|
+
* @default false
|
|
84
|
+
*/
|
|
85
|
+
binary?: boolean | undefined
|
|
86
|
+
/**
|
|
87
|
+
* Any object (Compatibility: 1.10+). This object will also be the
|
|
88
|
+
* context property of the Response Object.
|
|
89
|
+
*/
|
|
90
|
+
context?: TContext | undefined
|
|
91
|
+
/**
|
|
92
|
+
* Data to send in the request body. Usually for POST method requests.
|
|
93
|
+
* If the data field contains form-encoded data, you usually must also
|
|
94
|
+
* set the header `'Content-Type': 'application/x-www-form-urlencoded'`
|
|
95
|
+
* in the `headers` field.
|
|
96
|
+
*/
|
|
97
|
+
data?: string | undefined
|
|
98
|
+
/** A set of headers to include in the request */
|
|
99
|
+
headers?:
|
|
100
|
+
| {
|
|
101
|
+
[header: string]: string
|
|
102
|
+
}
|
|
103
|
+
| undefined
|
|
104
|
+
/**
|
|
105
|
+
* A MIME type to specify with the request (e.g.
|
|
106
|
+
* "text/html; charset=ISO-8859-1")
|
|
107
|
+
*/
|
|
108
|
+
overrideMimeType?: string | undefined
|
|
109
|
+
/** User name to use for authentication purposes. */
|
|
110
|
+
user?: string | undefined
|
|
111
|
+
/** Password to use for authentication purposes */
|
|
112
|
+
password?: string | undefined
|
|
113
|
+
/** Decode the response as specified type. Default value is "text" */
|
|
114
|
+
responseType?: XMLHttpRequestResponseType | undefined
|
|
115
|
+
/**
|
|
116
|
+
* When `true`, this is a synchronous request.
|
|
117
|
+
* Be careful: The entire Firefox UI will be locked and frozen until the
|
|
118
|
+
* request completes.In this mode, more data will be available in the
|
|
119
|
+
* return value.
|
|
120
|
+
*/
|
|
121
|
+
synchronous?: boolean | undefined
|
|
122
|
+
/**
|
|
123
|
+
* The number of milliseconds to wait before terminating the call. Zero
|
|
124
|
+
* (the default) means wait forever.
|
|
125
|
+
*/
|
|
126
|
+
timeout?: number | undefined
|
|
127
|
+
/**
|
|
128
|
+
* Object containing optional function callbacks to monitor the upload
|
|
129
|
+
* of data.
|
|
130
|
+
*/
|
|
131
|
+
upload?:
|
|
132
|
+
| {
|
|
133
|
+
onabort?(response: Response<TContext>): void
|
|
134
|
+
onerror?(response: Response<TContext>): void
|
|
135
|
+
onload?(response: Response<TContext>): void
|
|
136
|
+
onprogress?(response: ProgressResponse<TContext>): void
|
|
137
|
+
}
|
|
138
|
+
| undefined
|
|
139
|
+
|
|
140
|
+
// Event handlers
|
|
141
|
+
|
|
142
|
+
/** Will be called when the request is aborted */
|
|
143
|
+
onabort?(response: Response<TContext>): void
|
|
144
|
+
/** Will be called if an error occurs while processing the request */
|
|
145
|
+
onerror?(response: Response<TContext>): void
|
|
146
|
+
/** Will be called when the request has completed successfully */
|
|
147
|
+
onload?(response: Response<TContext>): void
|
|
148
|
+
/** Will be called when the request progress changes */
|
|
149
|
+
onprogress?(response: ProgressResponse<TContext>): void
|
|
150
|
+
/** Will be called repeatedly while the request is in progress */
|
|
151
|
+
onreadystatechange?(response: Response<TContext>): void
|
|
152
|
+
/** Will be called if/when the request times out */
|
|
153
|
+
ontimeout?(response: Response<TContext>): void
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Window object of the content page where the user script is running on.
|
|
159
|
+
* @see {@link http://wiki.greasespot.net/UnsafeWindow}
|
|
160
|
+
*/
|
|
161
|
+
declare var unsafeWindow: Window
|
|
162
|
+
|
|
163
|
+
declare var GM: {
|
|
164
|
+
// Headers
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Meta data about the running user script.
|
|
168
|
+
* @see {@link https://wiki.greasespot.net/GM.info}
|
|
169
|
+
*/
|
|
170
|
+
info: {
|
|
171
|
+
/** An object containing data about the currently running script */
|
|
172
|
+
script: GM.ScriptInfo
|
|
173
|
+
/**
|
|
174
|
+
* A string, the entire literal Metadata Block (without the delimiters)
|
|
175
|
+
* for the currently running script
|
|
176
|
+
*/
|
|
177
|
+
scriptMetaStr: string
|
|
178
|
+
/**
|
|
179
|
+
* The name of the user script engine handling this script's execution.
|
|
180
|
+
* The string `Greasemonkey`
|
|
181
|
+
*/
|
|
182
|
+
scriptHandler: string
|
|
183
|
+
/** The version of Greasemonkey, a string e.g. `4.0` */
|
|
184
|
+
version: string
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Values
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Allows user script authors to persist simple values across page loads and
|
|
191
|
+
* across origins.
|
|
192
|
+
* Strings, booleans, and integers are currently the only allowed data types.
|
|
193
|
+
* @see {@link https://wiki.greasespot.net/GM.setValue}
|
|
194
|
+
* @param name The unique (within this script) name for this value.
|
|
195
|
+
* Should be restricted to valid Javascript identifier characters.
|
|
196
|
+
* @param value Any valid value of these types. Any other type may cause
|
|
197
|
+
* undefined behavior, including crashes
|
|
198
|
+
* @returns A Promise, resolved successfully with no value on success,
|
|
199
|
+
* rejected with no value on failure
|
|
200
|
+
*/
|
|
201
|
+
setValue(name: string, value: GM.Value): Promise<void>
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Retrieves a value that was set with `GM.setValue`
|
|
205
|
+
* @see {@link https://wiki.greasespot.net/GM.getValue}
|
|
206
|
+
* @param name The property name to get
|
|
207
|
+
* @param defaultValue The default value to be returned when none has
|
|
208
|
+
* previously been set
|
|
209
|
+
* @returns A Promise, rejected in case of error and otherwise resolved with:
|
|
210
|
+
* - When this name has been set - `string`, `integer` or `boolean` as
|
|
211
|
+
* previously set
|
|
212
|
+
* - When this name has not been set, and default is provided - The value
|
|
213
|
+
* passed as default
|
|
214
|
+
* - When this name has not been set, and default is not provided -
|
|
215
|
+
* `undefined`
|
|
216
|
+
* @example
|
|
217
|
+
* // Retrieving the value associated with the name 'timezoneOffset' with a default value defined:
|
|
218
|
+
* const timezoneOffset = await GM.getValue("timezoneOffset", -5)
|
|
219
|
+
* @example
|
|
220
|
+
* // For structured data used `JSON.stringify()` to place an object into storage and then `JSON.parse()` to convert it back
|
|
221
|
+
* const storedObject = JSON.parse(await GM.getValue('foo', '{}'));
|
|
222
|
+
*/
|
|
223
|
+
getValue(name: string): Promise<GM.Value | undefined>
|
|
224
|
+
getValue<TValue = GM.Value>(
|
|
225
|
+
name: string,
|
|
226
|
+
defaultValue: TValue
|
|
227
|
+
): Promise<TValue>
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Deletes an existing name / value pair from storage.
|
|
231
|
+
* @see {@link https://wiki.greasespot.net/GM.deleteValue}
|
|
232
|
+
* @param name Property name to delete
|
|
233
|
+
* @returns A Promise, resolved successfully with no value on success,
|
|
234
|
+
* rejected with no value on failure.
|
|
235
|
+
*/
|
|
236
|
+
deleteValue(name: string): Promise<void>
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Retrieves an array of preference names that this script has stored
|
|
240
|
+
* @see {@link https://wiki.greasespot.net/GM.listValues}
|
|
241
|
+
* @returns A Promise, rejected in case of error and otherwise resolved with
|
|
242
|
+
* an string[] for previously set values
|
|
243
|
+
*/
|
|
244
|
+
listValues(): Promise<string[]>
|
|
245
|
+
|
|
246
|
+
// Resources
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Given a defined `@resource`, this method returns it as a URL
|
|
250
|
+
* @see {@link https://wiki.greasespot.net/GM.getResourceUrl}
|
|
251
|
+
* @param resourceName The name provided when the `@resource` was defined
|
|
252
|
+
* @returns A Promise, rejected on failure and resolved with a string URL on
|
|
253
|
+
* success.
|
|
254
|
+
* Treat the result as opaque string. It will work where you need a URL
|
|
255
|
+
* (for a `<link>` or `<style>` for CSS, for an `<img>` tag, or similar).
|
|
256
|
+
*/
|
|
257
|
+
getResourceUrl(resourceName: string): Promise<string>
|
|
258
|
+
|
|
259
|
+
// Other
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Displays a notification to the user, using the underlying operating
|
|
263
|
+
* system's notification mechanism
|
|
264
|
+
* @see {@link https://wiki.greasespot.net/GM.notification}
|
|
265
|
+
* @param text The main notification text
|
|
266
|
+
* @param title The title of the notification
|
|
267
|
+
* @param image The URL for an image to display in the dialog. If not
|
|
268
|
+
* provided, the Greasemonkey logo by default.
|
|
269
|
+
* @param onClick Callback, triggered when the notification's button is
|
|
270
|
+
* clicked.
|
|
271
|
+
*/
|
|
272
|
+
notification(
|
|
273
|
+
text: string,
|
|
274
|
+
title: string,
|
|
275
|
+
image?: string,
|
|
276
|
+
onClick?: () => void
|
|
277
|
+
): void
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Opens the specified URL in a new tab.
|
|
281
|
+
* @see {@link https://wiki.greasespot.net/GM.openInTab}
|
|
282
|
+
* @param url The URL to navigate the new tab to
|
|
283
|
+
* @param openInBackground Force tab to/to not open in a background tab.
|
|
284
|
+
* Default (unspecified) behavior honors Firefox configuration.
|
|
285
|
+
*/
|
|
286
|
+
openInTab(url: string, openInBackground?: boolean): void
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Adds an item to the User Script Commands menu.
|
|
290
|
+
* @param caption The caption to display on the menu item.
|
|
291
|
+
* @param commandFunc The function to call when the menu item is selected.
|
|
292
|
+
* @param accessKey A single character that can be used to select the
|
|
293
|
+
* item when the menu is open. It should be a letter in the caption.
|
|
294
|
+
* @see {@link https://wiki.greasespot.net/GM.registerMenuCommand}
|
|
295
|
+
*/
|
|
296
|
+
registerMenuCommand(
|
|
297
|
+
caption: string,
|
|
298
|
+
commandFunc: () => void,
|
|
299
|
+
accessKey?: string
|
|
300
|
+
): void
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Sets the current contents of the operating system's clipboard
|
|
304
|
+
* @see {@link https://wiki.greasespot.net/GM.setClipboard}
|
|
305
|
+
*/
|
|
306
|
+
setClipboard(text: string): void
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Performs a similar function to the standard XMLHttpRequest object, but
|
|
310
|
+
* allows these requests to cross the [same origin policy]{@link https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy} boundaries.
|
|
311
|
+
* @see {@link https://wiki.greasespot.net/GM.xmlHttpRequest}
|
|
312
|
+
*/
|
|
313
|
+
xmlHttpRequest(details: GM.Request): void
|
|
314
|
+
}
|