Repository navigation
Expand file tree
/
Copy pathtest-inject-script.js
More file actions
314 lines (298 loc) · 8.66 KB
/
Copy pathtest-inject-script.js
File metadata and controls
314 lines (298 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
(() => {
const SCRIPT_ID = 'excalidraw-control-script';
if (window[SCRIPT_ID]) {
return;
}
function getExcalidrawAPIFromDOM(domElement) {
if (!domElement) {
return null;
}
const reactFiberKey = Object.keys(domElement).find(
(key) => key.startsWith('__reactFiber$') || key.startsWith('__reactInternalInstance$'),
);
if (!reactFiberKey) {
return null;
}
let fiberNode = domElement[reactFiberKey];
if (!fiberNode) {
return null;
}
function isExcalidrawAPI(obj) {
return (
typeof obj === 'object' &&
obj !== null &&
typeof obj.updateScene === 'function' &&
typeof obj.getSceneElements === 'function' &&
typeof obj.getAppState === 'function'
);
}
function findApiInObject(objToSearch) {
if (isExcalidrawAPI(objToSearch)) {
return objToSearch;
}
if (typeof objToSearch === 'object' && objToSearch !== null) {
for (const key in objToSearch) {
if (Object.prototype.hasOwnProperty.call(objToSearch, key)) {
const found = findApiInObject(objToSearch[key]);
if (found) {
return found;
}
}
}
}
return null;
}
let excalidrawApiInstance = null;
let attempts = 0;
const MAX_TRAVERSAL_ATTEMPTS = 25;
while (fiberNode && attempts < MAX_TRAVERSAL_ATTEMPTS) {
if (fiberNode.stateNode && fiberNode.stateNode.props) {
const api = findApiInObject(fiberNode.stateNode.props);
if (api) {
excalidrawApiInstance = api;
break;
}
if (isExcalidrawAPI(fiberNode.stateNode.props.excalidrawAPI)) {
excalidrawApiInstance = fiberNode.stateNode.props.excalidrawAPI;
break;
}
}
if (fiberNode.memoizedProps) {
const api = findApiInObject(fiberNode.memoizedProps);
if (api) {
excalidrawApiInstance = api;
break;
}
if (isExcalidrawAPI(fiberNode.memoizedProps.excalidrawAPI)) {
excalidrawApiInstance = fiberNode.memoizedProps.excalidrawAPI;
break;
}
}
if (fiberNode.tag === 1 && fiberNode.stateNode && fiberNode.stateNode.state) {
const api = findApiInObject(fiberNode.stateNode.state);
if (api) {
excalidrawApiInstance = api;
break;
}
}
if (
fiberNode.tag === 0 ||
fiberNode.tag === 2 ||
fiberNode.tag === 14 ||
fiberNode.tag === 15 ||
fiberNode.tag === 11
) {
if (fiberNode.memoizedState) {
let currentHook = fiberNode.memoizedState;
let hookAttempts = 0;
const MAX_HOOK_ATTEMPTS = 15;
while (currentHook && hookAttempts < MAX_HOOK_ATTEMPTS) {
const api = findApiInObject(currentHook.memoizedState);
if (api) {
excalidrawApiInstance = api;
break;
}
currentHook = currentHook.next;
hookAttempts++;
}
if (excalidrawApiInstance) break;
}
}
if (fiberNode.stateNode) {
const api = findApiInObject(fiberNode.stateNode);
if (api && api !== fiberNode.stateNode.props && api !== fiberNode.stateNode.state) {
excalidrawApiInstance = api;
break;
}
}
if (
fiberNode.tag === 9 &&
fiberNode.memoizedProps &&
typeof fiberNode.memoizedProps.value !== 'undefined'
) {
const api = findApiInObject(fiberNode.memoizedProps.value);
if (api) {
excalidrawApiInstance = api;
break;
}
}
if (fiberNode.return) {
fiberNode = fiberNode.return;
} else {
break;
}
attempts++;
}
if (excalidrawApiInstance) {
window.excalidrawAPI = excalidrawApiInstance;
console.log('现在您可以通过 `window.foundExcalidrawAPI` 在控制台访问它。');
} else {
console.error('在检查组件树后未能找到 excalidrawAPI。');
}
return excalidrawApiInstance;
}
function createFullExcalidrawElement(skeleton) {
const id = Math.random().toString(36).substring(2, 9);
const seed = Math.floor(Math.random() * 2 ** 31);
const versionNonce = Math.floor(Math.random() * 2 ** 31);
const defaults = {
isDeleted: false,
fillStyle: 'hachure',
strokeWidth: 1,
strokeStyle: 'solid',
roughness: 1,
opacity: 100,
angle: 0,
groupIds: [],
strokeColor: '#000000',
backgroundColor: 'transparent',
version: 1,
locked: false,
};
const fullElement = {
id: id,
seed: seed,
versionNonce: versionNonce,
updated: Date.now(),
...defaults,
...skeleton,
};
return fullElement;
}
let targetElementForAPI = document.querySelector('.excalidraw-app');
if (targetElementForAPI) {
getExcalidrawAPIFromDOM(targetElementForAPI);
}
const eventHandler = {
getSceneElements: () => {
try {
return window.excalidrawAPI.getSceneElements();
} catch (error) {
return {
error: true,
msg: JSON.stringify(error),
};
}
},
addElement: (param) => {
try {
const existingElements = window.excalidrawAPI.getSceneElements();
const newElements = [...existingElements];
param.eles.forEach((ele, idx) => {
const newEle = createFullExcalidrawElement(ele);
newEle.index = `a${existingElements.length + idx + 1}`;
newElements.push(newEle);
});
console.log('newElements ==>', newElements);
const appState = window.excalidrawAPI.getAppState();
window.excalidrawAPI.updateScene({
elements: newElements,
appState: appState,
commitToHistory: true,
});
return {
success: true,
};
} catch (error) {
return {
error: true,
msg: JSON.stringify(error),
};
}
},
deleteElement: (param) => {
try {
const existingElements = window.excalidrawAPI.getSceneElements();
const newElements = [...existingElements];
const idx = newElements.findIndex((e) => e.id === param.id);
if (idx >= 0) {
newElements.splice(idx, 1);
const appState = window.excalidrawAPI.getAppState();
window.excalidrawAPI.updateScene({
elements: newElements,
appState: appState,
commitToHistory: true,
});
return {
success: true,
};
} else {
return {
error: true,
msg: 'element not found',
};
}
} catch (error) {
return {
error: true,
msg: JSON.stringify(error),
};
}
},
updateElement: (param) => {
try {
const existingElements = window.excalidrawAPI.getSceneElements();
const resIds = [];
for (let i = 0; i < param.length; i++) {
const idx = existingElements.findIndex((e) => e.id === param[i].id);
if (idx >= 0) {
resIds.push[idx];
window.excalidrawAPI.mutateElement(existingElements[idx], { ...param[i] });
}
}
return {
success: true,
msg: `已更新元素:${resIds.join(',')}`,
};
} catch (error) {
return {
error: true,
msg: JSON.stringify(error),
};
}
},
cleanup: () => {
try {
window.excalidrawAPI.resetScene();
return {
success: true,
};
} catch (error) {
return {
error: true,
msg: JSON.stringify(error),
};
}
},
};
const handleExecution = (event) => {
const { action, payload, requestId } = event.detail;
const param = JSON.parse(payload || '{}');
let data, error;
try {
const handler = eventHandler[action];
if (!handler) {
error = 'event name not found';
}
data = handler(param);
} catch (e) {
error = e.message;
}
window.dispatchEvent(
new CustomEvent('chrome-mcp:response', { detail: { requestId, data, error } }),
);
};
// --- Lifecycle Functions ---
const initialize = () => {
window.addEventListener('chrome-mcp:execute', handleExecution);
window.addEventListener('chrome-mcp:cleanup', cleanup);
window[SCRIPT_ID] = true;
};
const cleanup = () => {
window.removeEventListener('chrome-mcp:execute', handleExecution);
window.removeEventListener('chrome-mcp:cleanup', cleanup);
delete window[SCRIPT_ID];
delete window.excalidrawAPI;
};
initialize();
})();