diff options
| author | Stephen Blott | 2015-05-03 11:21:06 +0100 |
|---|---|---|
| committer | Stephen Blott | 2015-05-03 11:21:06 +0100 |
| commit | 7ecc8805053cfe32549136412d16d5c62d6949c7 (patch) | |
| tree | b996fad06e7805f347f3d9675939d9bc3ca4dc86 /background_scripts | |
| parent | 05431a5041913d78e44058e9e7f42ef9eee29ce9 (diff) | |
| download | vimium-7ecc8805053cfe32549136412d16d5c62d6949c7.tar.bz2 | |
Search completion; Google maps out, Amazon, Bing in.
Diffstat (limited to 'background_scripts')
| -rw-r--r-- | background_scripts/main.coffee | 9 | ||||
| -rw-r--r-- | background_scripts/search_engines.coffee | 71 | ||||
| -rw-r--r-- | background_scripts/settings.coffee | 12 |
3 files changed, 56 insertions, 36 deletions
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 4d2546fc..e678b2f7 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -51,10 +51,11 @@ completionSources = completers = omni: new MultiCompleter([ - completionSources.searchEngines, - completionSources.bookmarks, - completionSources.history, - completionSources.domains]) + completionSources.searchEngines + # completionSources.bookmarks, + # completionSources.history, + # completionSources.domains + ]) bookmarks: new MultiCompleter([completionSources.bookmarks]) tabs: new MultiCompleter([completionSources.tabs]) diff --git a/background_scripts/search_engines.coffee b/background_scripts/search_engines.coffee index 90fcb99f..362de3cc 100644 --- a/background_scripts/search_engines.coffee +++ b/background_scripts/search_engines.coffee @@ -22,8 +22,15 @@ class RegexpEngine constructor: (@regexps) -> match: (searchUrl) -> Utils.matchesAnyRegexp @regexps, searchUrl -# Completion engine for English-language Google search. -class Google extends RegexpEngine +# Several Google completion engines package responses in this way. +class GoogleXMLRegexpEngine extends RegexpEngine + parse: (xhr) -> + for suggestion in xhr.responseXML.getElementsByTagName "suggestion" + continue unless suggestion = suggestion.getAttribute "data" + suggestion + +class Google extends GoogleXMLRegexpEngine + # Example search URL: http://www.google.com/search?q=%s constructor: -> super [ # We match the major English-speaking TLDs. @@ -34,23 +41,13 @@ class Google extends RegexpEngine getUrl: (queryTerms) -> "http://suggestqueries.google.com/complete/search?ss_protocol=legace&client=toolbar&q=#{Utils.createSearchQuery queryTerms}" - parse: (xhr) -> - for suggestion in xhr.responseXML.getElementsByTagName "suggestion" - continue unless suggestion = suggestion.getAttribute "data" - suggestion - -class Youtube extends RegexpEngine +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" ] getUrl: (queryTerms) -> - "http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&q=#{Utils.createSearchQuery queryTerms}" - - parse: (xhr) -> - text = xhr.responseText - text = text.replace /^[^(]*\(/, "" - text = text.replace /\)[^\)]*$/, "" - suggestion[0] for suggestion in JSON.parse(text)[1] + "http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&xml=t&q=#{Utils.createSearchQuery queryTerms}" class Wikipedia extends RegexpEngine # Example search URL: http://www.wikipedia.org/w/index.php?title=Special:Search&search=%s @@ -63,18 +60,34 @@ class Wikipedia extends RegexpEngine parse: (xhr) -> JSON.parse(xhr.responseText)[1] -class GoogleMaps extends RegexpEngine - 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}" +## 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) -> +## console.log "xxxxxxxxxxxxxxxxxxxxx" +## "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) -> +## console.log "yyy", xhr.responseText +## data = JSON.parse xhr.responseText +## console.log "zzz" +## console.log data +## [] + +class Bing extends RegexpEngine + # Example search URL: https://www.bing.com/search?q=%s + constructor: -> super [ new RegExp "^https?://www\.bing\.com/search" ] + getUrl: (queryTerms) -> "http://api.bing.com/osjson.aspx?query=#{Utils.createSearchQuery queryTerms}" + 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/s/" ] + getUrl: (queryTerms) -> "https://completion.amazon.com/search/complete?method=completion&search-alias=aps&client=amazon-search-ui&mkt=1&q=#{Utils.createSearchQuery queryTerms}" + parse: (xhr) -> JSON.parse(xhr.responseText)[1] - parse: (xhr) -> - console.log xhr - [] - - 'google-maps': 'https://www.google.com/maps/search/', # A dummy search engine which is guaranteed to match any search URL, but never produces completions. This # allows the rest of the logic to be written knowing that there will be a search engine match. class DummySearchEngine @@ -84,9 +97,11 @@ class DummySearchEngine parse: -> [] completionEngines = [ - Google Youtube + Google Wikipedia + Bing + Amazon DummySearchEngine ] @@ -112,7 +127,7 @@ SearchEngines = @requests[searchUrl] = xhr = new XMLHttpRequest() xhr.open "GET", url, true # We set a fairly short timeout. If we block for too long, then we block *all* completers. - xhr.timeout = 300 + xhr.timeout = 500 xhr.ontimeout = => @cancel searchUrl, callback xhr.onerror = => @cancel searchUrl, callback xhr.send() diff --git a/background_scripts/settings.coffee b/background_scripts/settings.coffee index 01277741..06b59af8 100644 --- a/background_scripts/settings.coffee +++ b/background_scripts/settings.coffee @@ -91,10 +91,14 @@ root.Settings = Settings = searchUrl: "http://www.google.com/search?q=" # put in an example search engine searchEngines: [ - "w: http://www.wikipedia.org/w/index.php?title=Special:Search&search=%s Wikipedia", "" - "t: http://www.youtube.com/results?search_query=%s Youtube", "" - "m: https://www.google.com/maps/search/%s Google Maps", "" - ].join "\n" + # FIXME(smblott) Comment out these before merge. + "w: http://www.wikipedia.org/w/index.php?title=Special:Search&search=%s Wikipedia" + "t: http://www.youtube.com/results?search_query=%s Youtube" + "m: https://www.google.com/maps/search/%s Google Maps" + "b: https://www.bing.com/search?q=%s Bing" + "y: http://www.youtube.com/results?search_query=%s Youtube" + "az: http://www.amazon.com/s/?field-keywords=%s Amazon" + ].join "\n\n" newTabUrl: "chrome://newtab" grabBackFocus: false |
