fix(web): Fetch favicon from root domain before trying Google

This commit is contained in:
Michael Aaron Murphy 2022-03-29 23:47:01 +02:00 • committed by Michael Murphy
parent 60cd2e7154
commit 4d88ce451e

View file

@ -149,19 +149,23 @@ impl App {
|url: String| async move { fetch_favicon(&url, favicon_path, client).await }; |url: String| async move { fetch_favicon(&url, favicon_path, client).await };
// Searches for the favicon if it's not defined at the root of the domain. // Searches for the favicon if it's not defined at the root of the domain.
let mut result = fetch( let result = match favicon_from_page(&domain, client).await {
favicon_url_from_page_source(&domain, client) Some(url) => fetch(url).await,
.await
.unwrap_or_else(|| {
format!("https://www.google.com/s2/favicons?domain={}&sz=32", domain)
}),
)
.await;
if result.is_none() { // If not found, fetch from root domain.
// Searches for the favicon from the root of the domain. None => match fetch(["https://", &domain, "/favicon.ico"].concat()).await {
result = fetch(["https://", &domain, "/favicon.ico"].concat()).await; Some(favicon) => Some(favicon),
}
// If all else fails, try Google.
None => {
fetch(format!(
"https://www.google.com/s2/favicons?domain={}&sz=32",
domain
))
.await
}
},
};
match result { match result {
Some(icon) => { Some(icon) => {
@ -229,7 +233,7 @@ async fn fetch_favicon(url: &str, favicon_path: &Path, client: &Client) -> Optio
// Try to extract a favicon url from html the icon path // Try to extract a favicon url from html the icon path
// returned can be either absolute or relative to the page domain // returned can be either absolute or relative to the page domain
async fn favicon_url_from_page_source(domain: &str, client: &Client) -> Option<String> { async fn favicon_from_page(domain: &str, client: &Client) -> Option<String> {
let url = format!("https://{}", domain); let url = format!("https://{}", domain);
match client.get(&url).send().await { match client.get(&url).send().await {
Ok(html) => html Ok(html) => html