gree / unity-webview

zlib License
2.3k stars 691 forks source link

EvaluateJS does not work on Android #652

Open Sakura777 opened 3 years ago

Sakura777 commented 3 years ago

EvaluateJS does not work on Android. It works fine on iOS and Unity editor, and it works normally except for EvaluateJS on Android.

The code for testing is below. This does not work and the specified web page is displayed as is. webView.EvaluateJS ( "window.onload = function () {window.document.body.style.backgroundColor = 'black';};" ); Do you need any special settings?

Unity 2019.4.2f1, Android OS 10 Installation is done from Package Manager, and the version notation there is 1.0.0.

KojiNakamaru commented 3 years ago

Your code utilizes windows.onload which could be canceled in loading a web page. In order to avoid the issue, you should call EvaluateJS() in the ld: callback or at any other timing after the page is loaded. For example, the following modification for the sample app should work.

diff --git a/sample/Assets/Scripts/SampleWebView.cs b/sample/Assets/Scripts/SampleWebView.cs
index c060211..20ca61c 100644
--- a/sample/Assets/Scripts/SampleWebView.cs
+++ b/sample/Assets/Scripts/SampleWebView.cs
@@ -108,6 +108,7 @@ public class SampleWebView : MonoBehaviour
                     "};");
 #endif
                 webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)");
+                webViewObject.EvaluateJS(@"window.document.body.style.backgroundColor = 'black';");
             },
             //ua: "custom user agent string",
 #if UNITY_EDITOR
Sakura777 commented 3 years ago

Thank you for your reply. This worked fine.