WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

Trap anchor tags

Open NeilHayes opened this issue 3 years ago • 8 comments

Using CoreWebview2 to replace the IE ocx control, I have the need to trap when an anchor tag has been clicked on. I beleive in IE this was done using BeforeNavigate2 event but I do not see a direct replacement.

For example:

<a href='#Code123'>Run code 123 </a>
<a href='#Code124'>Run code 124 </a>
<a href='#Code125'>Run code 125 </a>

In this case these sections #Code123 doesn't exist in the html but where caught in the BeforeNavigate2 event handler and runs some other abitary code which is the requirement.

I thought similar could be done by using (Language C#):

               webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
               webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;

But the event CoreWebView2_WebResourceRequested doesn't event get fired. It gets fired when there is a valid hyperlink or leaving out the #.

Their is no javascript or id's in these HTML files. These HTML files are already deployed to end users and are not easily replaced.

How do I resolve this. I'm currently using build 1.0.1072.54 or Microsoft.Web.WebVew2.

AB#38370120

NeilHayes avatar Feb 24 '22 12:02 NeilHayes

Hey @NeilHayes - Could you try using the NavigationStarting event and see if that works here? Let me know if that doesn't work. Thanks!

champnic avatar Mar 01 '22 01:03 champnic

No the browser naviagtion event doesn't fire either. In my case I found the actual Anchor Tags where in an iFrame, so currently the only way I have found to get it to work is to add Javascript code to all the html files. From the files in the iFrame post a message to the parent, the parent catch the message and post that message on.....trouble is I don't own the html files.

NeilHayes avatar Mar 01 '22 11:03 NeilHayes

There's also a CoreWebView2.FrameNavigationStarting event. Does that work?

champnic avatar Mar 02 '22 06:03 champnic

Here are 3 basic files

  1. Call it menu.htm
<html>
<head>
<meta http-equiv=Content-Type content='text/html;'>
<title>Options</title>
</head>
<frameset cols='216,1*'>
<frame name=LeftFrame src=TOCFrame.htm>
<frame name=MainFrame src='Test1.htm'>
</frameset>
</html>
  1. Call it TOCFrame.htm
<html><head><base target=MainFrame>
 <title>TOC</title>
</style>
</head>
<body>
<div >
<h3>Main Options</h3>
<a href=Test1.htm>
Put out the fire
</div></body></html>
  1. Call it Test1.htm
<html xmlns:v='urn:schemas-microsoft-com:vml'>
<head>
</head>
<body>
<h1><a name='_top'></a></h1>
<a href='#WATER'>Water</a>
</body>
  1. All I need to trap is #WATER was clicked on

The CoreWebView2.FrameNavigationStarting event does not trap that. Reminder : I don't own the html files, I can't change them.

NeilHayes avatar Mar 02 '22 09:03 NeilHayes

I've confirmed that NavigationStarting never fires for anchor tags. I'm opening this as a bug on our backlog. Thanks!

champnic avatar Mar 02 '22 18:03 champnic

Is there a workaround for intercepting anchor tags

lockieluke avatar Apr 08 '25 22:04 lockieluke

We couldn't wait for the bug fix, so we redesigned.

NeilHayes avatar Apr 09 '25 04:04 NeilHayes

might have to switch to CEF now

lockieluke avatar Apr 10 '25 11:04 lockieluke