forked from utags/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.mjs
More file actions
87 lines (72 loc) · 2.12 KB
/
Copy pathwatch.mjs
File metadata and controls
87 lines (72 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import fs from 'node:fs'
import { getBuildOptions, runDevServer } from '../common.mjs'
const target = 'userscript'
const tag = 'dev'
const buildOptions = getBuildOptions(target, tag)
buildOptions.alias = {
...buildOptions.alias,
'browser-extension-storage': 'browser-extension-storage/userscript',
'browser-extension-utils': 'browser-extension-utils/userscript',
}
const { port } = await runDevServer(buildOptions, target, tag)
const text = fs.readFileSync(`build/${target}-${tag}/content.js`, 'utf8')
// Get all userscript GM_* and GM.* functions
const matched = new Set()
text.replaceAll(/(GM[_.]\w+)/gm, (match) => {
matched.add(match)
})
const grants = [...matched]
.map((v) => `// @grant${' '.repeat(8)}${v}`)
.join('\n')
matched.add('GM')
const apiExports = [...matched]
.filter((v) => !v.includes('GM.'))
.map((v) => ` "${v}": typeof ${v} === "undefined" ? undefined : ${v},`)
.join('\n')
const code = `// ==UserScript==
// @name localhost:${port}
// @namespace http://tampermonkey.net/
// @version 0.0.1
// @description try to take over the world!
// @author You
// @match https://*/*
// @match http://*/*
${grants}
// ==/UserScript==
(function () {
"use strict";
if (!document.body) {
return;
}
document.GMFunctions = {
${apiExports}
}
const script = document.createElement("script");
script.src = "http://localhost:${port}/content.js";
document.body.append(script);
new EventSource("http://localhost:${port}/esbuild").addEventListener(
"change",
() => {
location.reload();
}
);
})();
// END`
const html = `<html>
<head>
<title>Install Extension - target: ${target}</title>
</head>
<body>
<p><a href="index.user.js">Click this to install</a></p>
<p>Or add the code below to Tampermonkey</p>
<textarea
style="width: 100%; height: 80%; padding: 10px; box-sizing: border-box">
${code}</textarea>
<script>
document.querySelector("textarea").select();
</script>
</body>
</html>
`
fs.writeFileSync(`build/${target}-${tag}/index.html`, html)
fs.writeFileSync(`build/${target}-${tag}/index.user.js`, code)