question

VernonMarsden-2934 avatar image
1 Vote"
VernonMarsden-2934 asked VernonMarsden-2934 commented

How to: Inject CoreWebView2.ExecuteScriptAsync ?

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

windows-forms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks TianyuSun ;
I had some probs trying to compile the webView_NavigationCompleted .
All lines with 'webView.' in them were flagged with red_underline as errors .
Then my my Form1.cs[Design] went bad , so I deleted the Project ,
and started all over .
Now it compiles clean , but the webView is show , though visible = true .
so I'll Post that as a new Question , then get back to this .


0 Votes 0 ·

Hmm...
When I added Source property 'source = http://www.gogle.com'
the webView showed up at compile/go time .
Anyways , Im getting the red squiggle
under System.Web.Helpers .
I tried adding 'Using System.Web.Helpers' but it got flagged also .
So I am stumped .
How can this be fixed ?
Thanks for your Help...

0 Votes 0 ·

Hi @VernonMarsden-2934,

Thank you for your feedback.

You need to add this reference: System.Web.Helpers by right-clicking References > Add Reference… > Assemblies > type system.web.help in the “Search box” and select the System.Web.Helpers reference > OK.

35311-test2.png

Sincerely,
Tianyu


0 Votes 0 ·
test2.png (15.6 KiB)
TianyuSun-MSFT avatar image
0 Votes"
TianyuSun-MSFT answered TianyuSun-MSFT commented

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.



test.gif (1.0 MiB)
test2.png (15.6 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Tianyu ;
I get a search message that says:
"No items found for search term(s): System.Web.Helpers" .
How to fix ?
Thanks

0 Votes 0 ·

Hi @VernonMarsden-2934, if you go to this path C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies or C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\XXXXX\xxxxx will you see the System.Web.helpers.dll?

If you can find this dll file, then please do the same steps as I mentioned above but this time click Browse… button on Reference Manager window > go to the path and select the System.Web.helpers.dll file > Add.

If this dll file doesn’t exist, please try to install this NuGet package: Microsoft.AspNet.WebPages and check if this works.

0 Votes 0 ·
VernonMarsden-2934 avatar image
0 Votes"
VernonMarsden-2934 answered TianyuSun-MSFT edited

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I do find "System.Web.Helpers.dll" here :
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45
Does that help ?

0 Votes 0 ·

Yes, you can directly browse and add this dll file System.Web.Helpers.dll from the path that you mentioned above: C:\Program Files (x86)\Microsoft Visual Studio\Shared\Packages\Microsoft.AspNet.WebPages.3.2.7\lib\net45. Feel free to let me know if the errors still appear.
36736-1.png


0 Votes 0 ·
1.png (48.5 KiB)
VernonMarsden-2934 avatar image
0 Votes"
VernonMarsden-2934 answered TianyuSun-MSFT commented

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


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @VernonMarsden-2934, thank you for your feedback. Generally this will work, to narrow down this issue, could you try to create a new windows form application and add reference of System.Web.Helpers, after that type codes for example var str=System.Web.Helpers.Json.Decode(""); and check if this works.

0 Votes 0 ·

If the error still appears in newly created project, then this issue may be caused by the broken of dll or framework... So I suggest you try to repair Visual Studio, and to avoid the impact from other extensions, please launch VS > Extensions > Manage Extensions > Installed > Disable.

If the newly created project works well, the issue may be caused by the project(old) itself, or some packages and other references. So I suggest you go to solution(project) folder and delete the hidden .vs, bin and obj folders, then rebuild your solution and check again.

Looking forward to hearing from you.

0 Votes 0 ·
VernonMarsden-2934 avatar image
0 Votes"
VernonMarsden-2934 answered VernonMarsden-2934 commented

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

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @VernonMarsden-2934, as above suggestions don’t work(this error appears in newly created project), and if this issue block you for a long time, I may suggest you try to reinstall Visual Studio 2019 from Visual Studio Installer.

0 Votes 0 ·

I downloaded and re-installed VS
without UNinstalling first
is that ok?
Then I had to run Modify to add workloads
because install didn't ask for them .
Under Solution References it shows System.Web.helpers .
But Still getting same bad results .

Ouch !

Anyway you could make your project available to me .

0 Votes 0 ·