|
1 | 1 | import { browser } from "../../Browser"; |
2 | 2 | import { bare } from "../../IsolatedFrame"; |
3 | 3 |
|
| 4 | +import * as tldts from "tldts"; |
| 5 | + |
4 | 6 | export type OmniboxResult = { |
5 | 7 | kind: |
6 | 8 | | "search" |
@@ -109,11 +111,15 @@ function calculateRelevanceScore(result: OmniboxResult, query: string): number { |
109 | 111 |
|
110 | 112 | let score = 0; |
111 | 113 |
|
112 | | - if (urlString === lowerQuery || title === lowerQuery) { |
113 | | - return 100; |
| 114 | + // if (urlString === lowerQuery || title === lowerQuery) { |
| 115 | + // return 100; |
| 116 | + // } |
| 117 | + |
| 118 | + if (result.kind === "direct") { |
| 119 | + return 95; |
114 | 120 | } |
115 | 121 |
|
116 | | - if (result.kind === "direct" || result.kind === "directsearch") { |
| 122 | + if (result.kind === "directsearch") { |
117 | 123 | return 90; |
118 | 124 | } |
119 | 125 |
|
@@ -163,7 +169,7 @@ function rankResults( |
163 | 169 | suggestionDenied && |
164 | 170 | (b.kind === "direct" || b.kind === "directsearch") |
165 | 171 | ) { |
166 | | - // force direct results to the top |
| 172 | + // don't allow something other than What Was Typed to be higher if the user just backspaced |
167 | 173 | return 1; |
168 | 174 | } else { |
169 | 175 | return (b.relevanceScore || 0) - (a.relevanceScore || 0); |
@@ -196,35 +202,39 @@ const fetchHistoryResults = (query: string): OmniboxResult[] => { |
196 | 202 | return results.slice(0, 5); |
197 | 203 | }; |
198 | 204 |
|
199 | | -const addDirectResult = ( |
200 | | - query: string, |
201 | | - results: OmniboxResult[] |
202 | | -): OmniboxResult[] => { |
| 205 | +const addDirectResult = (query: string, results: OmniboxResult[]) => { |
| 206 | + let directurl; |
203 | 207 | if (URL.canParse(query)) { |
204 | | - return [ |
205 | | - { |
206 | | - kind: "direct", |
207 | | - url: new URL(query), |
208 | | - title: null, |
209 | | - favicon: null, |
210 | | - }, |
211 | | - ...results, |
212 | | - ]; |
| 208 | + directurl = new URL(query); |
213 | 209 | } else { |
214 | | - return [ |
215 | | - { |
216 | | - kind: "directsearch", |
217 | | - url: new URL( |
218 | | - AVAILABLE_SEARCH_ENGINES[ |
219 | | - browser.settings.defaultSearchEngine |
220 | | - ].searchUrlBuilder(query) |
221 | | - ), |
222 | | - title: query, |
223 | | - favicon: null, |
224 | | - }, |
225 | | - ...results, |
226 | | - ]; |
| 210 | + let parsed = tldts.parse(query); |
| 211 | + if ((parsed.domain && parsed.isIcann) || parsed.isIp) { |
| 212 | + // TODO: this probably isn't right for all cases |
| 213 | + // i think typing in `://a.com` would break it because it's an invalid url but passes tldts |
| 214 | + // but tldts doesn't parse path/port/schema so we can't use its parser |
| 215 | + directurl = new URL("https://" + query); |
| 216 | + } |
227 | 217 | } |
| 218 | + |
| 219 | + if (directurl) { |
| 220 | + results.unshift({ |
| 221 | + kind: "direct", |
| 222 | + url: directurl, |
| 223 | + title: null, |
| 224 | + favicon: null, |
| 225 | + }); |
| 226 | + } |
| 227 | + |
| 228 | + results.unshift({ |
| 229 | + kind: "directsearch", |
| 230 | + url: new URL( |
| 231 | + AVAILABLE_SEARCH_ENGINES[ |
| 232 | + browser.settings.defaultSearchEngine |
| 233 | + ].searchUrlBuilder(query) |
| 234 | + ), |
| 235 | + title: query, |
| 236 | + favicon: null, |
| 237 | + }); |
228 | 238 | }; |
229 | 239 |
|
230 | 240 | const fetchGoogleSuggestions = async ( |
@@ -290,15 +300,15 @@ export async function fetchSuggestions( |
290 | 300 | ...cachedGoogleResults, |
291 | 301 | ]; |
292 | 302 |
|
293 | | - combinedResults = addDirectResult(query, combinedResults); |
| 303 | + addDirectResult(query, combinedResults); |
294 | 304 |
|
295 | 305 | // first update, so the user sees something quickly |
296 | 306 | setResults(rankResults(combinedResults, query, suggestionDenied)); |
297 | 307 |
|
298 | 308 | const googleResults = await fetchGoogleSuggestions(query); |
299 | 309 |
|
300 | 310 | combinedResults = [...historyResults, ...googleResults]; |
301 | | - combinedResults = addDirectResult(query, combinedResults); |
| 311 | + addDirectResult(query, combinedResults); |
302 | 312 |
|
303 | 313 | // update with the new google results |
304 | 314 | setResults(rankResults(combinedResults, query, suggestionDenied)); |
|
0 commit comments