(tabId, timeoutMs = 12000)
| 42 | } |
| 43 | |
| 44 | function waitForTabReady(tabId, timeoutMs = 12000) { |
| 45 | return new Promise((resolve) => { |
| 46 | let settled = false; |
| 47 | const finish = () => { |
| 48 | if (settled) return; |
| 49 | settled = true; |
| 50 | chrome.tabs.onUpdated.removeListener(onUpdated); |
| 51 | clearTimeout(timer); |
| 52 | resolve(); |
| 53 | }; |
| 54 | const onUpdated = (updatedTabId, changeInfo) => { |
| 55 | if (updatedTabId === tabId && changeInfo.status === "complete") { |
| 56 | finish(); |
| 57 | } |
| 58 | }; |
| 59 | const timer = setTimeout(finish, timeoutMs); |
| 60 | |
| 61 | chrome.tabs.onUpdated.addListener(onUpdated); |
| 62 | chrome.tabs.get(tabId, (tab) => { |
| 63 | if (chrome.runtime.lastError) { |
| 64 | finish(); |
| 65 | return; |
| 66 | } |
| 67 | if (tab && tab.status === "complete") { |
| 68 | finish(); |
| 69 | } |
| 70 | }); |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | async function connectWS() { |
| 75 | if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) return; |
no test coverage detected