( code: string, url: string )
| 219 | |
| 220 | // 通过代码解析出脚本信息 (Subscribe) |
| 221 | export async function prepareSubscribeByCode( |
| 222 | code: string, |
| 223 | url: string |
| 224 | ): Promise<{ subscribe: Subscribe; oldSubscribe?: Subscribe }> { |
| 225 | /* |
| 226 | // ==UserSubscribe== |
| 227 | // @name xxx |
| 228 | // @description 订阅xxx系列脚本 |
| 229 | // @version 0.1.0 |
| 230 | // @author You |
| 231 | // @connect www.baidu.com |
| 232 | // @scriptUrl https://script.tampermonkey.net.cn/48.user.js |
| 233 | // @scriptUrl https://script.tampermonkey.net.cn/49.user.js |
| 234 | // ==/UserSubscribe== |
| 235 | */ |
| 236 | const dao = new SubscribeDAO(); |
| 237 | const metadata = parseMetadata(code); |
| 238 | if (!metadata) { |
| 239 | throw new Error(i18n_t("error_metadata_invalid")); |
| 240 | } |
| 241 | if (metadata.name === undefined) { |
| 242 | throw new Error(i18n_t("error_subscribe_name_required")); |
| 243 | } |
| 244 | const now = Date.now(); |
| 245 | const subscribe: Subscribe = { |
| 246 | url, // url of the user.sub.js |
| 247 | name: metadata.name[0], |
| 248 | code, |
| 249 | author: (metadata.author && metadata.author[0]) || "", |
| 250 | scripts: {}, |
| 251 | metadata: metadata, |
| 252 | status: SubscribeStatusType.enable, |
| 253 | createtime: now, |
| 254 | updatetime: now, |
| 255 | checktime: now, |
| 256 | }; |
| 257 | const old = await dao.get(url); // 已存在 -> 把之前的 scripts, createtime, status 抽出来 |
| 258 | if (old) { |
| 259 | const { url, scripts, createtime, status } = old; |
| 260 | // url 是一样的;Subscribe 不使用 name 和 namespace 判断,仅使用 url 作唯一键 |
| 261 | Object.assign(subscribe, { url, scripts, createtime, status }); |
| 262 | } |
| 263 | return { subscribe, oldSubscribe: old }; |
| 264 | } |
no test coverage detected