diff options
| author | Stephen Blott | 2015-05-02 12:14:16 +0100 |
|---|---|---|
| committer | Stephen Blott | 2015-05-02 14:35:49 +0100 |
| commit | 41495d11e6608767dde299223f10c8a606d4a8fb (patch) | |
| tree | fc9242889c5daa478fa26961dfe108a319b433c3 | |
| parent | 1257fc7a4fe7b9d0bfe1ad7ab7255f8dba4b988d (diff) | |
| download | vimium-41495d11e6608767dde299223f10c8a606d4a8fb.tar.bz2 | |
Search completion; minor tweaks.
Including:
- Make completers classes. That way, we may be able to get better
code reuse.
| -rw-r--r-- | background_scripts/completion.coffee | 9 | ||||
| -rw-r--r-- | background_scripts/search_engines.coffee | 46 | ||||
| -rw-r--r-- | lib/utils.coffee | 4 |
3 files changed, 33 insertions, 26 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 40c0d119..d8cf1667 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -326,7 +326,6 @@ class SearchEngineCompleter filter: (queryTerms, onComplete) -> SearchEngines.complete Settings.get("searchUrl"), queryTerms, (suggestions = []) => - console.log suggestions.length characterCount = queryTerms.join("").length completions = for suggestion in suggestions @@ -335,11 +334,11 @@ class SearchEngineCompleter onComplete completions computeRelevancy: (suggestion) -> - # We score search-engine completions by word relevancy, but weight increasingly as the number of + # We score search-engine completions by word relevancy, but weight the score increasingly as the number of # characters in the query terms increases. The idea is that, the more the user has had to type, the less - # likely it is that one of the other suggestion types has found what they're looking for, so the more - # likely it is that this suggestion will be useful. - # (1.0 - (1.0 / suggestion.characterCount)) * + # likely it is that one of the other suggestion types has proven useful, so the more likely it is that + # this suggestion will be useful. + # NOTE(smblott) This will require tweaking. (Math.min(suggestion.extraRelevancyData, 12)/12) * RankingUtils.wordRelevancy suggestion.queryTerms, suggestion.title, suggestion.title diff --git a/background_scripts/search_engines.coffee b/background_scripts/search_engines.coffee index 5d69d087..3dfea180 100644 --- a/background_scripts/search_engines.coffee +++ b/background_scripts/search_engines.coffee @@ -5,10 +5,14 @@ # getUrl: map these query terms to a completion URL. # parse: extract suggestions from the resulting (successful) XMLHttpRequest. # -Google = +class Google + constructor: -> name: "Google" match: (searchUrl) -> - true # TBD. + return true if /^https?:\/\/[a-z]+.google.com\//.test searchUrl + # NOTE(smblott). A temporary hack, just for me, and just for development. Will be removed. + return true if /localhost\/.*\/booky/.test searchUrl + false getUrl: (queryTerms) -> "http://suggestqueries.google.com/complete/search?ss_protocol=legace&client=toolbar&q=#{Utils.createSearchQuery queryTerms}" @@ -23,16 +27,17 @@ Google = else callback [] -# A dummy search engine which is guaranteed to match any search URL, but produces no completions. This allows -# the rest of the logic to be written knowing that there will be a search engine match. -DummySearchEngine = +# 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 + constructor: -> name: "Dummy" match: -> true # We return a useless URL which we know will succeed, but which won't generate any network traffic. getUrl: -> chrome.runtime.getURL "content_scripts/vimium.css" parse: (_, callback) -> callback [] -CompletionEngines = [ Google, DummySearchEngine ] +completionEngines = [ Google, DummySearchEngine ] SearchEngines = cancel: (searchUrl, callback = null) -> @@ -40,16 +45,18 @@ SearchEngines = delete @requests[searchUrl] callback? null - # Perform and HTTP GET. - # searchUrl is the search engine's URL, e.g. Settings.get("searchUrl") - # url is the URL to fetch - # callback will be called a successful XMLHttpRequest object, or null. + # Perform an HTTP GET. + # searchUrl is the search engine's URL, e.g. Settings.get("searchUrl"). + # url is the URL to fetch. + # callback will be called with a successful XMLHttpRequest object, or null. get: (searchUrl, url, callback) -> @requests ?= {} # Maps searchUrls to any outstanding HTTP request for that search engine. @cancel searchUrl - # We cache the results of recent requests (with a two-hour expiry). - @requestCache ?= new SimpleCache 2 * 60 * 60 * 1000 + # We cache the results of the most-recent 1000 requests (with a two-hour expiry). + # FIXME(smblott) Currently we're caching XMLHttpRequest objects, which is wasteful of memory. It would be + # better to handle caching at a higher level. + @requestCache ?= new SimpleCache 2 * 60 * 60 * 1000, 1000 if @requestCache.has url callback @requestCache.get url @@ -70,21 +77,22 @@ SearchEngines = else callback null - # Look up the search engine for this search URL. Because of DummySearchEngine, above, we know there will - # always be a match. Imagining that there may be many search engines, and knowing that this is called for - # every character entered, we cache the result. + # Look up the search-completion engine for this search URL. Because of DummySearchEngine, above, we know + # there will always be a match. Imagining that there may be many search engines, and knowing that this is + # called for every character entered, we cache the result. lookupEngine: (searchUrl) -> @engineCache ?= new SimpleCache 24 * 60 * 60 * 1000 if @engineCache.has searchUrl @engineCache.get searchUrl else - for engine in CompletionEngines + for engine in completionEngines + engine = new engine() return @engineCache.set searchUrl, engine if engine.match searchUrl # This is the main (actually, the only) entry point. - # searchUrl is the search engine's URL, e.g. Settings.get("searchUrl") - # queryTerms are the queryTerms - # callback will be applied to a list of suggestion strings (which will be an empty list, if anything goes + # searchUrl is the search engine's URL, e.g. Settings.get("searchUrl"). + # queryTerms are the queryTerms. + # callback will be applied to a list of suggestion strings (which may be an empty list, if anything goes # wrong). complete: (searchUrl, queryTerms, callback) -> return callback [] unless 0 < queryTerms.length diff --git a/lib/utils.coffee b/lib/utils.coffee index 338e535d..88fe9e2c 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -202,7 +202,8 @@ class SimpleCache constructor: (@expiry = 60 * 60 * 1000, @entries = 1000) -> @cache = {} @previous = {} - setInterval (=> @rotate()), @expiry + rotate = => @rotate() + setInterval rotate, @expiry rotate: -> @previous = @cache @@ -212,7 +213,6 @@ class SimpleCache (key of @cache) or key of @previous get: (key) -> - console.log "get", key if key of @cache @cache[key] else if key of @previous |
