@midscene/web 0.8.9 → 0.8.10-beta-20241225120902.0

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.
@@ -1945,33 +1945,11 @@ var scriptFileContent = async () => {
1945
1945
  }
1946
1946
  return import_node_fs3.default.readFileSync(scriptFileToRetrieve, "utf8");
1947
1947
  };
1948
- var lastTwoCallTime = [0, 0];
1949
- var callInterval = 1050;
1950
- async function getScreenshotBase64FromWindowId(windowId) {
1951
- const activeWindow = await chrome.windows.getAll({ populate: true });
1952
- if (activeWindow.find((w) => w.id === windowId) === void 0) {
1953
- throw new Error(`Window with id ${windowId} is not active`);
1954
- }
1955
- const now = Date.now();
1956
- if (now - lastTwoCallTime[0] < callInterval) {
1957
- const sleepTime = callInterval - (now - lastTwoCallTime[0]);
1958
- console.warn(
1959
- `Sleep for ${sleepTime}ms to avoid too frequent screenshot calls`
1960
- );
1961
- await new Promise((resolve) => setTimeout(resolve, sleepTime));
1962
- }
1963
- const base64 = await chrome.tabs.captureVisibleTab(windowId, {
1964
- format: "jpeg",
1965
- quality: 70
1966
- });
1967
- lastTwoCallTime.shift();
1968
- lastTwoCallTime.push(Date.now());
1969
- return base64;
1970
- }
1971
1948
  var ChromeExtensionProxyPage = class {
1972
1949
  constructor(tabId, windowId) {
1973
1950
  this.pageType = "chrome-extension-proxy";
1974
1951
  this.debuggerAttached = false;
1952
+ this.attachingDebugger = null;
1975
1953
  this.mouse = {
1976
1954
  click: async (x, y) => {
1977
1955
  await this.sendCommandToDebugger("Input.dispatchMouseEvent", {
@@ -2026,14 +2004,25 @@ var ChromeExtensionProxyPage = class {
2026
2004
  async attachDebugger() {
2027
2005
  if (this.debuggerAttached)
2028
2006
  return;
2029
- await chrome.debugger.attach({ tabId: this.tabId }, "1.3");
2030
- this.debuggerAttached = true;
2031
- chrome.debugger.onEvent.addListener((source, method, params) => {
2032
- console.log("debugger event", source, method, params);
2033
- if (method === "Debugger.detached") {
2034
- this.debuggerAttached = false;
2007
+ if (this.attachingDebugger) {
2008
+ await this.attachingDebugger;
2009
+ return;
2010
+ }
2011
+ this.attachingDebugger = (async () => {
2012
+ try {
2013
+ await chrome.debugger.attach({ tabId: this.tabId }, "1.3");
2014
+ this.debuggerAttached = true;
2015
+ chrome.debugger.onEvent.addListener((source, method, params) => {
2016
+ console.log("debugger event", source, method, params);
2017
+ if (method === "Debugger.detached") {
2018
+ this.debuggerAttached = false;
2019
+ }
2020
+ });
2021
+ } finally {
2022
+ this.attachingDebugger = null;
2035
2023
  }
2036
- });
2024
+ })();
2025
+ await this.attachingDebugger;
2037
2026
  }
2038
2027
  async detachDebugger() {
2039
2028
  if (!this.debuggerAttached)
@@ -2073,23 +2062,24 @@ var ChromeExtensionProxyPage = class {
2073
2062
  }
2074
2063
  return returnValue.result.value;
2075
2064
  }
2076
- async rectOfNodeId(nodeId) {
2077
- try {
2078
- const { model } = await this.sendCommandToDebugger(
2079
- "DOM.getBoxModel",
2080
- { nodeId }
2081
- );
2082
- return {
2083
- left: model.border[0],
2084
- top: model.border[1],
2085
- width: model.border[2] - model.border[0],
2086
- height: model.border[5] - model.border[1]
2087
- };
2088
- } catch (error) {
2089
- console.error("Error getting box model for nodeId", nodeId, error);
2090
- return null;
2091
- }
2092
- }
2065
+ // private async rectOfNodeId(nodeId: number): Promise<Rect | null> {
2066
+ // try {
2067
+ // const { model } =
2068
+ // await this.sendCommandToDebugger<CDPTypes.DOM.GetBoxModelResponse>(
2069
+ // 'DOM.getBoxModel',
2070
+ // { nodeId },
2071
+ // );
2072
+ // return {
2073
+ // left: model.border[0],
2074
+ // top: model.border[1],
2075
+ // width: model.border[2] - model.border[0],
2076
+ // height: model.border[5] - model.border[1],
2077
+ // };
2078
+ // } catch (error) {
2079
+ // console.error('Error getting box model for nodeId', nodeId, error);
2080
+ // return null;
2081
+ // }
2082
+ // }
2093
2083
  async getElementInfos() {
2094
2084
  const content = await this.getPageContentByCDP();
2095
2085
  if (content == null ? void 0 : content.size) {
@@ -2104,8 +2094,11 @@ var ChromeExtensionProxyPage = class {
2104
2094
  return content.size;
2105
2095
  }
2106
2096
  async screenshotBase64() {
2107
- const base64 = await getScreenshotBase64FromWindowId(this.windowId);
2108
- return base64;
2097
+ const base64 = await this.sendCommandToDebugger("Page.captureScreenshot", {
2098
+ format: "jpeg",
2099
+ quality: 70
2100
+ });
2101
+ return `data:image/jpeg;base64,${base64.data}`;
2109
2102
  }
2110
2103
  async url() {
2111
2104
  const url = await chrome.tabs.get(this.tabId).then((tab) => tab.url);