MicrosoftEdge / MicrosoftEdge/WebView2Feedback

certain flavor of prototype.js can break web message functionality

Open
#3,021 4 comments 0 reactions 2 assignees View on GitHub

Nobody has claimed this yet.

bug tracked
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Description

Native - JS web messaging is broken on some sites utilizing prototype.js.

Version
SDK: 1.0.1418.22
Runtime: 107.0.1418.62
Framework: WPF
OS: Win10

Repro Steps

I ran into the issue while I was visiting our Jenkins site, but I managed to narrow down the issue and make it reproducible.

  1. Get the Jenkins flavor of prototype.js

  2. Inject the following code - make sure to do it before starting navigation:

     webView2.CoreWebView2.WebMessageReceived += WebMessageReceived;
     // Downloaded from: https://raw.githubusercontent.com/jenkinsci/jenkins/master/war/src/main/webapp/scripts/prototype.js
     var prototypeJS = File.ReadAllText(@"prototype.js");
     await webView2.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(prototypeJS);
     await webView2.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.chrome.webview.postMessage([{\"age\": 21}, {\"age\": 23}]);");
     
     private void WebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e)
     {
         var message = e.WebMessageAsJson;
         try
         {
             var info = System.Text.Json.JsonSerializer.Deserialize<Info[]>(message);
             Debug.WriteLine(info);
         }
         catch (Exception ex)
         {
             Debug.WriteLine(ex.Message);
         }
     }
    
     public class Info
     {
         [JsonPropertyName("age")]
         public int Age { get; set; } 
     }```
    
    
  3. Start the browser - it won't be able to deserialize the web message

Notes:

E.g. see the change in the Array.prototype:

function toJSON() {
  var results = [];
  this.each(function(object) {
   var value = Object.toJSON(object);
   if (value !== undefined) results.push(value);
  });
  return '[' + results.join(', ') + ']';
 }
  • This issue might happen with any sites overriding certain prototypes
  • When prototype.js is injected into the page the e.WebMessageAsJson looks like this (BAD version):
"\"[{\\\"age\\\": 21}, {\\\"age\\\": 23}]\""
  • When prototype.js is NOT injected into the page, e.WebMessageAsJson looks like this (GOOD version):
"[{\"age\":21},{\"age\":23}]"

AB#47984249

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.