Xam.Plugin.Webview
Xam.Plugin.Webview copied to clipboard
Fix issue with window.btoa(str) only supporting ASCII / Latin1 character set
Problem: Exceptions when passing string data that contains an extended Unicode character set via JS callbacks, due to the limitation of window.btoa(str) which can only convert ASCII / Latin1 strings to base64.
Solution: Replaced JS window.btoa(str) with JSON.stringify(obj) that serializes the JS object into a string. This allows the full Unicode character set to be used within the JSON. The parsed JSON (JToken) is passed around in the Xamarin code. A JToken is versatile and dynamic - it can be converted to a JSON string, can be accessed like a dictionary or converted to a C# POCO object using JToken.ToObject<T>().
Simpler solution.
internal static string GenerateFunctionScript(string name) { return $"function {name}(str){{csharp(\"{{'action':'{name}','data':'\"+window.btoa(unescape(encodeURIComponent(str)))+\"'}}\");}}"; }