browserclaw 0.2.4 → 0.2.5

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.
package/dist/index.cjs CHANGED
@@ -701,10 +701,20 @@ async function pageTargetId(page) {
701
701
  }
702
702
  async function findPageByTargetId(browser, targetId, cdpUrl) {
703
703
  const pages = await getAllPages(browser);
704
+ let resolvedViaCdp = false;
704
705
  for (const page of pages) {
705
- const tid = await pageTargetId(page).catch(() => null);
706
+ let tid = null;
707
+ try {
708
+ tid = await pageTargetId(page);
709
+ resolvedViaCdp = true;
710
+ } catch {
711
+ tid = null;
712
+ }
706
713
  if (tid && tid === targetId) return page;
707
714
  }
715
+ if (!resolvedViaCdp && pages.length === 1) {
716
+ return pages[0];
717
+ }
708
718
  if (cdpUrl) {
709
719
  try {
710
720
  const listUrl = `${cdpUrl.replace(/\/+$/, "").replace(/^ws:/, "http:").replace(/\/cdp$/, "")}/json/list`;
@@ -835,6 +845,27 @@ function getIndentLevel(line) {
835
845
  const match = line.match(/^(\s*)/);
836
846
  return match ? Math.floor(match[1].length / 2) : 0;
837
847
  }
848
+ function matchInteractiveSnapshotLine(line, options) {
849
+ const depth = getIndentLevel(line);
850
+ if (options.maxDepth !== void 0 && depth > options.maxDepth) {
851
+ return null;
852
+ }
853
+ const match = line.match(/^(\s*-\s*)(\w+)(?:\s+"([^"]*)")?(.*)$/);
854
+ if (!match) {
855
+ return null;
856
+ }
857
+ const [, , roleRaw, name, suffix] = match;
858
+ if (roleRaw.startsWith("/")) {
859
+ return null;
860
+ }
861
+ const role = roleRaw.toLowerCase();
862
+ return {
863
+ roleRaw,
864
+ role,
865
+ ...name ? { name } : {},
866
+ suffix
867
+ };
868
+ }
838
869
  function createRoleNameTracker() {
839
870
  const counts = /* @__PURE__ */ new Map();
840
871
  const refsByKey = /* @__PURE__ */ new Map();
@@ -908,14 +939,11 @@ function buildRoleSnapshotFromAriaSnapshot(ariaSnapshot, options = {}) {
908
939
  if (options.interactive) {
909
940
  const result2 = [];
910
941
  for (const line of lines) {
911
- const depth = getIndentLevel(line);
912
- if (options.maxDepth !== void 0 && depth > options.maxDepth) continue;
913
- const match = line.match(/^(\s*-\s*)(\w+)(?:\s+"([^"]*)")?(.*)$/);
914
- if (!match) continue;
915
- const [, prefix, roleRaw, name, suffix] = match;
916
- if (roleRaw.startsWith("/")) continue;
917
- const role = roleRaw.toLowerCase();
942
+ const parsed = matchInteractiveSnapshotLine(line, options);
943
+ if (!parsed) continue;
944
+ const { roleRaw, role, name, suffix } = parsed;
918
945
  if (!INTERACTIVE_ROLES.has(role)) continue;
946
+ const prefix = line.match(/^(\s*-\s*)/)?.[1] ?? "";
919
947
  const ref = nextRef();
920
948
  const nth = tracker.getNextIndex(role, name);
921
949
  tracker.trackRef(role, name, ref);
@@ -978,16 +1006,13 @@ function buildRoleSnapshotFromAiSnapshot(aiSnapshot, options = {}) {
978
1006
  if (options.interactive) {
979
1007
  const out2 = [];
980
1008
  for (const line of lines) {
981
- const depth = getIndentLevel(line);
982
- if (options.maxDepth !== void 0 && depth > options.maxDepth) continue;
983
- const match = line.match(/^(\s*-\s*)(\w+)(?:\s+"([^"]*)")?(.*)$/);
984
- if (!match) continue;
985
- const [, prefix, roleRaw, name, suffix] = match;
986
- if (roleRaw.startsWith("/")) continue;
987
- const role = roleRaw.toLowerCase();
1009
+ const parsed = matchInteractiveSnapshotLine(line, options);
1010
+ if (!parsed) continue;
1011
+ const { roleRaw, role, name, suffix } = parsed;
988
1012
  if (!INTERACTIVE_ROLES.has(role)) continue;
989
1013
  const ref = parseAiSnapshotRef(suffix);
990
1014
  if (!ref) continue;
1015
+ const prefix = line.match(/^(\s*-\s*)/)?.[1] ?? "";
991
1016
  refs[ref] = { role, ...name ? { name } : {} };
992
1017
  out2.push(`${prefix}${roleRaw}${name ? ` "${name}"` : ""}${suffix}`);
993
1018
  }