diff options
| author | Stephen Blott | 2015-05-16 12:57:58 +0100 |
|---|---|---|
| committer | Stephen Blott | 2015-05-16 12:58:02 +0100 |
| commit | c09e70364118264804510ee4b06f3ff8d38933b1 (patch) | |
| tree | b674bbd0b4b97b73e5c0e7ae0b97909cb1209028 | |
| parent | 9742cee10745f256c66b52608090ddcc5107f33d (diff) | |
| download | vimium-c09e70364118264804510ee4b06f3ff8d38933b1.tar.bz2 | |
Refactor query extraction to Utils.extractQuery().
If we go with this and #1662, then we can share the utility.
| -rw-r--r-- | background_scripts/completion.coffee | 20 | ||||
| -rw-r--r-- | lib/utils.coffee | 15 |
2 files changed, 21 insertions, 14 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 66546708..eb80f3cd 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -230,26 +230,18 @@ class HistoryCompleter results = [] HistoryCache.use (history) => searchUrl = Settings.get "searchUrl" - searchUrlTerminator = new RegExp "[?&#/]" results = if queryTerms.length > 0 history.filter (entry) -> RankingUtils.matches(queryTerms, entry.url, entry.title) else [] onComplete results.map (entry) => - # If this history URL starts with the search URL, we reconstruct the original search terms, and insert - # them into the vomnibar when this suggestion is selected. We use try/catch because - # decodeURIComponent() can throw an error. - insertText = - try - if entry.url.startsWith searchUrl - # This maps "https://www.google.ie/search?q=star+wars&..." to "star wars". - entry.url[searchUrl.length..].split(searchUrlTerminator)[0].split("+").map(decodeURIComponent).join " " - catch - null - - # If this history item does not have a title and we found its query text above, then use its query - # text as its title. + # This entry's URL might match the default search engine, in which case we'll insert its query text + # into the vomnibar input whenever this entry is selected. + insertText = Utils.extractQuery searchUrl, entry.url + + # If this history item does not have a title and we successfully extracted query text above, then use + # that text in lieu of a title. entry.title ||= insertText if insertText? new Suggestion diff --git a/lib/utils.coffee b/lib/utils.coffee index cbc937b6..cd466b9b 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -114,6 +114,21 @@ Utils = searchUrl += "%s" unless 0 <= searchUrl.indexOf "%s" searchUrl.replace /%s/g, @createSearchQuery query + # Extract a query from url if it appears to be a URL created by createSearchQuery. + # For example, map "https://www.google.ie/search?q=star+wars&foo&bar" to "star wars". + extractQuery: do => + queryTerminator = new RegExp "[?&#/]" + httpProtocolRegexp = new RegExp "^https?://" + (searchUrl, url) -> + url = url.replace httpProtocolRegexp + searchUrl = searchUrl.split("%s")[0].replace httpProtocolRegexp + # We use try/catch because decodeURIComponent can throw an exception. + try + if url.startsWith searchUrl + url[searchUrl.length..].split(queryTerminator)[0].split("+").map(decodeURIComponent).join " " + catch + null + # Converts :string into a Google search if it's not already a URL. We don't bother with escaping characters # as Chrome will do that for us. convertToUrl: (string) -> |
