bb-browser 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -81,7 +81,9 @@ class SSEClient {
81
81
  data = trimmedLine.substring(5).trim();
82
82
  } else if (trimmedLine === "") {
83
83
  if (event && data) {
84
- await this.handleMessage(event, data);
84
+ this.handleMessage(event, data).catch(
85
+ (err) => console.error("[SSEClient] handleMessage error:", err)
86
+ );
85
87
  event = "";
86
88
  data = "";
87
89
  }
@@ -424,15 +426,15 @@ function initEventListeners() {
424
426
  });
425
427
  chrome.debugger.onDetach.addListener((source) => {
426
428
  if (source.tabId) {
427
- cleanupTab(source.tabId);
429
+ cleanupTab$2(source.tabId);
428
430
  console.log("[CDPService] Debugger detached from tab:", source.tabId);
429
431
  }
430
432
  });
431
433
  chrome.tabs.onRemoved.addListener((tabId) => {
432
- cleanupTab(tabId);
434
+ cleanupTab$2(tabId);
433
435
  });
434
436
  }
435
- function cleanupTab(tabId) {
437
+ function cleanupTab$2(tabId) {
436
438
  attachedTabs.delete(tabId);
437
439
  pendingDialogs.delete(tabId);
438
440
  networkRequests.delete(tabId);
@@ -584,13 +586,19 @@ function handleException(tabId, params) {
584
586
  jsErrors.set(tabId, errors);
585
587
  }
586
588
 
589
+ const tabSnapshotRefs$1 = /* @__PURE__ */ new Map();
590
+ const tabActiveFrameId$2 = /* @__PURE__ */ new Map();
591
+ function cleanupTab$1(tabId) {
592
+ tabSnapshotRefs$1.delete(tabId);
593
+ tabActiveFrameId$2.delete(tabId);
594
+ }
587
595
  async function getSnapshot$1(tabId, options = {}) {
588
596
  const { interactive = false } = options;
589
597
  console.log("[DOMService] Getting snapshot for tab:", tabId, { interactive });
590
598
  await injectBuildDomTreeScript(tabId);
591
599
  const domTreeResult = await executeBuildDomTree(tabId);
592
600
  const snapshotResult = interactive ? convertToAccessibilityTree(domTreeResult) : convertToFullTree(domTreeResult);
593
- snapshotResult.refs;
601
+ tabSnapshotRefs$1.set(tabId, snapshotResult.refs);
594
602
  console.log("[DOMService] Snapshot complete:", {
595
603
  mode: interactive ? "interactive" : "full",
596
604
  linesCount: snapshotResult.snapshot.split("\n").length,
@@ -599,6 +607,10 @@ async function getSnapshot$1(tabId, options = {}) {
599
607
  return snapshotResult;
600
608
  }
601
609
  function getFrameTarget(tabId) {
610
+ const frameId = tabActiveFrameId$2.get(tabId) ?? null;
611
+ if (frameId !== null) {
612
+ return { tabId, frameIds: [frameId] };
613
+ }
602
614
  return { tabId };
603
615
  }
604
616
  async function injectBuildDomTreeScript(tabId) {
@@ -883,22 +895,29 @@ function convertToFullTree(result) {
883
895
  return { snapshot: lines.join("\n"), refs };
884
896
  }
885
897
 
886
- let lastSnapshotRefs = {};
898
+ const tabSnapshotRefs = /* @__PURE__ */ new Map();
899
+ const tabActiveFrameId$1 = /* @__PURE__ */ new Map();
887
900
  async function loadRefsFromStorage() {
888
901
  try {
889
- const result = await chrome.storage.session.get("snapshotRefs");
890
- if (result.snapshotRefs) {
891
- lastSnapshotRefs = result.snapshotRefs;
892
- console.log("[CDPDOMService] Loaded refs from storage:", Object.keys(lastSnapshotRefs).length);
902
+ const result = await chrome.storage.session.get("tabSnapshotRefs");
903
+ if (result.tabSnapshotRefs) {
904
+ const stored = result.tabSnapshotRefs;
905
+ for (const [tabIdStr, refs] of Object.entries(stored)) {
906
+ tabSnapshotRefs.set(Number(tabIdStr), refs);
907
+ }
908
+ console.log("[CDPDOMService] Loaded refs from storage:", tabSnapshotRefs.size, "tabs");
893
909
  }
894
910
  } catch (e) {
895
911
  console.warn("[CDPDOMService] Failed to load refs from storage:", e);
896
912
  }
897
913
  }
898
- async function saveRefsToStorage(refs) {
914
+ async function saveRefsToStorage(tabId, refs) {
899
915
  try {
900
- await chrome.storage.session.set({ snapshotRefs: refs });
901
- console.log("[CDPDOMService] Saved refs to storage:", Object.keys(refs).length);
916
+ const result = await chrome.storage.session.get("tabSnapshotRefs");
917
+ const stored = result.tabSnapshotRefs || {};
918
+ stored[String(tabId)] = refs;
919
+ await chrome.storage.session.set({ tabSnapshotRefs: stored });
920
+ console.log("[CDPDOMService] Saved refs to storage for tab:", tabId, Object.keys(refs).length);
902
921
  } catch (e) {
903
922
  console.warn("[CDPDOMService] Failed to save refs to storage:", e);
904
923
  }
@@ -916,8 +935,8 @@ async function getSnapshot(tabId, options = {}) {
916
935
  tagName: refInfo.tagName
917
936
  };
918
937
  }
919
- lastSnapshotRefs = convertedRefs;
920
- await saveRefsToStorage(convertedRefs);
938
+ tabSnapshotRefs.set(tabId, convertedRefs);
939
+ await saveRefsToStorage(tabId, convertedRefs);
921
940
  console.log("[CDPDOMService] Snapshot complete:", {
922
941
  linesCount: result.snapshot.split("\n").length,
923
942
  refsCount: Object.keys(convertedRefs).length
@@ -927,15 +946,24 @@ async function getSnapshot(tabId, options = {}) {
927
946
  refs: convertedRefs
928
947
  };
929
948
  }
930
- async function getRefInfo(ref) {
949
+ async function getRefInfo(tabId, ref) {
931
950
  const refId = ref.startsWith("@") ? ref.slice(1) : ref;
932
- if (lastSnapshotRefs[refId]) {
933
- return lastSnapshotRefs[refId];
951
+ const refs = tabSnapshotRefs.get(tabId);
952
+ if (refs?.[refId]) {
953
+ return refs[refId];
934
954
  }
935
- if (Object.keys(lastSnapshotRefs).length === 0) {
955
+ if (!tabSnapshotRefs.has(tabId)) {
936
956
  await loadRefsFromStorage();
957
+ const loaded = tabSnapshotRefs.get(tabId);
958
+ if (loaded?.[refId]) {
959
+ return loaded[refId];
960
+ }
937
961
  }
938
- return lastSnapshotRefs[refId] || null;
962
+ return null;
963
+ }
964
+ function cleanupTab(tabId) {
965
+ tabSnapshotRefs.delete(tabId);
966
+ tabActiveFrameId$1.delete(tabId);
939
967
  }
940
968
  async function getElementCenterByXPath(tabId, xpath) {
941
969
  const result = await evaluate(tabId, `
@@ -966,7 +994,7 @@ async function getElementCenterByXPath(tabId, xpath) {
966
994
  return result;
967
995
  }
968
996
  async function clickElement(tabId, ref) {
969
- const refInfo = await getRefInfo(ref);
997
+ const refInfo = await getRefInfo(tabId, ref);
970
998
  if (!refInfo) {
971
999
  throw new Error(`Ref "${ref}" not found. Run snapshot first to get available refs.`);
972
1000
  }
@@ -977,7 +1005,7 @@ async function clickElement(tabId, ref) {
977
1005
  return { role, name };
978
1006
  }
979
1007
  async function hoverElement(tabId, ref) {
980
- const refInfo = await getRefInfo(ref);
1008
+ const refInfo = await getRefInfo(tabId, ref);
981
1009
  if (!refInfo) {
982
1010
  throw new Error(`Ref "${ref}" not found. Run snapshot first to get available refs.`);
983
1011
  }
@@ -988,7 +1016,7 @@ async function hoverElement(tabId, ref) {
988
1016
  return { role, name };
989
1017
  }
990
1018
  async function fillElement(tabId, ref, text) {
991
- const refInfo = await getRefInfo(ref);
1019
+ const refInfo = await getRefInfo(tabId, ref);
992
1020
  if (!refInfo) {
993
1021
  throw new Error(`Ref "${ref}" not found. Run snapshot first to get available refs.`);
994
1022
  }
@@ -1021,7 +1049,7 @@ async function fillElement(tabId, ref, text) {
1021
1049
  return { role, name };
1022
1050
  }
1023
1051
  async function typeElement(tabId, ref, text) {
1024
- const refInfo = await getRefInfo(ref);
1052
+ const refInfo = await getRefInfo(tabId, ref);
1025
1053
  if (!refInfo) {
1026
1054
  throw new Error(`Ref "${ref}" not found. Run snapshot first to get available refs.`);
1027
1055
  }
@@ -1049,7 +1077,7 @@ async function typeElement(tabId, ref, text) {
1049
1077
  return { role, name };
1050
1078
  }
1051
1079
  async function getElementText(tabId, ref) {
1052
- const refInfo = await getRefInfo(ref);
1080
+ const refInfo = await getRefInfo(tabId, ref);
1053
1081
  if (!refInfo) {
1054
1082
  throw new Error(`Ref "${ref}" not found. Run snapshot first to get available refs.`);
1055
1083
  }
@@ -1072,7 +1100,7 @@ async function getElementText(tabId, ref) {
1072
1100
  return text;
1073
1101
  }
1074
1102
  async function checkElement(tabId, ref) {
1075
- const refInfo = await getRefInfo(ref);
1103
+ const refInfo = await getRefInfo(tabId, ref);
1076
1104
  if (!refInfo) {
1077
1105
  throw new Error(`Ref "${ref}" not found. Run snapshot first to get available refs.`);
1078
1106
  }
@@ -1103,7 +1131,7 @@ async function checkElement(tabId, ref) {
1103
1131
  return { role, name, wasAlreadyChecked: result };
1104
1132
  }
1105
1133
  async function uncheckElement(tabId, ref) {
1106
- const refInfo = await getRefInfo(ref);
1134
+ const refInfo = await getRefInfo(tabId, ref);
1107
1135
  if (!refInfo) {
1108
1136
  throw new Error(`Ref "${ref}" not found. Run snapshot first to get available refs.`);
1109
1137
  }
@@ -1134,7 +1162,7 @@ async function uncheckElement(tabId, ref) {
1134
1162
  return { role, name, wasAlreadyUnchecked: result };
1135
1163
  }
1136
1164
  async function selectOption(tabId, ref, value) {
1137
- const refInfo = await getRefInfo(ref);
1165
+ const refInfo = await getRefInfo(tabId, ref);
1138
1166
  if (!refInfo) {
1139
1167
  throw new Error(`Ref "${ref}" not found. Run snapshot first to get available refs.`);
1140
1168
  }
@@ -1191,7 +1219,7 @@ async function selectOption(tabId, ref, value) {
1191
1219
  return { role, name, selectedValue, selectedLabel };
1192
1220
  }
1193
1221
  async function waitForElement(tabId, ref, maxWait = 1e4, interval = 200) {
1194
- const refInfo = await getRefInfo(ref);
1222
+ const refInfo = await getRefInfo(tabId, ref);
1195
1223
  if (!refInfo) {
1196
1224
  throw new Error(`Ref "${ref}" not found. Run snapshot first to get available refs.`);
1197
1225
  }
@@ -1219,8 +1247,9 @@ async function waitForElement(tabId, ref, maxWait = 1e4, interval = 200) {
1219
1247
  }
1220
1248
  throw new Error(`Timeout waiting for element @${ref} after ${maxWait}ms`);
1221
1249
  }
1222
- function setActiveFrameId(frameId) {
1223
- console.log("[CDPDOMService] Active frame changed:", frameId ?? "main");
1250
+ function setActiveFrameId(tabId, frameId) {
1251
+ tabActiveFrameId$1.set(tabId, frameId);
1252
+ console.log("[CDPDOMService] Active frame changed:", { tabId, frameId: frameId ?? "main" });
1224
1253
  }
1225
1254
  async function pressKey(tabId, key, modifiers = []) {
1226
1255
  let modifierFlags = 0;
@@ -1365,7 +1394,17 @@ chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, _tab) => {
1365
1394
  console.log("[TraceService] Initialized");
1366
1395
 
1367
1396
  initEventListeners();
1368
- let activeFrameId = null;
1397
+ const tabActiveFrameId = /* @__PURE__ */ new Map();
1398
+ async function resolveTab(command) {
1399
+ if (command.tabId !== void 0 && typeof command.tabId === "number") {
1400
+ return chrome.tabs.get(command.tabId);
1401
+ }
1402
+ const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
1403
+ if (!tab?.id) {
1404
+ throw new Error("No active tab found");
1405
+ }
1406
+ return tab;
1407
+ }
1369
1408
  async function handleCommand(command) {
1370
1409
  console.log("[CommandHandler] Processing command:", command.id, command.action);
1371
1410
  let result;
@@ -1530,8 +1569,8 @@ async function handleOpen(command) {
1530
1569
  };
1531
1570
  }
1532
1571
  async function handleSnapshot(command) {
1533
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1534
- if (!activeTab || !activeTab.id) {
1572
+ const activeTab = await resolveTab(command);
1573
+ if (!activeTab.id) {
1535
1574
  return {
1536
1575
  id: command.id,
1537
1576
  success: false,
@@ -1577,8 +1616,8 @@ async function handleClick(command) {
1577
1616
  error: "Missing ref parameter"
1578
1617
  };
1579
1618
  }
1580
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1581
- if (!activeTab || !activeTab.id) {
1619
+ const activeTab = await resolveTab(command);
1620
+ if (!activeTab.id) {
1582
1621
  return {
1583
1622
  id: command.id,
1584
1623
  success: false,
@@ -1614,8 +1653,8 @@ async function handleHover(command) {
1614
1653
  error: "Missing ref parameter"
1615
1654
  };
1616
1655
  }
1617
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1618
- if (!activeTab || !activeTab.id) {
1656
+ const activeTab = await resolveTab(command);
1657
+ if (!activeTab.id) {
1619
1658
  return {
1620
1659
  id: command.id,
1621
1660
  success: false,
@@ -1659,8 +1698,8 @@ async function handleFill(command) {
1659
1698
  error: "Missing text parameter"
1660
1699
  };
1661
1700
  }
1662
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1663
- if (!activeTab || !activeTab.id) {
1701
+ const activeTab = await resolveTab(command);
1702
+ if (!activeTab.id) {
1664
1703
  return {
1665
1704
  id: command.id,
1666
1705
  success: false,
@@ -1705,8 +1744,8 @@ async function handleType(command) {
1705
1744
  error: "Missing text parameter"
1706
1745
  };
1707
1746
  }
1708
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1709
- if (!activeTab || !activeTab.id) {
1747
+ const activeTab = await resolveTab(command);
1748
+ if (!activeTab.id) {
1710
1749
  return {
1711
1750
  id: command.id,
1712
1751
  success: false,
@@ -1743,8 +1782,8 @@ async function handleCheck(command) {
1743
1782
  error: "Missing ref parameter"
1744
1783
  };
1745
1784
  }
1746
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1747
- if (!activeTab || !activeTab.id) {
1785
+ const activeTab = await resolveTab(command);
1786
+ if (!activeTab.id) {
1748
1787
  return {
1749
1788
  id: command.id,
1750
1789
  success: false,
@@ -1781,8 +1820,8 @@ async function handleUncheck(command) {
1781
1820
  error: "Missing ref parameter"
1782
1821
  };
1783
1822
  }
1784
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1785
- if (!activeTab || !activeTab.id) {
1823
+ const activeTab = await resolveTab(command);
1824
+ if (!activeTab.id) {
1786
1825
  return {
1787
1826
  id: command.id,
1788
1827
  success: false,
@@ -1827,8 +1866,8 @@ async function handleSelect(command) {
1827
1866
  error: "Missing value parameter"
1828
1867
  };
1829
1868
  }
1830
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1831
- if (!activeTab || !activeTab.id) {
1869
+ const activeTab = await resolveTab(command);
1870
+ if (!activeTab.id) {
1832
1871
  return {
1833
1872
  id: command.id,
1834
1873
  success: false,
@@ -1858,8 +1897,8 @@ async function handleSelect(command) {
1858
1897
  }
1859
1898
  }
1860
1899
  async function handleClose(command) {
1861
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1862
- if (!activeTab || !activeTab.id) {
1900
+ const activeTab = await resolveTab(command);
1901
+ if (!activeTab.id) {
1863
1902
  return {
1864
1903
  id: command.id,
1865
1904
  success: false,
@@ -1872,6 +1911,9 @@ async function handleClose(command) {
1872
1911
  console.log("[CommandHandler] Closing tab:", tabId, url);
1873
1912
  try {
1874
1913
  await chrome.tabs.remove(tabId);
1914
+ cleanupTab$1(tabId);
1915
+ cleanupTab(tabId);
1916
+ tabActiveFrameId.delete(tabId);
1875
1917
  return {
1876
1918
  id: command.id,
1877
1919
  success: true,
@@ -1899,8 +1941,8 @@ async function handleGet(command) {
1899
1941
  error: "Missing attribute parameter"
1900
1942
  };
1901
1943
  }
1902
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1903
- if (!activeTab || !activeTab.id) {
1944
+ const activeTab = await resolveTab(command);
1945
+ if (!activeTab.id) {
1904
1946
  return {
1905
1947
  id: command.id,
1906
1948
  success: false,
@@ -1953,8 +1995,8 @@ async function handleGet(command) {
1953
1995
  }
1954
1996
  }
1955
1997
  async function handleScreenshot(command) {
1956
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
1957
- if (!activeTab || !activeTab.id || !activeTab.windowId) {
1998
+ const activeTab = await resolveTab(command);
1999
+ if (!activeTab.id || !activeTab.windowId) {
1958
2000
  return {
1959
2001
  id: command.id,
1960
2002
  success: false,
@@ -2009,8 +2051,8 @@ async function handleWait(command) {
2009
2051
  error: "Missing ref parameter"
2010
2052
  };
2011
2053
  }
2012
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2013
- if (!activeTab || !activeTab.id) {
2054
+ const activeTab = await resolveTab(command);
2055
+ if (!activeTab.id) {
2014
2056
  return {
2015
2057
  id: command.id,
2016
2058
  success: false,
@@ -2051,8 +2093,8 @@ async function handlePress(command) {
2051
2093
  error: "Missing key parameter"
2052
2094
  };
2053
2095
  }
2054
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2055
- if (!activeTab || !activeTab.id) {
2096
+ const activeTab = await resolveTab(command);
2097
+ if (!activeTab.id) {
2056
2098
  return {
2057
2099
  id: command.id,
2058
2100
  success: false,
@@ -2104,8 +2146,8 @@ async function handleScroll(command) {
2104
2146
  error: `Invalid direction: ${direction}`
2105
2147
  };
2106
2148
  }
2107
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2108
- if (!activeTab || !activeTab.id) {
2149
+ const activeTab = await resolveTab(command);
2150
+ if (!activeTab.id) {
2109
2151
  return {
2110
2152
  id: command.id,
2111
2153
  success: false,
@@ -2133,8 +2175,8 @@ async function handleScroll(command) {
2133
2175
  }
2134
2176
  }
2135
2177
  async function handleBack(command) {
2136
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2137
- if (!activeTab || !activeTab.id) {
2178
+ const activeTab = await resolveTab(command);
2179
+ if (!activeTab.id) {
2138
2180
  return {
2139
2181
  id: command.id,
2140
2182
  success: false,
@@ -2173,8 +2215,8 @@ async function handleBack(command) {
2173
2215
  }
2174
2216
  }
2175
2217
  async function handleForward(command) {
2176
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2177
- if (!activeTab || !activeTab.id) {
2218
+ const activeTab = await resolveTab(command);
2219
+ if (!activeTab.id) {
2178
2220
  return {
2179
2221
  id: command.id,
2180
2222
  success: false,
@@ -2205,8 +2247,8 @@ async function handleForward(command) {
2205
2247
  }
2206
2248
  }
2207
2249
  async function handleRefresh(command) {
2208
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2209
- if (!activeTab || !activeTab.id) {
2250
+ const activeTab = await resolveTab(command);
2251
+ if (!activeTab.id) {
2210
2252
  return {
2211
2253
  id: command.id,
2212
2254
  success: false,
@@ -2244,8 +2286,8 @@ async function handleEval(command) {
2244
2286
  error: "Missing script parameter"
2245
2287
  };
2246
2288
  }
2247
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2248
- if (!activeTab || !activeTab.id) {
2289
+ const activeTab = await resolveTab(command);
2290
+ if (!activeTab.id) {
2249
2291
  return {
2250
2292
  id: command.id,
2251
2293
  success: false,
@@ -2422,6 +2464,9 @@ async function handleTabClose(command) {
2422
2464
  const title = targetTab.title || "";
2423
2465
  const url = targetTab.url || "";
2424
2466
  await chrome.tabs.remove(tabId);
2467
+ cleanupTab$1(tabId);
2468
+ cleanupTab(tabId);
2469
+ tabActiveFrameId.delete(tabId);
2425
2470
  return {
2426
2471
  id: command.id,
2427
2472
  success: true,
@@ -2449,8 +2494,8 @@ async function handleFrame(command) {
2449
2494
  error: "Missing selector parameter"
2450
2495
  };
2451
2496
  }
2452
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2453
- if (!activeTab || !activeTab.id) {
2497
+ const activeTab = await resolveTab(command);
2498
+ if (!activeTab.id) {
2454
2499
  return {
2455
2500
  id: command.id,
2456
2501
  success: false,
@@ -2461,7 +2506,7 @@ async function handleFrame(command) {
2461
2506
  console.log("[CommandHandler] Switching to frame:", selector);
2462
2507
  try {
2463
2508
  const iframeInfoResults = await chrome.scripting.executeScript({
2464
- target: { tabId, frameIds: activeFrameId !== null ? [activeFrameId] : [0] },
2509
+ target: { tabId, frameIds: tabActiveFrameId.get(tabId) !== null && tabActiveFrameId.get(tabId) !== void 0 ? [tabActiveFrameId.get(tabId)] : [0] },
2465
2510
  func: (sel) => {
2466
2511
  const iframe = document.querySelector(sel);
2467
2512
  if (!iframe) {
@@ -2540,8 +2585,8 @@ async function handleFrame(command) {
2540
2585
  error: `无法访问 frame (frameId: ${targetFrameId}),可能是跨域 iframe`
2541
2586
  };
2542
2587
  }
2543
- activeFrameId = targetFrameId;
2544
- setActiveFrameId(String(targetFrameId));
2588
+ tabActiveFrameId.set(tabId, targetFrameId);
2589
+ setActiveFrameId(tabId, String(targetFrameId));
2545
2590
  const matchedFrameInfo = frames.find((f) => f.frameId === targetFrameId);
2546
2591
  return {
2547
2592
  id: command.id,
@@ -2566,8 +2611,17 @@ async function handleFrame(command) {
2566
2611
  }
2567
2612
  async function handleFrameMain(command) {
2568
2613
  console.log("[CommandHandler] Switching to main frame");
2569
- activeFrameId = null;
2570
- setActiveFrameId(null);
2614
+ const activeTab = await resolveTab(command);
2615
+ if (!activeTab.id) {
2616
+ return {
2617
+ id: command.id,
2618
+ success: false,
2619
+ error: "No active tab found"
2620
+ };
2621
+ }
2622
+ const tabId = activeTab.id;
2623
+ tabActiveFrameId.set(tabId, null);
2624
+ setActiveFrameId(tabId, null);
2571
2625
  return {
2572
2626
  id: command.id,
2573
2627
  success: true,
@@ -2588,8 +2642,8 @@ async function handleDialog(command) {
2588
2642
  error: "Missing or invalid dialogResponse parameter (accept/dismiss)"
2589
2643
  };
2590
2644
  }
2591
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2592
- if (!activeTab || !activeTab.id) {
2645
+ const activeTab = await resolveTab(command);
2646
+ if (!activeTab.id) {
2593
2647
  return {
2594
2648
  id: command.id,
2595
2649
  success: false,
@@ -2650,8 +2704,8 @@ function waitForTabLoad(tabId, timeout = 3e4) {
2650
2704
  });
2651
2705
  }
2652
2706
  async function handleNetwork(command) {
2653
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2654
- if (!activeTab || !activeTab.id) {
2707
+ const activeTab = await resolveTab(command);
2708
+ if (!activeTab.id) {
2655
2709
  return {
2656
2710
  id: command.id,
2657
2711
  success: false,
@@ -2742,8 +2796,8 @@ async function handleNetwork(command) {
2742
2796
  }
2743
2797
  }
2744
2798
  async function handleConsole(command) {
2745
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2746
- if (!activeTab || !activeTab.id) {
2799
+ const activeTab = await resolveTab(command);
2800
+ if (!activeTab.id) {
2747
2801
  return {
2748
2802
  id: command.id,
2749
2803
  success: false,
@@ -2791,8 +2845,8 @@ async function handleConsole(command) {
2791
2845
  }
2792
2846
  }
2793
2847
  async function handleErrors(command) {
2794
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2795
- if (!activeTab || !activeTab.id) {
2848
+ const activeTab = await resolveTab(command);
2849
+ if (!activeTab.id) {
2796
2850
  return {
2797
2851
  id: command.id,
2798
2852
  success: false,
@@ -2845,8 +2899,8 @@ async function handleTrace(command) {
2845
2899
  try {
2846
2900
  switch (subCommand) {
2847
2901
  case "start": {
2848
- const [activeTab] = await chrome.tabs.query({ active: true, currentWindow: true });
2849
- if (!activeTab || !activeTab.id) {
2902
+ const activeTab = await resolveTab(command);
2903
+ if (!activeTab.id) {
2850
2904
  return {
2851
2905
  id: command.id,
2852
2906
  success: false,