Add generic www pattern to open any website (#156)
* Add generic `www` pattern to open any website Opens a website in default browser, e.g. `www github.com` * fix(web): Ignore invalid URLs rather than panicking * Improved syntax for early return * Allow fetching icon even if query has no domain * Add icon for www web search query; fix whitespace * Default to https for www web plugin shortcut
This commit is contained in:
parent
643e39bd1b
commit
a12c131c26
2 changed files with 40 additions and 27 deletions
|
|
@ -130,17 +130,17 @@ impl App {
|
|||
async fn fetch_icon_in_background(&self, def: &Definition, favicon_path: &Path) {
|
||||
let client = self.client.clone();
|
||||
|
||||
let url = build_query(def, "");
|
||||
let url = Url::parse(&url).expect("invalid url");
|
||||
let icon_source = def.icon.clone();
|
||||
|
||||
let domain = url
|
||||
.domain()
|
||||
.map(|domain| domain.to_string())
|
||||
.expect("url have no domain");
|
||||
|
||||
let favicon_path = favicon_path.to_path_buf();
|
||||
|
||||
let query = build_query(def, "");
|
||||
let domain = Url::parse(&query).ok().map(|url| {
|
||||
url.domain()
|
||||
.map(|d| d.to_string())
|
||||
.expect("url has no domain")
|
||||
});
|
||||
|
||||
tokio::spawn(async move {
|
||||
let client = &client;
|
||||
let favicon_path = &favicon_path;
|
||||
|
|
@ -155,19 +155,24 @@ impl App {
|
|||
Some(icon_source)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|url| fetch(url)),
|
||||
// Searches for the favicon if it's not defined at the root of the domain.
|
||||
favicon_from_page(&domain, client)
|
||||
.await
|
||||
.map(|url| fetch(url)),
|
||||
// If not found, fetch from root domain.
|
||||
Some(fetch(["https://", &domain, "/favicon.ico"].concat())),
|
||||
// If all else fails, try Google.
|
||||
Some(fetch(format!(
|
||||
"https://www.google.com/s2/favicons?domain={}&sz=32",
|
||||
domain
|
||||
))),
|
||||
];
|
||||
|
||||
if let Some(domain) = domain {
|
||||
icon_sources.append(&mut vec![
|
||||
// Searches for the favicon if it's not defined at the root of the domain.
|
||||
favicon_from_page(&domain, client)
|
||||
.await
|
||||
.map(|url| fetch(url)),
|
||||
// If not found, fetch from root domain.
|
||||
Some(fetch(["https://", &domain, "/favicon.ico"].concat())),
|
||||
// If all else fails, try Google.
|
||||
Some(fetch(format!(
|
||||
"https://www.google.com/s2/favicons?domain={}&sz=32",
|
||||
domain
|
||||
))),
|
||||
]);
|
||||
}
|
||||
|
||||
// await every single source and take the first one, which does not return None
|
||||
let mut result = None;
|
||||
for f in icon_sources.drain(..).flatten() {
|
||||
|
|
@ -190,7 +195,7 @@ impl App {
|
|||
tracing::error!("error writing favicon to {:?}: {}", &favicon_path, err);
|
||||
}
|
||||
}
|
||||
None => tracing::error!("no icon found for {}", domain),
|
||||
None => tracing::error!("no icon found for {}", query),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue