@btraut/browser-bridge 0.8.0 → 0.8.1

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.
@@ -418,6 +418,10 @@ var DEBUGGER_PROTOCOL_VERSION = "1.3";
418
418
  var DEBUGGER_IDLE_TIMEOUT_KEY = "debuggerIdleTimeoutMs";
419
419
  var DEFAULT_DEBUGGER_IDLE_TIMEOUT_MS = 15e3;
420
420
  var DEFAULT_DEBUGGER_COMMAND_TIMEOUT_MS = 1e4;
421
+ var DEFAULT_SEND_TO_TAB_TIMEOUT_MS = 1e4;
422
+ var HISTORY_DISPATCH_TIMEOUT_MS = 2e3;
423
+ var HISTORY_NAVIGATION_SIGNAL_TIMEOUT_MS = 8e3;
424
+ var HISTORY_POST_NAV_DOM_GRACE_TIMEOUT_MS = 2e3;
421
425
  var AGENT_TAB_ID_KEY = "agentTabId";
422
426
  var AGENT_TAB_GROUP_TITLE = "\u{1F309} Browser Bridge";
423
427
  var nowIso = () => (/* @__PURE__ */ new Date()).toISOString();
@@ -809,14 +813,42 @@ var getDefaultTabId = async () => {
809
813
  return await getActiveTabId();
810
814
  }
811
815
  };
