aboutsummaryrefslogtreecommitdiffstats
path: root/background_scripts/completion.coffee
diff options
context:
space:
mode:
authorStephen Blott2015-05-01 13:19:23 +0100
committerStephen Blott2015-05-02 14:35:49 +0100
commit3616e0d01ffb38e9b7d344b99ad6ce7bc6aa071e (patch)
tree3bac6dc0267feea852d91ed3139f55030b6d719d /background_scripts/completion.coffee
parent1debac63fcc71c88427da9b1ae450067c15cd2b2 (diff)
downloadvimium-3616e0d01ffb38e9b7d344b99ad6ce7bc6aa071e.tar.bz2
Search completion; initial working version.
Diffstat (limited to 'background_scripts/completion.coffee')
-rw-r--r--background_scripts/completion.coffee62
1 files changed, 61 insertions, 1 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee
index 21730c4e..4f94f9e9 100644
--- a/background_scripts/completion.coffee
+++ b/background_scripts/completion.coffee
@@ -13,7 +13,7 @@
# It also has an attached "computeRelevancyFunction" which determines how well this item matches the given
# query terms.
class Suggestion
- showRelevancy: false # Set this to true to render relevancy when debugging the ranking scores.
+ showRelevancy: true # Set this to true to render relevancy when debugging the ranking scores.
# - type: one of [bookmark, history, tab].
# - computeRelevancyFunction: a function which takes a Suggestion and returns a relevancy score
@@ -321,6 +321,65 @@ class TabCompleter
else
tabRecency.recencyScore(suggestion.tabId)
+# searchUrl is the URL that will be used for the search, either the default search URL, or a custom
+# search-engine URL. The other arguments area obvious.
+# If we know the search-suggestion URL for searchUrl, then use it to pass a list of suggestions to callback.
+# Otherwise, just call callback.
+#
+# Note: That's all TBD. For now, we just assume Google and use it.
+#
+getOnlineSuggestions = do ->
+ xhrs = {} # Maps searchUrl to outstanding HTTP request.
+ (searchUrl, queryTerms, callback) ->
+ # Cancel any outstanding requests.
+ xhrs?[searchUrl]?.abort()
+ xhrs[searchUrl] = null
+
+ sendNoSuggestions = -> xhrs[searchUrl] = null; callback []
+ return sendNoSuggestions() if queryTerms.length == 0
+
+ url = "http://suggestqueries.google.com/complete/search?ss_protocol=legace&client=toolbar&q=#{Utils.createSearchQuery queryTerms}"
+ xhrs[searchUrl] = xhr = new XMLHttpRequest()
+ xhr.open "GET", url, true
+ xhr.timeout = 500
+ xhr.ontimeout = sendNoSuggestions
+ xhr.onerror = sendNoSuggestions
+ xhr.send()
+
+ xhr.onreadystatechange = (response) =>
+ if xhr.readyState == 4
+ suggestions = xhr.responseXML?.getElementsByTagName "suggestion"
+ return sendNoSuggestions() unless xhr.status == 200 and suggestions
+ xhr[searchUrl] = null
+ suggestions =
+ for suggestion in suggestions
+ continue unless suggestion = suggestion.getAttribute "data"
+ suggestion
+ callback suggestions
+
+class SearchEngineCompleter
+ refresh: ->
+ filter: (queryTerms, onComplete) ->
+ return onComplete([]) if queryTerms.length == 0
+
+ getOnlineSuggestions Settings.get("searchUrl"), queryTerms, (suggestions) =>
+ completions =
+ for suggestion in suggestions
+ url = Utils.createSearchUrl suggestion.split /\s+/
+ new Suggestion queryTerms, "suggestion", url, suggestion, @computeRelevancy
+ characterCount = queryTerms.join(" ").length
+ completion.characterCount = characterCount for completion in completions
+ onComplete completions
+
+ computeRelevancy: (suggestion) ->
+ # We score search-engine completions by word relevancy, but weight 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 a search suggestion will be useful.
+ # (1.0 - (1.0 / suggestion.characterCount)) *
+ (Math.min(suggestion.characterCount, 12)/12) *
+ RankingUtils.wordRelevancy suggestion.queryTerms, suggestion.title, suggestion.title
+
# A completer which will return your search engines
class CustomSearchEngineCompleter
searchEngines: {}
@@ -617,6 +676,7 @@ root.MultiCompleter = MultiCompleter
root.HistoryCompleter = HistoryCompleter
root.DomainCompleter = DomainCompleter
root.TabCompleter = TabCompleter
+root.SearchEngineCompleter = SearchEngineCompleter
root.CustomSearchEngineCompleter = CustomSearchEngineCompleter
root.HistoryCache = HistoryCache
root.RankingUtils = RankingUtils