(params: {
uuid: string;
code: string;
args: Record<string, unknown>;
grants: string[];
name: string;
requires?: Array<{ url: string; content: string }>;
configValues?: Record<string, unknown>;
})
| 383 | |
| 384 | // 执行 Skill Script:构建最小化脚本上下文,注入 args,执行并返回结果 |
| 385 | async executeSkillScript(params: { |
| 386 | uuid: string; |
| 387 | code: string; |
| 388 | args: Record<string, unknown>; |
| 389 | grants: string[]; |
| 390 | name: string; |
| 391 | requires?: Array<{ url: string; content: string }>; |
| 392 | configValues?: Record<string, unknown>; |
| 393 | }): Promise<unknown> { |
| 394 | const uuid = params.uuid; |
| 395 | const metadata: any = { |
| 396 | grant: params.grants, |
| 397 | }; |
| 398 | // 通过 compileScriptCodeByResource 包裹代码,加上 with(arguments[0]||this.$) 等上下文绑定, |
| 399 | // 使脚本能访问 sandboxContext 上注入的变量(args、GM API 等) |
| 400 | const compiledCode = compileScriptCodeByResource({ |
| 401 | name: params.name, |
| 402 | code: params.code, |
| 403 | require: params.requires || [], |
| 404 | isContextMenu: false, |
| 405 | }); |
| 406 | |
| 407 | // 构造最小化的 ScriptLoadInfo |
| 408 | const scriptLoadInfo = { |
| 409 | uuid, |
| 410 | name: params.name, |
| 411 | namespace: "", |
| 412 | type: SCRIPT_TYPE_BACKGROUND, |
| 413 | status: 1, |
| 414 | sort: 0, |
| 415 | runStatus: "complete" as const, |
| 416 | createtime: Date.now(), |
| 417 | checktime: 0, |
| 418 | code: compiledCode, |
| 419 | value: {}, |
| 420 | flag: "", |
| 421 | resource: {}, |
| 422 | metadata, |
| 423 | originalMetadata: metadata, |
| 424 | metadataStr: "", |
| 425 | userConfigStr: "", |
| 426 | } as ScriptLoadInfo; |
| 427 | |
| 428 | // 使用 BgExecScriptWarp 执行,它会自动构建 setTimeout/setInterval 等 |
| 429 | const extensionEnv = await this.extensionEnvAsync; |
| 430 | const exec = new BgExecScriptWarp(scriptLoadInfo, this.windowMessage, extensionEnv); |
| 431 | // 通过 sandboxContext 注入 args(BgExecScriptWarp 通过 globalInjection 注入了 setTimeout 等, |
| 432 | // sandboxContext 已经包含了这些,再追加 args 即可) |
| 433 | if ((exec as any).sandboxContext) { |
| 434 | (exec as any).sandboxContext.args = params.args; |
| 435 | (exec as any).sandboxContext.CAT_CONFIG = Object.freeze(params.configValues || {}); |
| 436 | } |
| 437 | |
| 438 | try { |
| 439 | const result = await exec.exec(); |
| 440 | return result; |
| 441 | } finally { |
| 442 | exec.stop(); |
nothing calls this directly
no test coverage detected