aboutsummaryrefslogtreecommitdiffstats
path: root/background_scripts
diff options
context:
space:
mode:
Diffstat (limited to 'background_scripts')
-rw-r--r--background_scripts/completion.coffee37
1 files changed, 33 insertions, 4 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee
index 23696185..b6a52a15 100644
--- a/background_scripts/completion.coffee
+++ b/background_scripts/completion.coffee
@@ -238,7 +238,7 @@ class DomainCompleter
onComplete()
onPageVisited: (newPage) ->
- domain = @parseDomain(newPage.url)
+ domain = @parseDomainAndScheme newPage.url
if domain
slot = @domains[domain] ||= { entry: newPage, referenceCount: 0 }
# We want each entry in our domains hash to point to the most recent History entry for that domain.
@@ -250,15 +250,40 @@ class DomainCompleter
@domains = {}
else
toRemove.urls.forEach (url) =>
- domain = @parseDomain(url)
+ domain = @parseDomainAndScheme url
if domain and @domains[domain] and ( @domains[domain].referenceCount -= 1 ) == 0
delete @domains[domain]
- parseDomain: (url) -> url.split("/")[2] || ""
+ # Return something like "http://www.example.com" or false.
+ parseDomainAndScheme: (url) ->
+ Utils.hasFullUrlPrefix(url) and not Utils.hasChromePrefix(url) and url.split("/",3).join "/"
# Suggestions from the Domain completer have the maximum relevancy. They should be shown first in the list.
computeRelevancy: -> 1
+# TabRecency associates a logical timestamp with each tab id.
+class TabRecency
+ constructor: ->
+ @timestamp = 1
+ @cache = {}
+
+ chrome.tabs.onActivated.addListener (activeInfo) => @add activeInfo.tabId
+ chrome.tabs.onRemoved.addListener (tabId) => @remove tabId
+
+ chrome.tabs.onReplaced.addListener (addedTabId, removedTabId) =>
+ @remove removedTabId
+ @add addedTabId
+
+ add: (tabId) -> @cache[tabId] = ++@timestamp
+ remove: (tabId) -> delete @cache[tabId]
+
+ # Recently-visited tabs get a higher score (except the current tab, which gets a low score).
+ recencyScore: (tabId) ->
+ @cache[tabId] ||= 1
+ if @cache[tabId] == @timestamp then 0.0 else @cache[tabId] / @timestamp
+
+tabRecency = new TabRecency()
+
# Searches through all open tabs, matching on title and URL.
class TabCompleter
filter: (queryTerms, onComplete) ->
@@ -274,7 +299,10 @@ class TabCompleter
onComplete(suggestions)
computeRelevancy: (suggestion) ->
- RankingUtils.wordRelevancy(suggestion.queryTerms, suggestion.url, suggestion.title)
+ if suggestion.queryTerms.length
+ RankingUtils.wordRelevancy(suggestion.queryTerms, suggestion.url, suggestion.title)
+ else
+ tabRecency.recencyScore(suggestion.tabId)
# A completer which will return your search engines
class SearchEngineCompleter
@@ -547,3 +575,4 @@ root.SearchEngineCompleter = SearchEngineCompleter
root.HistoryCache = HistoryCache
root.RankingUtils = RankingUtils
root.RegexpCache = RegexpCache
+root.TabRecency = TabRecency