diff options
| author | Stephen Blott | 2016-09-24 09:51:11 +0100 |
|---|---|---|
| committer | GitHub | 2016-09-24 09:51:11 +0100 |
| commit | fc8ed2ae1b237b83974b7408b6a174a3af3ab9cc (patch) | |
| tree | b41bbff3950dc24b1c8120db2101e81cc6896507 /background_scripts/marks.coffee | |
| parent | a75e99c1a4a2ca327bf46dbf78b5ce973f3fe082 (diff) | |
| parent | d6b3845a8a9d7604bad7f1a24d87c39bdc116125 (diff) | |
| download | vimium-fc8ed2ae1b237b83974b7408b6a174a3af3ab9cc.tar.bz2 | |
Merge pull request #2250 from smblott-github/better-global-marks
Use prefix matching for global marks.
Diffstat (limited to 'background_scripts/marks.coffee')
| -rw-r--r-- | background_scripts/marks.coffee | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index 8fe30b2f..dbc14671 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -63,10 +63,14 @@ Marks = # The tab we're trying to find no longer exists. We either find another tab with a matching URL and use it, # or we create a new tab. focusOrLaunch: (markInfo, req) -> - chrome.tabs.query { url: markInfo.url }, (tabs) => + # If we're not going to be scrolling to a particular position in the tab, then we choose all tabs with a + # matching URL prefix. Otherwise, we require an exact match (because it doesn't make sense to scroll + # unless there's an exact URL match). + query = if markInfo.scrollX == markInfo.scrollY == 0 then "#{markInfo.url}*" else markInfo.url + chrome.tabs.query { url: query }, (tabs) => if 0 < tabs.length - # We have a matching tab: use it (prefering, if there are more than one, one in the current window). - @pickTabInWindow tabs, (tab) => + # We have at least one matching tab. Pick one and go to it. + @pickTab tabs, (tab) => @gotoPositionInTab extend markInfo, tabId: tab.id else # There is no existing matching tab, we'll have to create one. @@ -75,11 +79,19 @@ Marks = # is loaded, its DOM is ready and it registers with the background page. tabLoadedHandlers[tab.id] = => @gotoPositionInTab extend markInfo, tabId: tab.id - # Given a list of tabs, pick one in the current window, if possible, otherwise just pick any. - pickTabInWindow: (tabs, continuation) -> + # Given a list of tabs candidate tabs, pick one. Prefer tabs in the current window and tabs with shorter + # (matching) URLs. + pickTab: (tabs, callback) -> chrome.windows.getCurrent ({ id }) -> + # Prefer tabs in the current window, if there are any. tabsInWindow = tabs.filter (tab) -> tab.windowId == id - continuation tabsInWindow[0] ? tabs[0] + tabs = tabsInWindow if 0 < tabsInWindow.length + # If more than one tab remains and the current tab is still a candidate, then don't pick the current + # tab (because jumping to it does nothing). + tabs = (tab for tab in tabs when not tab.active) if 1 < tabs.length + # Prefer shorter URLs. + tabs.sort (a,b) -> a.url.length - b.url.length + callback tabs[0] root = exports ? window root.Marks = Marks |
