MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / ScriptService

Class ScriptService

src/app/service/service_worker/script.ts:71–1534  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

69};
70
71export class ScriptService {
72 logger: Logger;
73 scriptCodeDAO: ScriptCodeDAO = new ScriptCodeDAO();
74 localStorageDAO: LocalStorageDAO = new LocalStorageDAO();
75 compiledResourceDAO: CompiledResourceDAO = new CompiledResourceDAO();
76 private readonly scriptUpdateCheck;
77
78 constructor(
79 private readonly systemConfig: SystemConfig,
80 private readonly group: Group,
81 private readonly mq: IMessageQueue,
82 private readonly valueService: ValueService,
83 private readonly resourceService: ResourceService,
84 private readonly scriptDAO: ScriptDAO
85 ) {
86 this.logger = LoggerCore.logger().with({ service: "script" });
87 this.scriptCodeDAO.enableCache();
88 this.scriptUpdateCheck = new ScriptUpdateCheck(systemConfig, group, mq, valueService, resourceService, scriptDAO);
89 }
90
91 listenerScriptInstall() {
92 // 初始化脚本安装监听
93 chrome.webNavigation.onBeforeNavigate.addListener(
94 (req: chrome.webNavigation.WebNavigationBaseCallbackDetails) => {
95 const lastError = chrome.runtime.lastError;
96 if (lastError) {
97 console.error("chrome.runtime.lastError in chrome.webNavigation.onBeforeNavigate:", lastError);
98 return;
99 }
100 // 处理url, 实现安装脚本
101 let targetUrl: string;
102 // 判断是否为 file:///*/*.user.js 或 file:///*/*.skill.js(skill 仅 agent 启用时)
103 if (
104 req.url.startsWith("file://") &&
105 (req.url.endsWith(".user.js") || (EnableAgent && req.url.endsWith(".skill.js")))
106 ) {
107 targetUrl = req.url;
108 } else {
109 const reqUrl = new URL(req.url);
110 // 判断是否有hash
111 if (!reqUrl.hash) {
112 return undefined;
113 }
114 // 判断是否有url参数
115 const idx = reqUrl.hash.indexOf("url=");
116 if (idx < 0) {
117 return undefined;
118 }
119 // 获取url参数
120 targetUrl = reqUrl.hash.substring(idx + 4);
121 }
122 // 读取脚本url内容, 进行安装
123 const logger = this.logger.with({ url: targetUrl });
124 logger.debug("install script");
125 this.openInstallPageByUrl(targetUrl, { source: "user", byWebRequest: true })
126 .catch((e) => {
127 logger.error("install script error", Logger.E(e));
128 // 不再重定向当前url

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected