gree / unity-webview

zlib License
2.26k stars 687 forks source link

Adding Post Message Support on WebGL for Cross-Origin Comms #1088

Open MoizAhmedd opened 1 month ago

MoizAhmedd commented 1 month ago

Currently, the only way to communicate with the webview on WebGL, is using EvaluateJS

This is problematic, because running.eval on the iframe that the webview creates, is blocked by browser when applied on cross-origin frames.

A safe and common way to get around this is to post messages to the iframe instead.

It would be great for the WebGL version to have support for post message, so that communication between game and cross-origin frames can occur

KojiNakamaru commented 1 month ago

I pushed a modified sample app to introduce postMessage:

https://github.com/gree/unity-webview/commits/example-for-1088/

As we can see in https://github.com/gree/unity-webview/commit/40aba92f1b6788c852732bf176af76c539fd7aae, postMessage requires addEventListener in the iframe content, so it is not a complete alternative. Anyway, as shown here, adding postMessage is easy and might be adequate if you can edit the iframe content.

MoizAhmedd commented 1 month ago

looks pretty good, will test this out

edit: so from what I understand, we'll still need separate logic to handle messages received/it won't trigger the callback in webviewObject.init callback?

KojiNakamaru commented 1 month ago

Yes, so I wrote "not a complete alternative."

KojiNakamaru commented 1 month ago

Sorry that the last comment was a bit too brief and didn't answer about the callback specifically. I've further updated the branch so that it can be directly utilized. Could you please check the latest.