|
1 | | -import { InstanceType, SavedGroup, Stored, TabSessionData, UpgradeMsg } from 'src/types' |
| 1 | +import { Container, InstanceType, SavedGroup, Stored, TabSessionData, UpgradeMsg } from 'src/types' |
2 | 2 | import { UpgradingState } from 'src/types' |
3 | 3 | import { DEFAULT_SETTINGS, GROUP_URL, NOID, URL_URL, V4_GROUP_URL_LEN } from 'src/defaults' |
4 | 4 | import { V4_URL_URL_LEN } from 'src/defaults' |
@@ -123,6 +123,66 @@ void (async function main() { |
123 | 123 | if (newVersion <= currentVersion) browser.runtime.reload() |
124 | 124 | }) |
125 | 125 |
|
| 126 | + browser.omnibox.setDefaultSuggestion({ description: 'brooo' }) |
| 127 | + |
| 128 | + function matchContainers(input: string): Container[] { |
| 129 | + // TODO: order by score of some sort? |
| 130 | + // TODO: At the very least, put an exact match first. |
| 131 | + // TODO: Validate we want Sidebery records, not Firefox records. |
| 132 | + return Object.values(Containers.reactive.byId).filter(container => |
| 133 | + container.name.toLowerCase().includes(input.toLowerCase()) |
| 134 | + ) |
| 135 | + } |
| 136 | + |
| 137 | + browser.omnibox.onInputChanged.addListener(async (input, suggest) => { |
| 138 | + const suggestions = matchContainers(input).map(ctx => ({ |
| 139 | + content: ctx.name, |
| 140 | + description: ctx.name, |
| 141 | + deletable: false, |
| 142 | + })) |
| 143 | + suggest(suggestions) |
| 144 | + }) |
| 145 | + |
| 146 | + browser.omnibox.onInputEntered.addListener(async (input, _disposition) => { |
| 147 | + // NOTE: We're semantically _re-opening_ tabs, which conflicts with a disposition. Ignore it. |
| 148 | + |
| 149 | + if (!Windows.lastFocusedWinId) { |
| 150 | + Logs.err('omnibox: no last focused window ID found') |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + const matchingContainers = matchContainers(input) |
| 155 | + if (matchingContainers.length <= 0) { |
| 156 | + Logs.warn('omnibox: no matching containers found') |
| 157 | + return |
| 158 | + } |
| 159 | + const firstMatchingContainer = matchingContainers[0] |
| 160 | + |
| 161 | + const sidebarTabs = await Tabs.getSidebarTabs(Windows.lastFocusedWinId) |
| 162 | + if (!sidebarTabs) { |
| 163 | + Logs.err('omnibox: no sidebar tabs found for last focused window ID') |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + const con = IPC.getConnection(InstanceType.sidebar, Windows.lastFocusedWinId) |
| 168 | + if ((con?.localPort && con.localPort.error) || (con?.remotePort && con.remotePort.error)) { |
| 169 | + Logs.err('need to fall back to creating tabs by hand') |
| 170 | + return |
| 171 | + } |
| 172 | + |
| 173 | + const activeTabs = sidebarTabs.filter(tab => tab.active) |
| 174 | + try { |
| 175 | + await IPC.sidebar( |
| 176 | + Windows.lastFocusedWinId, |
| 177 | + 'reopenInContainer', |
| 178 | + activeTabs.map(tab => tab.id), |
| 179 | + firstMatchingContainer.id |
| 180 | + ) |
| 181 | + } catch { |
| 182 | + console.warn('failed to re-open tabs', activeTabs, 'in container', firstMatchingContainer) |
| 183 | + } |
| 184 | + }) |
| 185 | + |
126 | 186 | Logs.info(`Init end: ${performance.now() - ts}ms`) |
127 | 187 | })() |
128 | 188 |
|
|
0 commit comments