| 1278 | }, |
| 1279 | |
| 1280 | async command(command, extra = {}) { |
| 1281 | this.error = ""; |
| 1282 | this.annotationError = ""; |
| 1283 | this.beginCommand(); |
| 1284 | const previousActiveBrowserId = this.activeBrowserId; |
| 1285 | const previousActiveContextId = this.activeBrowserContextId; |
| 1286 | const commandName = String(command || "").toLowerCase(); |
| 1287 | try { |
| 1288 | const targetContextId = commandName === "open" |
| 1289 | ? await this.contextIdForNewBrowser() |
| 1290 | : this.normalizeContextId(extra.context_id || extra.contextId) || await this.contextIdForActiveBrowser(); |
| 1291 | const targetBrowserId = this.normalizeBrowserId(extra.browser_id ?? this.activeBrowserId); |
| 1292 | this.contextId = targetContextId; |
| 1293 | const response = await websocket.request( |
| 1294 | "browser_viewer_command", |
| 1295 | { |
| 1296 | ...extra, |
| 1297 | context_id: targetContextId, |
| 1298 | browser_id: targetBrowserId, |
| 1299 | viewer_id: this._viewerToken, |
| 1300 | viewer_transport: this.requestedViewerTransport(), |
| 1301 | command, |
| 1302 | }, |
| 1303 | { timeoutMs: 20000 }, |
| 1304 | ); |
| 1305 | const data = firstOk(response); |
| 1306 | this.applyBrowserListing(data.browsers || [], targetContextId, { replaceAll: Boolean(data.all_browsers) }); |
| 1307 | this.viewerTransport = this.normalizeViewerTransport(data.viewer_transport); |
| 1308 | const result = data.result || {}; |
| 1309 | const resultContextId = this.normalizeContextId( |
| 1310 | result.context_id |
| 1311 | || result.state?.context_id |
| 1312 | || data.active_browser_context_id |
| 1313 | || targetContextId |
| 1314 | ); |
| 1315 | const preferredBrowser = this.browserById( |
| 1316 | result.id |
| 1317 | || result.state?.id |
| 1318 | || result.last_interacted_browser_id |
| 1319 | || data.last_interacted_browser_id, |
| 1320 | resultContextId, |
| 1321 | ) |
| 1322 | || this.browserById(this.activeBrowserId, this.activeBrowserContextId) |
| 1323 | || this.firstBrowser(resultContextId) |
| 1324 | || this.firstBrowser(); |
| 1325 | this.setActiveBrowserId(preferredBrowser?.id || null, preferredBrowser?.context_id || resultContextId); |
| 1326 | this.applyActiveFrameState( |
| 1327 | this.stateFromCommandResult(result) |
| 1328 | || this.browserById(this.activeBrowserId, this.activeBrowserContextId) |
| 1329 | ); |
| 1330 | if (!this.activeBrowserId) { |
| 1331 | this.frameState = null; |
| 1332 | this.frameSrc = ""; |
| 1333 | } |
| 1334 | if (result.state?.currentUrl || result.currentUrl) { |
| 1335 | this.address = result.state?.currentUrl || result.currentUrl; |
| 1336 | } |
| 1337 | this.applySnapshot(data.snapshot); |