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) } }