How to: Inject CoreWebView2.ExecuteScriptAsync ?

vmars316 621 Reputation points
2020-10-23T18:00:50.363+00:00

Thanks ;

I want to Inject a .jsscript into Navigation Completed Event . 
This is as close as I have gotten so far .

        private void webView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
//            string htmlDoc = await webView.CoreWebView2.ExecuteScriptAsync("document.documentElement.outerHTML");
//            infoBox.Text = htmlDoc;
        }
```Is there a WebView2 example somewhere for this ;   
Pls , how can I do this ?      
Thanks  
Community Center | Not monitored
{count} vote

4 answers

Sort by: Most helpful
  1. Tianyu Sun-MSFT 34,691 Reputation points Microsoft External Staff
    2020-10-26T09:41:35.587+00:00

    Hi Vernon,

    Thank you for posting this issue in Microsoft Q&A forum.

    As you know, the await operator can only be used within an async method, so if you want to use it in private void webView_NavigationCompleted(XXXXX) event, you probably need to add async in the codes, like this private async void webView_NavigationCompleted(XXXXX).

    In addition, I did some tests on my side and please kindly check(refer to) following codes(snippet):

            private async void webView_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)  
            {  
                string html = await webView.CoreWebView2.ExecuteScriptAsync("document.documentElement.outerHTML");  
                var htmldecoded = System.Web.Helpers.Json.Decode(html);             
                richTextBox1.Text = htmldecoded;  
            }  
    

    The result:

    35051-test.gif

    • Update1:

    System.Web.Helpers.XXXX is included in this reference: System.Web.Helpers, please also remember to add System.Web.Helpers reference by right-clicking References > Add Reference… > Assemblies > type system.web.help in the “Search box” and select the System.Web.Helpers reference > OK.

    35302-test2.png

    Sincerely,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

  2. vmars316 621 Reputation points
    2020-10-30T16:06:51.587+00:00

    Sorry to say I get the following messages for all "Microsoft.AspNet.WebPages" I have tried to install :

    This package contains core runtime assemblies shared between ASP.NET MVC and ASP.NET Web Pages.

    Severity Code Description Project File Line Suppression State
    Error CS0234 The type or namespace name 'Helpers' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) WebView2-NavigationCompleted C:\Users\vmars\source\repos\WebView2-NavigationCompleted\Form1.cs 24 Active
    Error CS0234 The type or namespace name 'Helpers' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) WebView2-NavigationCompleted C:\Users\vmars\source\repos\WebView2-NavigationCompleted\Form1.cs 11 Active
    Warning The referenced component 'C:\Users\vmars\source\repos\WebView2-NavigationCompleted\packages\Microsoft.Web.WebView2.1.0.674-prerelease\build....\lib\uap10.0\Microsoft.Web.WebView2.Core.winmd' could not be found. WebView2-NavigationCompleted

    Is there a specific "Microsoft.AspNet.WebPages" I should try to install , there lots of them .
    Thanks


  3. vmars316 621 Reputation points
    2020-11-02T15:51:31.387+00:00

    Sorry to say , still getting same error:

    36901-systemwebhelpers-01.png

    36766-systemwebhelpers-02.png

    36846-systemwebhelpers-03.png

    36797-systemwebhelpers-04.png

    36866-systemwebhelpers-05.png


  4. vmars316 621 Reputation points
    2020-11-07T00:55:28.51+00:00

    Sorry to say , I tried all that you mentioned :

    These are the error msgs when trying to compile with 'PF5' ,
    Because there is no Start Button .

    Severity Code Description Project File Line Suppression State
    Error CS0234 The type or namespace name 'Json' does not exist in the namespace 'System.Web.Helpers' (are you missing an assembly reference?) WebView2-NavigationCompleted C:\Users\vmars\source\repos\WebView2-NavigationCompleted\Form1.cs 24 Active

    Severity Code Description Project File Line Suppression State
    Warning The referenced component 'C:\Users\vmars\source\repos\WebView2-NavigationCompleted\packages\Microsoft.Web.WebView2.1.0.674-prerelease\build....\lib\uap10.0\Microsoft.Web.WebView2.Core.winmd' could not be found. WebView2-NavigationCompleted


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.