From 5cb7e78cabff5d33ed07f223928268233eefb08c Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 30 May 2015 14:30:36 +0100 Subject: Search completion; add support for Google Maps completion. --- background_scripts/completion_engines.coffee | 40 +++++++++++++++++----------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/background_scripts/completion_engines.coffee b/background_scripts/completion_engines.coffee index 189f66f3..4601ef6a 100644 --- a/background_scripts/completion_engines.coffee +++ b/background_scripts/completion_engines.coffee @@ -34,17 +34,39 @@ class GoogleXMLRegexpEngine extends RegexpEngine class Google extends GoogleXMLRegexpEngine # Example search URL: http://www.google.com/search?q=%s - constructor: -> - super [ + constructor: (regexps = null) -> + regexps ?= [ # We match the major English-speaking TLDs. new RegExp "^https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/" new RegExp "localhost/cgi-bin/booky" # Only for smblott. ] + super regexps getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, "http://suggestqueries.google.com/complete/search?ss_protocol=legace&client=toolbar&q=%s" +# A wrapper class for Google completions. This adds prefix terms to the query, and strips those terms from +# the resulting suggestions. For example, for Google Maps, we add "map of" as a prefix, then strip "map of" +# from the resulting suggestions. +class GoogleWithPrefix + constructor: (prefix, args...) -> + @engine = new Google args... + @prefix = "#{prefix.trim()} " + @queryTerms = @prefix.split /\s+/ + match: (args...) -> @engine.match args... + getUrl: (queryTerms) -> @engine.getUrl [ @queryTerms..., queryTerms... ] + parse: (xhr) -> + @engine.parse(xhr) + .filter (suggestion) => suggestion.startsWith @prefix + .map (suggestion) => suggestion[@prefix.length..].ltrim() + +# For Google Maps, we add the prefix "map of" to the query, and send it to Google's general search engine, +# then strip "map of" from the resulting suggestions. +class GoogleMaps extends GoogleWithPrefix + # Example search URL: https://www.google.com/maps?q=%s + constructor: -> super "map of", [ new RegExp "https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/maps" ] + class Youtube extends GoogleXMLRegexpEngine # Example search URL: http://www.youtube.com/results?search_query=%s constructor: -> @@ -67,19 +89,6 @@ class Wikipedia extends RegexpEngine parse: (xhr) -> JSON.parse(xhr.responseText)[1] -## Does not work... -## class GoogleMaps extends RegexpEngine -## # Example search URL: https://www.google.com/maps/search/%s -## constructor: -> -## super [ new RegExp "^https?://www\.google\.com/maps/search/" ] -## -## getUrl: (queryTerms) -> -## "https://www.google.com/s?tbm=map&fp=1&gs_ri=maps&source=hp&suggest=p&authuser=0&hl=en&pf=p&tch=1&ech=2&q=#{Utils.createSearchQuery queryTerms}" -## -## parse: (xhr) -> -## data = JSON.parse xhr.responseText -## [] - class Bing extends RegexpEngine # Example search URL: https://www.bing.com/search?q=%s constructor: -> super [ new RegExp "^https?://www\.bing\.com/search" ] @@ -120,6 +129,7 @@ class DummyCompletionEngine # Note: Order matters here. CompletionEngines = [ Youtube + GoogleMaps Google DuckDuckGo Wikipedia -- cgit v1.2.3 From ecdc649e220ef11c30be1f2af4496559bf35d4c4 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 30 May 2015 14:31:37 +0100 Subject: Search completion; remove unused code. This removes a couple of lines which should have been removed previously. --- background_scripts/completion_engines.coffee | 3 --- 1 file changed, 3 deletions(-) diff --git a/background_scripts/completion_engines.coffee b/background_scripts/completion_engines.coffee index 4601ef6a..329a609e 100644 --- a/background_scripts/completion_engines.coffee +++ b/background_scripts/completion_engines.coffee @@ -26,7 +26,6 @@ class RegexpEngine # Several Google completion engines package XML responses in this way. class GoogleXMLRegexpEngine extends RegexpEngine - doNotCache: false # true (disbaled, experimental) parse: (xhr) -> for suggestion in xhr.responseXML.getElementsByTagName "suggestion" continue unless suggestion = suggestion.getAttribute "data" @@ -38,7 +37,6 @@ class Google extends GoogleXMLRegexpEngine regexps ?= [ # We match the major English-speaking TLDs. new RegExp "^https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/" - new RegExp "localhost/cgi-bin/booky" # Only for smblott. ] super regexps @@ -77,7 +75,6 @@ class Youtube extends GoogleXMLRegexpEngine "http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&xml=t&q=%s" class Wikipedia extends RegexpEngine - doNotCache: false # true (disbaled, experimental) # Example search URL: http://www.wikipedia.org/w/index.php?title=Special:Search&search=%s constructor: -> super [ new RegExp "^https?://[a-z]+\.wikipedia\.org/" ] -- cgit v1.2.3 From ff15254e963e0f7c2232190579426cf9e717d27a Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 30 May 2015 14:35:47 +0100 Subject: Search completion; simplify completion-engine definitions. --- background_scripts/completion_engines.coffee | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/background_scripts/completion_engines.coffee b/background_scripts/completion_engines.coffee index 329a609e..c9a18a7d 100644 --- a/background_scripts/completion_engines.coffee +++ b/background_scripts/completion_engines.coffee @@ -21,7 +21,7 @@ # A base class for common regexp-based matching engines. class RegexpEngine - constructor: (@regexps) -> + constructor: (args...) -> @regexps = args match: (searchUrl) -> Utils.matchesAnyRegexp @regexps, searchUrl # Several Google completion engines package XML responses in this way. @@ -34,11 +34,7 @@ class GoogleXMLRegexpEngine extends RegexpEngine class Google extends GoogleXMLRegexpEngine # Example search URL: http://www.google.com/search?q=%s constructor: (regexps = null) -> - regexps ?= [ - # We match the major English-speaking TLDs. - new RegExp "^https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/" - ] - super regexps + super regexps ? new RegExp "^https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, @@ -63,12 +59,12 @@ class GoogleWithPrefix # then strip "map of" from the resulting suggestions. class GoogleMaps extends GoogleWithPrefix # Example search URL: https://www.google.com/maps?q=%s - constructor: -> super "map of", [ new RegExp "https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/maps" ] + constructor: -> super "map of", new RegExp "https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/maps" class Youtube extends GoogleXMLRegexpEngine # Example search URL: http://www.youtube.com/results?search_query=%s constructor: -> - super [ new RegExp "^https?://[a-z]+\.youtube\.com/results" ] + super new RegExp "^https?://[a-z]+\.youtube\.com/results" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, @@ -77,7 +73,7 @@ class Youtube extends GoogleXMLRegexpEngine class Wikipedia extends RegexpEngine # Example search URL: http://www.wikipedia.org/w/index.php?title=Special:Search&search=%s constructor: -> - super [ new RegExp "^https?://[a-z]+\.wikipedia\.org/" ] + super new RegExp "^https?://[a-z]+\.wikipedia\.org/" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, @@ -88,13 +84,13 @@ class Wikipedia extends RegexpEngine class Bing extends RegexpEngine # Example search URL: https://www.bing.com/search?q=%s - constructor: -> super [ new RegExp "^https?://www\.bing\.com/search" ] + constructor: -> super new RegExp "^https?://www\.bing\.com/search" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, "http://api.bing.com/osjson.aspx?query=%s" parse: (xhr) -> JSON.parse(xhr.responseText)[1] class Amazon extends RegexpEngine # Example search URL: http://www.amazon.com/s/?field-keywords=%s - constructor: -> super [ new RegExp "^https?://www\.amazon\.(com|co.uk|ca|com.au)/s/" ] + constructor: -> super new RegExp "^https?://www\.amazon\.(com|co.uk|ca|com.au)/s/" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, "https://completion.amazon.com/search/complete?method=completion&search-alias=aps&client=amazon-search-ui&mkt=1&q=%s" @@ -102,7 +98,7 @@ class Amazon extends RegexpEngine class DuckDuckGo extends RegexpEngine # Example search URL: https://duckduckgo.com/?q=%s - constructor: -> super [ new RegExp "^https?://([a-z]+\.)?duckduckgo\.com/" ] + constructor: -> super new RegExp "^https?://([a-z]+\.)?duckduckgo\.com/" getUrl: (queryTerms) -> getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, "https://duckduckgo.com/ac/?q=%s" parse: (xhr) -> @@ -110,7 +106,7 @@ class DuckDuckGo extends RegexpEngine class Webster extends RegexpEngine # Example search URL: http://www.merriam-webster.com/dictionary/%s - constructor: -> super [ new RegExp "^https?://www.merriam-webster.com/dictionary/" ] + constructor: -> super new RegExp "^https?://www.merriam-webster.com/dictionary/" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, "http://www.merriam-webster.com/autocomplete?query=%s" parse: (xhr) -> JSON.parse(xhr.responseText).suggestions -- cgit v1.2.3 From 278933e3ea52d456ce2b07bfaea93d5667fef03f Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 30 May 2015 14:40:17 +0100 Subject: Search completion; refactor regexp parsing (simplification). --- background_scripts/completion_engines.coffee | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/background_scripts/completion_engines.coffee b/background_scripts/completion_engines.coffee index c9a18a7d..f15e6db4 100644 --- a/background_scripts/completion_engines.coffee +++ b/background_scripts/completion_engines.coffee @@ -21,7 +21,7 @@ # A base class for common regexp-based matching engines. class RegexpEngine - constructor: (args...) -> @regexps = args + constructor: (args...) -> @regexps = args.map (regexp) -> new RegExp regexp match: (searchUrl) -> Utils.matchesAnyRegexp @regexps, searchUrl # Several Google completion engines package XML responses in this way. @@ -34,7 +34,7 @@ class GoogleXMLRegexpEngine extends RegexpEngine class Google extends GoogleXMLRegexpEngine # Example search URL: http://www.google.com/search?q=%s constructor: (regexps = null) -> - super regexps ? new RegExp "^https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/" + super regexps ? "^https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, @@ -59,12 +59,12 @@ class GoogleWithPrefix # then strip "map of" from the resulting suggestions. class GoogleMaps extends GoogleWithPrefix # Example search URL: https://www.google.com/maps?q=%s - constructor: -> super "map of", new RegExp "https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/maps" + constructor: -> super "map of", "https?://[a-z]+\.google\.(com|ie|co\.uk|ca|com\.au)/maps" class Youtube extends GoogleXMLRegexpEngine # Example search URL: http://www.youtube.com/results?search_query=%s constructor: -> - super new RegExp "^https?://[a-z]+\.youtube\.com/results" + super "^https?://[a-z]+\.youtube\.com/results" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, @@ -73,7 +73,7 @@ class Youtube extends GoogleXMLRegexpEngine class Wikipedia extends RegexpEngine # Example search URL: http://www.wikipedia.org/w/index.php?title=Special:Search&search=%s constructor: -> - super new RegExp "^https?://[a-z]+\.wikipedia\.org/" + super "^https?://[a-z]+\.wikipedia\.org/" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, @@ -84,13 +84,13 @@ class Wikipedia extends RegexpEngine class Bing extends RegexpEngine # Example search URL: https://www.bing.com/search?q=%s - constructor: -> super new RegExp "^https?://www\.bing\.com/search" + constructor: -> super "^https?://www\.bing\.com/search" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, "http://api.bing.com/osjson.aspx?query=%s" parse: (xhr) -> JSON.parse(xhr.responseText)[1] class Amazon extends RegexpEngine # Example search URL: http://www.amazon.com/s/?field-keywords=%s - constructor: -> super new RegExp "^https?://www\.amazon\.(com|co.uk|ca|com.au)/s/" + constructor: -> super "^https?://www\.amazon\.(com|co.uk|ca|com.au)/s/" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, "https://completion.amazon.com/search/complete?method=completion&search-alias=aps&client=amazon-search-ui&mkt=1&q=%s" @@ -98,15 +98,14 @@ class Amazon extends RegexpEngine class DuckDuckGo extends RegexpEngine # Example search URL: https://duckduckgo.com/?q=%s - constructor: -> super new RegExp "^https?://([a-z]+\.)?duckduckgo\.com/" - getUrl: (queryTerms) -> + constructor: -> super "^https?://([a-z]+\.)?duckduckgo\.com/" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, "https://duckduckgo.com/ac/?q=%s" parse: (xhr) -> suggestion.phrase for suggestion in JSON.parse xhr.responseText class Webster extends RegexpEngine # Example search URL: http://www.merriam-webster.com/dictionary/%s - constructor: -> super new RegExp "^https?://www.merriam-webster.com/dictionary/" + constructor: -> super "^https?://www.merriam-webster.com/dictionary/" getUrl: (queryTerms) -> Utils.createSearchUrl queryTerms, "http://www.merriam-webster.com/autocomplete?query=%s" parse: (xhr) -> JSON.parse(xhr.responseText).suggestions -- cgit v1.2.3