MCPcopy Create free account
hub / github.com/scriptscat/scriptcat / compilePreInjectScript

Function compilePreInjectScript

src/app/service/content/utils.ts:224–246  ·  view source on GitHub ↗
(
  script: ScriptLoadInfo,
  scriptCode: string,
  autoDeleteMountFunction: boolean = false
)

Source from the content-addressed store, hash-verified

222 * 将脚本函数编译为预注入脚本代码
223 */
224export function compilePreInjectScript(
225 script: ScriptLoadInfo,
226 scriptCode: string,
227 autoDeleteMountFunction: boolean = false
228): string {
229 const scriptEnvTag = isInjectIntoContent(script.metadata) ? ScriptEnvTag.content : ScriptEnvTag.inject;
230 const eventNamePrefix = `evt${process.env.SC_RANDOM_KEY}.${scriptEnvTag}`; // 仅用于early-start初始化
231 const flag = `${script.flag}`;
232 const scriptInfo = trimScriptInfo(script);
233 const scriptInfoJSON = `${JSON.stringify(scriptInfo)}`;
234 const autoDeleteMountCode = autoDeleteMountFunction ? `try{delete window['${flag}']}catch(e){}` : "";
235 const evScriptLoad = `${eventNamePrefix}${DefinedFlags.scriptLoadComplete}`;
236 const evEnvLoad = `${eventNamePrefix}${DefinedFlags.envLoadComplete}`;
237 return `window['${flag}'] = function(){${autoDeleteMountCode}${scriptCode}};
238{
239 let o = { cancelable: true, detail: { scriptFlag: '${flag}', scriptInfo: (${scriptInfoJSON}) } },
240 c = typeof cloneInto === "function" ? cloneInto(o, performance) : o,
241 f = () => performance.dispatchEvent(new CustomEvent('${evScriptLoad}', c)),
242 needWait = f();
243 if (needWait) performance.addEventListener('${evEnvLoad}', f, { once: true });
244}
245`;
246}
247
248export function addStyle(css: string): HTMLStyleElement {
249 const dom = document.createElement("style");

Callers 1

compileInjectionCodeFunction · 0.90

Calls 2

isInjectIntoContentFunction · 0.85
trimScriptInfoFunction · 0.85

Tested by

no test coverage detected