Xam.Plugin.Webview icon indicating copy to clipboard operation
Xam.Plugin.Webview copied to clipboard

Fix issue with window.btoa(str) only supporting ASCII / Latin1 character set

Open roygoode opened this issue 7 years ago • 1 comments

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>().

roygoode avatar May 15 '18 18:05 roygoode

Simpler solution.

    internal static string GenerateFunctionScript(string name)
   {
       return $"function {name}(str){{csharp(\"{{'action':'{name}','data':'\"+window.btoa(unescape(encodeURIComponent(str)))+\"'}}\");}}";
   }

drmashu avatar Mar 19 '19 03:03 drmashu