예제 #1
0
        /// <summary>
        ///     Dispatch a <see cref="JavaScriptBridgeMessage" />.
        /// </summary>
        /// <param name="webView">A <see cref="IWebView" /> instance.</param>
        /// <param name="javaScriptBridgeMessage">The message.</param>
        internal static void DispatchMessage(this IWebView webView, JavaScriptBridgeMessage javaScriptBridgeMessage)
        {
            var json = javaScriptBridgeMessage?.Serialize();

            if (!string.IsNullOrEmpty(json))
            {
                Trace.WriteLine(
                    $"Calling {Constants.BridgeJavaScript} JavaScriptBridge.handleNativeMessage",
                    Constants.TraceName);
                Trace.WriteLine(javaScriptBridgeMessage, Constants.TraceName);

                // Need to sanitize message sent to bridge

                // Escape any single quotes, since that's what we're using
                json = json.Replace("'", "\\'");

                // Escape any CL/LF
                json = json.Replace(Environment.NewLine, "\\\\n");

                json = json.Replace("\n", "\\\\n");

                var command = $"JavaScriptBridge.handleNativeMessage('{json}');";
                // in WPF ensure UI access
                webView.TryEvalAsync(command);
            }
        }
예제 #2
0
 /// <summary>
 /// Serializes the specified <paramref name="javaScriptBridgeMessage"/> to JSON.
 /// </summary>
 /// <param name="javaScriptBridgeMessage">A <see cref="JavaScriptBridgeMessage"/>.</param>
 /// <returns>JSON representation of the <paramref name="javaScriptBridgeMessage"/></returns>
 public static string Serialize(this JavaScriptBridgeMessage javaScriptBridgeMessage)
 {
     return(JsonConvert.SerializeObject(javaScriptBridgeMessage, MessageSerializationSettings));
 }