(code: string)
| 22 | |
| 23 | // 从脚本代码抽出Metadata |
| 24 | export function parseMetadata(code: string): SCMetadata | null { |
| 25 | let isSubscribe = false; |
| 26 | let headerContent: string; |
| 27 | let m: RegExpExecArray | null; |
| 28 | if ((m = HEADER_BLOCK.exec(code))) { |
| 29 | isSubscribe = m[1] === "Subscribe"; |
| 30 | headerContent = m[2]; |
| 31 | } else { |
| 32 | return null; |
| 33 | } |
| 34 | const metadata: SCMetadata = {} as SCMetadata; |
| 35 | META_LINE.lastIndex = 0; // 重置正则表达式的lastIndex(用于复用) |
| 36 | while ((m = META_LINE.exec(headerContent)) !== null) { |
| 37 | const key = m[1].toLowerCase(); |
| 38 | const val = m[2]?.trim() ?? ""; |
| 39 | const values = metadata[key] || (metadata[key] = []); |
| 40 | values.push(val); |
| 41 | } |
| 42 | if (!metadata.name || Object.keys(metadata).length < 3) return null; |
| 43 | if (!metadata.namespace) metadata.namespace = [""]; |
| 44 | if (isSubscribe) metadata.usersubscribe = []; // 如果是 user.sub.js, 在 metadata 会有一个额外的 usersubscribe |
| 45 | return metadata; |
| 46 | } |
| 47 | |
| 48 | // 从网址取得脚本代码 |
| 49 | export async function fetchScriptBody(url: string, signal?: AbortSignal): Promise<string> { |
no test coverage detected