[NUI] Add an API for evaluating js with callback in WebView.
authorhuayong.xu <huayong.xu@samsung.com>
Fri, 11 Feb 2022 08:22:22 +0000 (16:22 +0800)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Fri, 11 Mar 2022 01:49:26 +0000 (10:49 +0900)
src/Tizen.NUI/src/public/WebView/WebView.cs
test/Tizen.NUI.WebViewTest/SimpleWebViewApp.cs

index d67e68a..f21a44f 100755 (executable)
@@ -1758,6 +1758,19 @@ namespace Tizen.NUI.BaseComponents
         }
 
         /// <summary>
+        /// Evaluates JavaScript code represented as a string.
+        /// </summary>
+        /// <param name="script">The JavaScript code</param>
+        /// <param name="handler">The callback for result of JavaScript code evaluation</param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public void EvaluateJavaScript(string script, JavaScriptMessageHandler handler)
+        {
+            System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(handler);
+            Interop.WebView.EvaluateJavaScript(SwigCPtr, script, new global::System.Runtime.InteropServices.HandleRef(this, ip));
+            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+        }
+
+        /// <summary>
         /// Add a message handler into the WebView.
         /// </summary>
         /// <param name="objectName">The name of exposed object</param>
index 50276fb..e052fae 100755 (executable)
@@ -641,6 +641,11 @@ namespace Tizen.NUI.WebViewTest
             }
         }
 
+        private void OnJavaScriptEvaluated(string message)
+        {
+            Log.Info("WebView", $"------------javascript evaluation result: {message}-------");
+        }
+
         private void OnJavaScriptAlert(string message)
         {
             Log.Info("WebView", $"------------javascript alert {message}-------");
@@ -850,6 +855,7 @@ namespace Tizen.NUI.WebViewTest
                         //simpleWebView.ClearAllTilesResources();
                         //simpleWebView.ClearHistory();
                         simpleWebView.EvaluateJavaScript("document.body.style.backgroundColor='yellow';");
+                        simpleWebView.EvaluateJavaScript("document.body.innerHTML", OnJavaScriptEvaluated);
                         Log.Info("WebView", $"web view, ScaleFactor is {simpleWebView.GetScaleFactor()}");
 
                         //Rectangle viewArea = new Rectangle(0, 0, 20, 20);