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

Function createContext

src/app/service/content/create_context.ts:15–113  ·  view source on GitHub ↗
(
  scriptRes: TScriptInfo,
  GMInfo: any,
  envPrefix: string,
  message: Message,
  contentMsg: Message,
  scriptGrants: Set<string>
)

Source from the content-addressed store, hash-verified

13
14// 构建沙盒上下文
15export const createContext = (
16 scriptRes: TScriptInfo,
17 GMInfo: any,
18 envPrefix: string,
19 message: Message,
20 contentMsg: Message,
21 scriptGrants: Set<string>
22) => {
23 // 按照GMApi构建
24 const valueChangeListener = new ListenerManager<GMTypes.ValueChangeListener>();
25 const EE = new EventEmitter<string, any>();
26 // 如果是preDocumentStart脚本,装载loadScriptPromise
27 let loadScriptPromise: Promise<void> | undefined;
28 let loadScriptResolve: (() => void) | undefined;
29 if (isEarlyStartScript(scriptRes.metadata)) {
30 loadScriptPromise = new Promise((resolve) => {
31 loadScriptResolve = resolve;
32 });
33 }
34 let invalid = false;
35 const GM = Object.create(null);
36 GM.info = GMInfo;
37 const context = createGMBase({
38 prefix: envPrefix,
39 message,
40 contentMsg,
41 scriptRes,
42 valueChangeListener,
43 EE,
44 runFlag: uuidv4(),
45 eventId: 10000,
46 GM: GM,
47 GM_info: GMInfo,
48 window: Object.create(null),
49 grantSet: new Set(),
50 loadScriptPromise,
51 loadScriptResolve,
52 setInvalidContext() {
53 if (invalid) return;
54 invalid = true;
55 this.valueChangeListener.clear();
56 this.EE.removeAllListeners();
57 this.runFlag = `${uuidv4()}(invalid)`; // 更改 uuid 防止 runFlag 相关操作
58 // 释放记忆
59 this.message = null;
60 this.scriptRes = null;
61 this.valueChangeListener = null;
62 this.EE = null;
63 },
64 isInvalidContext() {
65 return invalid;
66 },
67 });
68 const grantedAPIs: { [key: string]: any } = Object.create(null);
69 const __methodInject__ = (grant: string): boolean => {
70 const grantSet: Set<string> = context.grantSet;
71 const s = GMContextApiGet(grant);
72 if (!s) return false; // @grant 的定义未实现,略过 (返回 false 表示 @grant 不存在)

Callers 6

constructorMethod · 0.90
createTestContextFunction · 0.90
AppContext.tsxFile · 0.85
ScriptTable.tsxFile · 0.85
ScriptCard.tsxFile · 0.85

Calls 6

isEarlyStartScriptFunction · 0.90
attachNavigateHandlerFunction · 0.90
__methodInject__Function · 0.85
keysMethod · 0.80
createMethod · 0.65
hasMethod · 0.65

Tested by 1

createTestContextFunction · 0.72