(_: unknown, tab?: Tabs.Tab)
| 84 | const companionTabs = new Set<number>(); |
| 85 | |
| 86 | const sendBootData = async (_: unknown, tab?: Tabs.Tab) => { |
| 87 | if (!tab?.url || !tab.id) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | const cacheData = getLocalBootData(); |
| 92 | if (cacheData?.settings?.optOutCompanion) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | const url = new URL(tab.url); |
| 97 | const href = url.origin + url.pathname + url.search; |
| 98 | if (shouldSkipCompanionUrl(url) || noPostCache.has(href)) { |
| 99 | if (!companionTabs.delete(tab.id)) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // Unmount the companion left over from a previous SPA route |
| 104 | await browser.tabs |
| 105 | .sendMessage(tab.id, { settings: cacheData?.settings ?? {}, url: href }) |
| 106 | .catch(() => undefined); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | const [deviceId, boot] = await Promise.all([ |
| 111 | getOrGenerateDeviceId(), |
| 112 | getBootData({ app: 'companion', url: href }), |
| 113 | ]); |
| 114 | |
| 115 | // Error responses carry no settings and must not be cached as misses |
| 116 | if (boot.settings && !boot.postData) { |
| 117 | noPostCache.add(href); |
| 118 | } |
| 119 | |
| 120 | if (boot.postData) { |
| 121 | companionTabs.add(tab.id); |
| 122 | } else { |
| 123 | companionTabs.delete(tab.id); |
| 124 | } |
| 125 | |
| 126 | let settingsOutput = boot.settings; |
| 127 | if (!cacheData?.user || !('providers' in cacheData?.user)) { |
| 128 | settingsOutput = { ...settingsOutput, ...cacheData?.settings }; |
| 129 | } |
| 130 | await browser.tabs.sendMessage(tab.id, { |
| 131 | ...boot, |
| 132 | deviceId, |
| 133 | url: href, |
| 134 | settings: settingsOutput, |
| 135 | }); |
| 136 | }; |
| 137 | |
| 138 | const sendRequestResponse = async ( |
| 139 | requestKey: string, |
no test coverage detected