aboutsummaryrefslogtreecommitdiffstats
path: root/background_scripts
diff options
context:
space:
mode:
Diffstat (limited to 'background_scripts')
-rw-r--r--background_scripts/completion.coffee60
-rw-r--r--background_scripts/exclusions.coffee7
-rw-r--r--background_scripts/main.coffee57
-rw-r--r--background_scripts/settings.coffee1
4 files changed, 87 insertions, 38 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee
index 23696185..dc5519d5 100644
--- a/background_scripts/completion.coffee
+++ b/background_scripts/completion.coffee
@@ -26,7 +26,6 @@ class Suggestion
generateHtml: ->
return @html if @html
- favIconUrl = @tabFavIconUrl or "#{@getUrlRoot(@url)}/favicon.ico"
relevancyHtml = if @showRelevancy then "<span class='relevancy'>#{@computeRelevancy()}</span>" else ""
# NOTE(philc): We're using these vimium-specific class names so we don't collide with the page's CSS.
@html =
@@ -35,8 +34,7 @@ class Suggestion
<span class="vimiumReset vomnibarSource">#{@type}</span>
<span class="vimiumReset vomnibarTitle">#{@highlightTerms(Utils.escapeHtml(@title))}</span>
</div>
- <div class="vimiumReset vomnibarBottomHalf vomnibarIcon"
- style="background-image: url(#{favIconUrl});">
+ <div class="vimiumReset vomnibarBottomHalf">
<span class="vimiumReset vomnibarUrl">#{@shortenUrl(@highlightTerms(Utils.escapeHtml(@url)))}</span>
#{relevancyHtml}
</div>
@@ -238,7 +236,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 +248,58 @@ 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. These are used to provide an initial
+# recency-based ordering in the tabs vomnibar (which allows jumping quickly between recently-visited tabs).
+class TabRecency
+ timestamp: 1
+ current: -1
+ cache: {}
+ lastVisited: null
+ lastVisitedTime: null
+ timeDelta: 500 # Milliseconds.
+
+ constructor: ->
+ chrome.tabs.onActivated.addListener (activeInfo) => @register activeInfo.tabId
+ chrome.tabs.onRemoved.addListener (tabId) => @deregister tabId
+
+ chrome.tabs.onReplaced.addListener (addedTabId, removedTabId) =>
+ @deregister removedTabId
+ @register addedTabId
+
+ register: (tabId) ->
+ currentTime = new Date()
+ # Register tabId if it has been visited for at least @timeDelta ms. Tabs which are visited only for a
+ # very-short time (e.g. those passed through with `5J`) aren't registered as visited at all.
+ if @lastVisitedTime? and @timeDelta <= currentTime - @lastVisitedTime
+ @cache[@lastVisited] = ++@timestamp
+
+ @current = @lastVisited = tabId
+ @lastVisitedTime = currentTime
+
+ deregister: (tabId) ->
+ if tabId == @lastVisited
+ # Ensure we don't register this tab, since it's going away.
+ @lastVisited = @lastVisitedTime = null
+ 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 tabId == @current 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) ->
@@ -269,12 +310,14 @@ class TabCompleter
suggestions = results.map (tab) =>
suggestion = new Suggestion(queryTerms, "tab", tab.url, tab.title, @computeRelevancy)
suggestion.tabId = tab.id
- suggestion.tabFavIconUrl = tab.favIconUrl
suggestion
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 +590,4 @@ root.SearchEngineCompleter = SearchEngineCompleter
root.HistoryCache = HistoryCache
root.RankingUtils = RankingUtils
root.RegexpCache = RegexpCache
+root.TabRecency = TabRecency
diff --git a/background_scripts/exclusions.coffee b/background_scripts/exclusions.coffee
index 3a8ef1e7..2b34238b 100644
--- a/background_scripts/exclusions.coffee
+++ b/background_scripts/exclusions.coffee
@@ -6,7 +6,12 @@ RegexpCache =
if regexp = @cache[pattern]
regexp
else
- @cache[pattern] = new RegExp("^" + pattern.replace(/\*/g, ".*") + "$")
+ @cache[pattern] =
+ # We use try/catch to ensure that a broken regexp doesn't wholly cripple Vimium.
+ try
+ new RegExp("^" + pattern.replace(/\*/g, ".*") + "$")
+ catch
+ /^$/ # Match the empty string.
# The Exclusions class manages the exclusion rule setting.
# An exclusion is an object with two attributes: pattern and passKeys.
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee
index b40907fb..3ec618c9 100644
--- a/background_scripts/main.coffee
+++ b/background_scripts/main.coffee
@@ -8,7 +8,7 @@ keyQueue = "" # Queue of keys typed
validFirstKeys = {}
singleKeyCommands = []
focusedFrame = null
-framesForTab = {}
+frameIdsForTab = {}
# Keys are either literal characters, or "named" - for example <a-b> (alt+b), <left> (left arrow) or <f12>
# This regular expression captures two groups: the first is a named key, the second is the remainder of
@@ -282,16 +282,14 @@ BackgroundCommands =
{ name: "toggleHelpDialog", dialogHtml: helpDialogHtml(), frameId:frameId }))
moveTabLeft: (count) -> moveTab(null, -count)
moveTabRight: (count) -> moveTab(null, count)
- nextFrame: (count) ->
+ nextFrame: (count,frameId) ->
chrome.tabs.getSelected(null, (tab) ->
- frames = framesForTab[tab.id].frames
- currIndex = getCurrFrameIndex(frames)
-
- # TODO: Skip the "top" frame (which doesn't actually have a <frame> tag),
- # since it exists only to contain the other frames.
- newIndex = (currIndex + count) % frames.length
-
- chrome.tabs.sendMessage(tab.id, { name: "focusFrame", frameId: frames[newIndex].id, highlight: true }))
+ frames = frameIdsForTab[tab.id]
+ # We can't always track which frame chrome has focussed, but here we learn that it's frameId; so add an
+ # additional offset such that we do indeed start from frameId.
+ count = (count + Math.max 0, frameIdsForTab[tab.id].indexOf frameId) % frames.length
+ frames = frameIdsForTab[tab.id] = [frames[count..]..., frames[0...count]...]
+ chrome.tabs.sendMessage(tab.id, { name: "focusFrame", frameId: frames[0], highlight: true }))
closeTabsOnLeft: -> removeTabsRelative "before"
closeTabsOnRight: -> removeTabsRelative "after"
@@ -347,7 +345,7 @@ updateOpenTabs = (tab) ->
scrollY: null
deletor: null
# Frames are recreated on refresh
- delete framesForTab[tab.id]
+ delete frameIdsForTab[tab.id]
setBrowserActionIcon = (tabId,path) ->
chrome.browserAction.setIcon({ tabId: tabId, path: path })
@@ -394,7 +392,7 @@ chrome.tabs.onUpdated.addListener (tabId, changeInfo, tab) ->
code: Settings.get("userDefinedLinkHintCss")
runAt: "document_start"
chrome.tabs.insertCSS tabId, cssConf, -> chrome.runtime.lastError
- updateOpenTabs(tab)
+ updateOpenTabs(tab) if changeInfo.url?
updateActiveState(tabId)
chrome.tabs.onAttached.addListener (tabId, attachedInfo) ->
@@ -429,7 +427,7 @@ chrome.tabs.onRemoved.addListener (tabId) ->
# scroll position)
tabInfoMap.deletor = -> delete tabInfoMap[tabId]
setTimeout tabInfoMap.deletor, 1000
- delete framesForTab[tabId]
+ delete frameIdsForTab[tabId]
chrome.tabs.onActiveChanged.addListener (tabId, selectInfo) -> updateActiveState(tabId)
@@ -554,9 +552,9 @@ checkKeyQueue = (keysToCheck, tabId, frameId) ->
refreshedCompletionKeys = true
else
if registryEntry.passCountToFunction
- BackgroundCommands[registryEntry.command](count)
+ BackgroundCommands[registryEntry.command](count, frameId)
else if registryEntry.noRepeat
- BackgroundCommands[registryEntry.command]()
+ BackgroundCommands[registryEntry.command](frameId)
else
repeatFunction(BackgroundCommands[registryEntry.command], count, 0, frameId)
@@ -603,21 +601,21 @@ openOptionsPageInNewTab = ->
chrome.tabs.create({ url: chrome.runtime.getURL("pages/options.html"), index: tab.index + 1 }))
registerFrame = (request, sender) ->
- unless framesForTab[sender.tab.id]
- framesForTab[sender.tab.id] = { frames: [] }
-
- if (request.is_top)
- focusedFrame = request.frameId
- framesForTab[sender.tab.id].total = request.total
+ (frameIdsForTab[sender.tab.id] ?= []).push request.frameId
- framesForTab[sender.tab.id].frames.push({ id: request.frameId })
-
-handleFrameFocused = (request, sender) -> focusedFrame = request.frameId
+unregisterFrame = (request, sender) ->
+ tabId = sender.tab.id
+ if frameIdsForTab[tabId]?
+ if request.tab_is_closing
+ updateOpenTabs sender.tab
+ else
+ frameIdsForTab[tabId] = frameIdsForTab[tabId].filter (id) -> id != request.frameId
-getCurrFrameIndex = (frames) ->
- for i in [0...frames.length]
- return i if frames[i].id == focusedFrame
- frames.length + 1
+handleFrameFocused = (request, sender) ->
+ tabId = sender.tab.id
+ if frameIdsForTab[tabId]?
+ frameIdsForTab[tabId] =
+ [request.frameId, (frameIdsForTab[tabId].filter (id) -> id != request.frameId)...]
# Port handler mapping
portHandlers =
@@ -633,6 +631,7 @@ sendRequestHandlers =
openUrlInCurrentTab: openUrlInCurrentTab,
openOptionsPageInNewTab: openOptionsPageInNewTab,
registerFrame: registerFrame,
+ unregisterFrame: unregisterFrame,
frameFocused: handleFrameFocused,
upgradeNotificationClosed: upgradeNotificationClosed,
updateScrollPosition: handleUpdateScrollPosition,
@@ -640,7 +639,7 @@ sendRequestHandlers =
isEnabledForUrl: isEnabledForUrl,
saveHelpDialogSettings: saveHelpDialogSettings,
selectSpecificTab: selectSpecificTab,
- refreshCompleter: refreshCompleter
+ refreshCompleter: refreshCompleter,
createMark: Marks.create.bind(Marks),
gotoMark: Marks.goto.bind(Marks)
diff --git a/background_scripts/settings.coffee b/background_scripts/settings.coffee
index d6e8fcde..4a63a5fb 100644
--- a/background_scripts/settings.coffee
+++ b/background_scripts/settings.coffee
@@ -61,6 +61,7 @@ root.Settings = Settings =
# or strings
defaults:
scrollStepSize: 60
+ smoothScroll: true
keyMappings: "# Insert your prefered key mappings here."
linkHintCharacters: "sadfjklewcmpgh"
linkHintNumbers: "0123456789"