From cf263ded5412e9560938feb53d390e2586670665 Mon Sep 17 00:00:00 2001 From: hangwin Date: Tue, 17 Jun 2025 17:55:32 +0800 Subject: [PATCH] Update excalidraw-prompt.md --- prompt/excalidraw-prompt.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/prompt/excalidraw-prompt.md b/prompt/excalidraw-prompt.md index 8c1e3b3..3ff707a 100644 --- a/prompt/excalidraw-prompt.md +++ b/prompt/excalidraw-prompt.md @@ -232,5 +232,6 @@ 9. **禁止使用截图工具** ## 需要注入的脚本 - -(()=>{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{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;ie.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}}),)};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()})(); +```javascript +(()=>{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{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;ie.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}}),)};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()})(); +```