aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorStephen Blott2015-05-18 07:04:38 +0100
committerStephen Blott2015-05-18 07:04:38 +0100
commitf38b77a1899ebc13a684c50b1c4b803c87df63b5 (patch)
treec6b066d1989b105fe61a456a99cb6cce84c3dfd0 /lib
parentc16b1c20e20c27b0e46038326fd97f1a721634ef (diff)
parent904398308debad22fedec227ec8e71adb2bc5720 (diff)
downloadvimium-f38b77a1899ebc13a684c50b1c4b803c87df63b5.tar.bz2
Merge branch 'vomnibar-insert-history-with-tab'
Conflicts: background_scripts/completion.coffee
Diffstat (limited to 'lib')
-rw-r--r--lib/utils.coffee20
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) ->