Skip to content

Commit a12c131

Browse files
authored
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
1 parent 643e39b commit a12c131

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

plugins/src/web/config.ron

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@
8787
matches: ["lib"],
8888
queries: [(name: "Libraries.io", query: "libraries.io/search?q=")]
8989
),
90-
(
91-
matches: ["mdn"],
92-
queries: [(name: "Mozilla Developer Network", query: "developer.mozilla.org/search?q=")]
93-
),
90+
(
91+
matches: ["mdn"],
92+
queries: [(name: "Mozilla Developer Network", query: "developer.mozilla.org/search?q=")]
93+
),
9494
(
9595
matches: ["npm"],
9696
queries: [(name: "npm", query: "npmjs.com/search?q=")]
@@ -115,10 +115,10 @@
115115
matches: ["stack"],
116116
queries: [(name: "Stack Overflow", query: "stackoverflow.com/search?q=")]
117117
),
118-
(
119-
matches: ["sp", "startpage"],
120-
queries: [(name: "Startpage", query: "startpage.com/sp/search?query=")]
121-
),
118+
(
119+
matches: ["sp", "startpage"],
120+
queries: [(name: "Startpage", query: "startpage.com/sp/search?query=")]
121+
),
122122
(
123123
matches: ["twitch"],
124124
queries: [(name: "Twitch", query: "twitch.tv/search?term=")]
@@ -131,6 +131,14 @@
131131
matches: ["wiki"],
132132
queries: [(name: "Wikipedia", query: "wikipedia.org/w/index.php?search=")]
133133
),
134+
(
135+
matches: ["www"],
136+
queries: [(
137+
name: "Open Website",
138+
query: "https://",
139+
icon: "https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/Crystal128-browser.svg/179px-Crystal128-browser.svg.png",
140+
)]
141+
),
134142
(
135143
matches: ["xaut", "arxauth", "arxivauthor"],
136144
queries: [(name: "ArXiv", query: "https://arxiv.org/search/?searchtype=author&source=header&query=")]

plugins/src/web/mod.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,17 @@ impl App {
130130
async fn fetch_icon_in_background(&self, def: &Definition, favicon_path: &Path) {
131131
let client = self.client.clone();
132132

133-
let url = build_query(def, "");
134-
let url = Url::parse(&url).expect("invalid url");
135133
let icon_source = def.icon.clone();
136134

137-
let domain = url
138-
.domain()
139-
.map(|domain| domain.to_string())
140-
.expect("url have no domain");
141-
142135
let favicon_path = favicon_path.to_path_buf();
143136

137+
let query = build_query(def, "");
138+
let domain = Url::parse(&query).ok().map(|url| {
139+
url.domain()
140+
.map(|d| d.to_string())
141+
.expect("url has no domain")
142+
});
143+
144144
tokio::spawn(async move {
145145
let client = &client;
146146
let favicon_path = &favicon_path;
@@ -155,19 +155,24 @@ impl App {
155155
Some(icon_source)
156156
.filter(|s| !s.is_empty())
157157
.map(|url| fetch(url)),
158-
// Searches for the favicon if it's not defined at the root of the domain.
159-
favicon_from_page(&domain, client)
160-
.await
161-
.map(|url| fetch(url)),
162-
// If not found, fetch from root domain.
163-
Some(fetch(["https://", &domain, "/favicon.ico"].concat())),
164-
// If all else fails, try Google.
165-
Some(fetch(format!(
166-
"https://www.google.com/s2/favicons?domain={}&sz=32",
167-
domain
168-
))),
169158
];
170159

160+
if let Some(domain) = domain {
161+
icon_sources.append(&mut vec![
162+
// Searches for the favicon if it's not defined at the root of the domain.
163+
favicon_from_page(&domain, client)
164+
.await
165+
.map(|url| fetch(url)),
166+
// If not found, fetch from root domain.
167+
Some(fetch(["https://", &domain, "/favicon.ico"].concat())),
168+
// If all else fails, try Google.
169+
Some(fetch(format!(
170+
"https://www.google.com/s2/favicons?domain={}&sz=32",
171+
domain
172+
))),
173+
]);
174+
}
175+
171176
// await every single source and take the first one, which does not return None
172177
let mut result = None;
173178
for f in icon_sources.drain(..).flatten() {
@@ -190,7 +195,7 @@ impl App {
190195
tracing::error!("error writing favicon to {:?}: {}", &favicon_path, err);
191196
}
192197
}
193-
None => tracing::error!("no icon found for {}", domain),
198+
None => tracing::error!("no icon found for {}", query),
194199
}
195200
});
196201
}

0 commit comments

Comments
 (0)