From a89c7106be1323e835cfd56c6c9fad72e0a3c36c Mon Sep 17 00:00:00 2001 From: lareinayanyu Date: Thu, 26 Dec 2024 17:21:39 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20canvas=E9=80=9A=E4=BF=A1=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/runtime/components/react/mpx-canvas/html.ts | 6 ++---- .../lib/runtime/components/react/mpx-canvas/index.tsx | 6 +++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/webpack-plugin/lib/runtime/components/react/mpx-canvas/html.ts b/packages/webpack-plugin/lib/runtime/components/react/mpx-canvas/html.ts index c8a3a8140..e8d6bdbdf 100644 --- a/packages/webpack-plugin/lib/runtime/components/react/mpx-canvas/html.ts +++ b/packages/webpack-plugin/lib/runtime/components/react/mpx-canvas/html.ts @@ -316,8 +316,7 @@ var handleError = function (err, message) { document.removeEventListener('message', handleIncomingMessage); }; -function handleIncomingMessage(e) { - var data = JSON.parse(e.data); +function handleIncomingMessage(data) { if (Array.isArray(data)) { for (var i = 0; i < data.length; i++) { try { @@ -335,8 +334,7 @@ function handleIncomingMessage(e) { } } -window.addEventListener('message', handleIncomingMessage); -document.addEventListener('message', handleIncomingMessage); +window.mpxWebviewMessageCallback = handleIncomingMessage diff --git a/packages/webpack-plugin/lib/runtime/components/react/mpx-canvas/index.tsx b/packages/webpack-plugin/lib/runtime/components/react/mpx-canvas/index.tsx index b5b31cc3d..3c56abde3 100644 --- a/packages/webpack-plugin/lib/runtime/components/react/mpx-canvas/index.tsx +++ b/packages/webpack-plugin/lib/runtime/components/react/mpx-canvas/index.tsx @@ -106,7 +106,11 @@ const _Canvas = forwardRef, CanvasPr useEffect(() => { const webviewPostMessage = (message: WebviewMessage) => { if (canvasRef.current.webview) { - canvasRef.current.webview.postMessage(JSON.stringify(message)) + const jsCode = ` + window.mpxWebviewMessageCallback(${JSON.stringify(message)}); + true; + ` + canvasRef.current.webview.injectJavaScript(jsCode) } }