diff options
| author | Stephen Blott | 2015-05-18 07:04:38 +0100 |
|---|---|---|
| committer | Stephen Blott | 2015-05-18 07:04:38 +0100 |
| commit | f38b77a1899ebc13a684c50b1c4b803c87df63b5 (patch) | |
| tree | c6b066d1989b105fe61a456a99cb6cce84c3dfd0 /lib/utils.coffee | |
| parent | c16b1c20e20c27b0e46038326fd97f1a721634ef (diff) | |
| parent | 904398308debad22fedec227ec8e71adb2bc5720 (diff) | |
| download | vimium-f38b77a1899ebc13a684c50b1c4b803c87df63b5.tar.bz2 | |
Merge branch 'vomnibar-insert-history-with-tab'
Conflicts:
background_scripts/completion.coffee
Diffstat (limited to 'lib/utils.coffee')
| -rw-r--r-- | lib/utils.coffee | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/utils.coffee b/lib/utils.coffee index 9a5661de..65e26b7a 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -114,6 +114,26 @@ 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 from the given search URL. + # 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.replace httpProtocolRegexp + [ searchUrl, suffixTerms... ] = searchUrl.split "%s" + # We require the URL to start with the search URL. + return null unless url.startsWith searchUrl + # We require any remaining terms in the search URL to also be present in the URL. + for suffix in suffixTerms + return null unless 0 <= url.indexOf suffix + # We use try/catch because decodeURIComponent can throw an exception. + try + 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) -> |