812
- var sendToTab = async (tabId, action, params) => {
816
+ var sendToTab = async (tabId, action, params, options) => {
817
+ const timeoutMs = typeof options?.timeoutMs === "number" && Number.isFinite(options.timeoutMs) ? Math.max(1, Math.floor(options.timeoutMs)) : DEFAULT_SEND_TO_TAB_TIMEOUT_MS;
813
818
  const attemptSend = async () => {
814
819
  return await new Promise((resolve) => {
815
820
  const message = { action, params };
821
+ let settled = false;
822
+ const finish = (result) => {
823
+ if (settled) {
824
+ return;
825
+ }
826
+ settled = true;
827
+ if (timeout !== void 0) {
828
+ clearTimeout(timeout);
829
+ }
830
+ resolve(result);
831
+ };
832
+ let timeout;
833
+ timeout = self.setTimeout(() => {
834
+ finish({
835
+ ok: false,
836
+ error: {
837
+ code: "TIMEOUT",
838
+ message: `Timed out waiting for content response after ${timeoutMs}ms.`,
839
+ retryable: true,
840
+ details: {
841
+ action,
842
+ tab_id: tabId,
843
+ timeout_ms: timeoutMs
844
+ }
845
+ }
846
+ });
847
+ }, timeoutMs);
816
848
  chrome.tabs.sendMessage(tabId, message, (response) => {
817
849
  const error = chrome.runtime.lastError;
818
850
  if (error) {
819
- resolve({
851
+ finish({
820
852
  ok: false,
821
853
  error: {
822
854
  code: "EVALUATION_FAILED",
@@ -827,7 +859,7 @@ var sendToTab = async (tabId, action, params) => {
827
859
  return;
828
860
  }
829
861
  if (!response || typeof response !== "object") {
830
- resolve({
862
+ finish({
831
863
  ok: false,
832
864
  error: {
833
865
  code: "EVALUATION_FAILED",
@@ -837,7 +869,7 @@ var sendToTab = async (tabId, action, params) => {
837
869
  });
838
870
  return;
839
871
  }
840
- resolve(response);
872
+ finish(response);
841
873
  });
842
874
  });
843
875
  };
@@ -863,6 +895,67 @@ var sendToTab = async (tabId, action, params) => {
863
895
  }
864
896
  };
865
897
  };
898
+ var waitForHistoryNavigationSignal = async (tabId, timeoutMs) => {
899
+ return await new Promise((resolve, reject) => {
900
+ let timeout;
901
+ const cleanup = () => {
902
+ if (timeout !== void 0) {
903
+ clearTimeout(timeout);
904
+ }
905
+ chrome.webNavigation.onCommitted.removeListener(onCommitted);
906
+ chrome.webNavigation.onHistoryStateUpdated.removeListener(
907
+ onHistoryStateUpdated
908
+ );
909
+ chrome.webNavigation.onReferenceFragmentUpdated.removeListener(
910
+ onReferenceFragmentUpdated
911
+ );
912
+ chrome.tabs.onUpdated.removeListener(onTabUpdated);
913
+ };
914
+ const resolveSignal = () => {
915
+ cleanup();
916
+ resolve();
917
+ };
918
+ const onCommitted = (details) => {
919
+ if (details.tabId !== tabId || details.frameId !== 0) {
920
+ return;
921
+ }
922
+ resolveSignal();
923
+ };
924
+ const onHistoryStateUpdated = (details) => {
925
+ if (details.tabId !== tabId || details.frameId !== 0) {
926
+ return;
927
+ }
928
+ resolveSignal();
929
+ };
930
+ const onReferenceFragmentUpdated = (details) => {
931
+ if (details.tabId !== tabId || details.frameId !== 0) {
932
+ return;
933
+ }
934
+ resolveSignal();
935
+ };
936
+ const onTabUpdated = (updatedTabId, changeInfo) => {
937
+ if (updatedTabId !== tabId) {
938
+ return;
939
+ }
940
+ if (typeof changeInfo.url !== "string" || changeInfo.url.length === 0) {
941
+ return;
942
+ }
943
+ resolveSignal();
944
+ };
945
+ chrome.webNavigation.onCommitted.addListener(onCommitted);
946
+ chrome.webNavigation.onHistoryStateUpdated.addListener(
947
+ onHistoryStateUpdated
948
+ );
949
+ chrome.webNavigation.onReferenceFragmentUpdated.addListener(
950
+ onReferenceFragmentUpdated
951
+ );
952
+ chrome.tabs.onUpdated.addListener(onTabUpdated);
953
+ timeout = self.setTimeout(() => {
954
+ cleanup();
955
+ reject(new Error("Timed out waiting for history navigation signal."));
956
+ }, timeoutMs);
957
+ });
958
+ };
866
959
  var waitForDomContentLoaded = async (tabId, timeoutMs) => {
867
960
  return await new Promise((resolve, reject) => {
868
961
  let timeout;
@@ -1313,21 +1406,37 @@ var DriveSocket = class {
1313
1406
  if (tabId === void 0) {
1314
1407
  tabId = await getDefaultTabId();
1315
1408
  }
1316
- const result = await sendToTab(tabId, message.action);
1317
- if (!result.ok) {
1409
+ const navigationSignal = waitForHistoryNavigationSignal(
1410
+ tabId,
1411
+ HISTORY_NAVIGATION_SIGNAL_TIMEOUT_MS
1412
+ );
1413
+ const result = await sendToTab(
1414
+ tabId,
1415
+ message.action,
1416
+ void 0,
1417
+ {
1418
+ timeoutMs: HISTORY_DISPATCH_TIMEOUT_MS
1419
+ }
1420
+ );
1421
+ if (!result.ok && result.error.code !== "TIMEOUT") {
1318
1422
  respondError(result.error);
1319
1423
  return;
1320
1424
  }
1321
1425
  markTabActive(tabId);
1322
1426
  try {
1323
- await waitForDomContentLoaded(tabId, 3e4);
1324
- } catch (error) {
1325
- respondError({
1326
- code: "TIMEOUT",
1327
- message: error instanceof Error ? error.message : "Timed out waiting.",
1328
- retryable: true
1329
- });
1330
- return;
1427
+ await navigationSignal;
1428
+ try {
1429
+ await waitForDomContentLoaded(
1430
+ tabId,
1431
+ HISTORY_POST_NAV_DOM_GRACE_TIMEOUT_MS
1432
+ );
1433
+ } catch {
1434
+ }
1435
+ } catch {
1436
+ if (!result.ok) {
1437
+ respondError(result.error);
1438
+ return;
1439
+ }
1331
1440
  }
1332
1441
  respondOk({ ok: true });
1333
1442
  return;
@@ -1901,10 +2010,14 @@ var DriveSocket = class {
1901
2010
  if (tabId === void 0) {
1902
2011
  tabId = await getDefaultTabId();
1903
2012
  }
2013
+ const timeoutMs = message.action === "drive.wait_for" && typeof params.timeout_ms === "number" && Number.isFinite(params.timeout_ms) ? Math.max(1, Math.floor(params.timeout_ms) + 1e3) : void 0;
1904
2014
  const result = await sendToTab(
1905
2015
  tabId,
1906
2016
  message.action,
1907
- params
2017
+ params,
2018
+ {
2019
+ timeoutMs
2020
+ }
1908
2021
  );
1909
2022
  if (result.ok) {
1910
2023
  respondOk(result.result ?? { ok: true });