[NUI] Add an API for evaluating js with callback in WebView.
authorhuayong.xu <huayong.xu@samsung.com>
Wed, 16 Feb 2022 05:11:28 +0000 (13:11 +0800)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Thu, 24 Feb 2022 02:45:01 +0000 (11:45 +0900)
src/Tizen.NUI/src/public/WebView/WebView.cs
test/Tizen.NUI.WebViewTest/SimpleWebViewApp.cs

index 953cdd3..95014d9 100755 (executable)
@@ -1642,6 +1642,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 9b931bb..1619947 100755 (executable)
@@ -595,6 +595,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}-------");
@@ -808,6 +813,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);