| 112 | // Given a list of tabs candidate tabs, pick one. Prefer tabs in the current window and tabs with |
| 113 | // shorter (matching) URLs. |
| 114 | async function pickTab(tabs) { |
| 115 | // NOTE(philc): We assume getCurrent() can return null, but I didn't confirm this. Also, it should |
| 116 | // be impossible for the user to invoke Vimium-related keys if all windows are closed. |
| 117 | const window = await chrome.windows.getCurrent(); |
| 118 | const windowId = window?.id; |
| 119 | // Prefer tabs in the current window, if there are any. |
| 120 | const tabsInWindow = tabs.filter((tab) => tab.windowId === windowId); |
| 121 | if (tabsInWindow.length > 0) tabs = tabsInWindow; |
| 122 | // If more than one tab remains and the current tab is still a candidate, then don't pick the |
| 123 | // current tab (because jumping to it does nothing). |
| 124 | if (tabs.length > 1) { |
| 125 | tabs = tabs.filter((t) => !t.active); |
| 126 | } |
| 127 | |
| 128 | // Prefer shorter URLs. |
| 129 | tabs.sort((a, b) => a.url.length - b.url.length); |
| 130 | return tabs[0]; |
| 131 | } |