MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
441 stars 52 forks source link

Favicon via API #615

Closed dbuechel closed 2 years ago

dbuechel commented 3 years ago

We'd like to propose an extension of the API to easily retrieve the current favicon of a web page (something like https://cefsharp.github.io/api/85.3.x/html/M_CefSharp_IDisplayHandler_OnFaviconUrlChange.htm). Ideally, this API would also handle the case where a web page dynamically changes its favicon over time.

AB#30216493

champnic commented 3 years ago

I've added this request to our backlog. Thanks!

darbid commented 3 years ago

Just landed here to find this one on the feature request list. It would be nice if this was implemented.

jasonstephen15 commented 3 years ago

Hi @dbuechel / @darbid, we are investigating this feature request now. Do both of you mind expanding a bit on the scenario of how you would use such API?

darbid commented 3 years ago

My use case is I save a session history of urls visited. In my list I show the fav icon of the URL. I get the icon when the user visits the url and not when the user later shows the list so I avoid any login or authentication issues.

dbuechel commented 3 years ago

Our use case is that we want to render the favicon as window icon or aside the title of a browser tab, as most browsers nowadays do.

ukandrewc commented 3 years ago

There is this: https://www.google.com/s2/favicons?domain=https://github.com

darbid commented 3 years ago

@ukandrewc that does not work for some sites and as far as I can tell there is no way to detect it did not work as it returns a default icon.. it's my last choice.

Actually GitHub is one that does not work.

ukandrewc commented 3 years ago

Ah yes, I clicked the link, but didn't notice it wasn't GitHub icon ;-)

ExecuteScriptAsync with this, gets the URL or default:

const l=document.evaluate("//link[contains(@rel,'icon')]", document).iterateNext()
if(l==null)
  document.location.protocol+"//"+document.location.host+"/favicon.ico"
else
  x.href
darbid commented 3 years ago

I did a bit of poking around with favicons while looking at building a history list. What I found as a good work around is to follow some code like Microsoft shows in their own examples see the HandleTabNavCompleted line 562 here. The only issue with it is that it looks for rel=icon, but some websites now have a svg resource attached to this element which is a little difficult at least with WPF to deal with. For example look at Github.

<link rel="alternate icon" class="js-site-favicon" type="image/png" href="https://github.githubassets.com/favicons/favicon.png">

<link rel="icon" class="js-site-favicon" type="image/svg+xml" href="https://github.githubassets.com/favicons/favicon.svg">

You need to extend the javascript MS wrote to check for the image type and/or check for rel=alternate icon in the case of Github.

The only issue with this is that you only get a url which you then need to download. This works fine if the website does not require authentication/cookies etc, however if it does then the favicon's url will not work. One use case and reason that Webview2 should have a "download" function like CefSharp/CEF has which automatically adds these kinds or things.

You can see this problem if you use the MS example app above and navigate to a site that requires authentication / login. Then you go and view the history. At this point the app tries to use the favicon urls it harvested during navigation to get the images and if authentication is needed then these windows start popping up.

ukandrewc commented 3 years ago

@darbid Why the 'Actually' attitude ;-( They can also be emdedded base64

darbid commented 3 years ago

@darbid Why the 'Actually' attitude ;-( They can also be emdedded base64

I edited my text.

ukandrewc commented 3 years ago

@darbid Thank you, appreciated ;-)

Tochibee commented 3 years ago

@dbuechel Hey, I've been tasked to look into the implementation of this feature. I do have a question though, could you elaborate on what you mean by "this API would also handle the case where a web page dynamically changes its favicon over time". Do you mean that if the current webpage were to change its favicon while the page is active, that you would be able to get access to the new icon, or something else?

dbuechel commented 3 years ago

@Tochibee Excellent, thanks a lot. Yes indeed, there are web pages which update their favicon to indicate a status change or alike (e.g. a build server once the build is completed or a chat client when a new message has arrived). Ideally, this API would handle that case by firing an event. So for us it would be sufficient if the API were an event which is fired as follows:

Tochibee commented 3 years ago

Ah, that makes sense. thanks for the clarification.

Tochibee commented 3 years ago

Hey @darbid and @dbuechel and anyone else. Is having the favicon URL all that you need, or will it also be necessary/beneficial to return the image itself in addition to the URL?

dbuechel commented 3 years ago

@Tochibee It certainly would be very beneficial to have the image itself as well, at least for us, then we wouldn't need to load it manually ourselves.

darbid commented 3 years ago

@Tochibee the url is a great start but for some urls you would need to add cookies etc. , so I would very much like the image. However as I wrote above if their is no fav icon or your code fails please don't return a default icon.

leonidukg commented 2 years ago

Need to return exactly the finished picture of the site. The connection can work through a proxy and you can't request the picture directly. And you should also store the favicon in the cache, so you don't have to ask the site about it every time.

champnic commented 2 years ago

The CoreWebView2.FaviconChanged event is now available for use in SDKs 1.0.1293.44+. Thanks!