From c09e70364118264804510ee4b06f3ff8d38933b1 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 16 May 2015 12:57:58 +0100 Subject: Refactor query extraction to Utils.extractQuery(). If we go with this and #1662, then we can share the utility. --- lib/utils.coffee | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib/utils.coffee') 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) -> -- cgit v1.2.3 From 0c7668d6cc46cf24081b6b64ece27faef6667dea Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 17 May 2015 11:30:30 +0100 Subject: TabToOpen: tidy up pre-PR. --- lib/utils.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/utils.coffee') diff --git a/lib/utils.coffee b/lib/utils.coffee index cd466b9b..724250e0 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -114,7 +114,7 @@ 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. + # Extract a query from url if it appears to be a URL created generated 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 "[?&#/]" -- cgit v1.2.3 From 222d3dd6491bca8f152382fb84e2d43c499b8951 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 17 May 2015 11:34:37 +0100 Subject: TabToOpen: more tidy up pre-PR. --- lib/utils.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/utils.coffee') diff --git a/lib/utils.coffee b/lib/utils.coffee index 724250e0..63b8ba0e 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -114,7 +114,7 @@ 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 generated from the given search URL. + # 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 "[?&#/]" -- cgit v1.2.3 From 904398308debad22fedec227ec8e71adb2bc5720 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 17 May 2015 13:24:46 +0100 Subject: TabToOpen: respect trailing options when extracting query terms. A custom search engine like this... i: https://www.google.ie/search?q=%s&num=30&newwindow=1&biw=1918&bih=1015&tbm=isch Google image search Should not match a URL like this... https://www.google.ie/search?q=bbc+sport --- lib/utils.coffee | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'lib/utils.coffee') diff --git a/lib/utils.coffee b/lib/utils.coffee index 63b8ba0e..03403644 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -121,10 +121,15 @@ Utils = httpProtocolRegexp = new RegExp "^https?://" (searchUrl, url) -> url = url.replace httpProtocolRegexp - searchUrl = searchUrl.split("%s")[0].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 - if url.startsWith searchUrl url[searchUrl.length..].split(queryTerminator)[0].split("+").map(decodeURIComponent).join " " catch null -- cgit v1.2.3