-
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathfetch-userscript.ts
More file actions
27 lines (24 loc) · 1000 Bytes
/
fetch-userscript.ts
File metadata and controls
27 lines (24 loc) · 1000 Bytes
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
/**
* Resolve a userscript page URL to the raw code URL.
* Handles Greasyfork, OpenUserJS, and raw .user.js URLs.
*/
export function resolveUserscriptUrl(url: string): string {
const greasyforkMatch = url.match(/greasyfork\.org\/(?:[\w-]+\/)?scripts\/(\d+)/)
if (greasyforkMatch) {
return `https://greasyfork.org/scripts/${greasyforkMatch[1]}/code/script.user.js`
}
const openuserMatch = url.match(/openuserjs\.org\/scripts\/([^/]+)\/([^/]+)/)
if (openuserMatch) {
return `https://openuserjs.org/install/${openuserMatch[1]}/${openuserMatch[2]}.user.js`
}
return url
}
/**
* Parse userscript text: extract the name and strip the metadata header.
*/
export function parseUserscript(text: string): { code: string; name: string } {
const nameMatch = text.match(/@name\s+(.+)/)
const name = nameMatch ? nameMatch[1].trim() : 'Imported userscript'
const code = text.replace(/\/\/\s*==UserScript==[\s\S]*?\/\/\s*==\/UserScript==\s*/, '').trim()
return { code, name }
}