@midscene/web 0.7.2 → 0.7.3-beta-20241104100519.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.
package/dist/es/debug.js CHANGED
@@ -1,129 +1,150 @@
1
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
- }) : x)(function(x) {
4
- if (typeof require !== "undefined")
5
- return require.apply(this, arguments);
6
- throw Error('Dynamic require of "' + x + '" is not supported');
7
- });
8
-
9
- // src/debug/index.ts
10
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
11
- import path from "path";
12
-
13
- // src/extractor/constants.ts
14
- import {
15
- NodeType,
16
- TEXT_MAX_SIZE,
17
- TEXT_SIZE_THRESHOLD
18
- } from "@midscene/shared/constants";
19
-
20
- // src/debug/index.ts
21
- import {
22
- processImageElementInfo,
23
- resizeImg,
24
- saveBase64Image
25
- } from "@midscene/shared/img";
26
- async function generateExtractData(page, targetDir, saveImgType) {
27
- const file = await page.screenshot();
28
- const screenshotBuffer = readFileSync(file);
29
- const inputImgBase64 = screenshotBuffer.toString("base64");
30
- const {
31
- elementsPositionInfo,
32
- captureElementSnapshot,
33
- elementsPositionInfoWithoutText
34
- } = await getElementInfos(page);
35
- const inputImagePath = path.join(targetDir, "input.png");
36
- const outputImagePath = path.join(targetDir, "output.png");
37
- const outputWithoutTextImgPath = path.join(
38
- targetDir,
39
- "output_without_text.png"
40
- );
41
- const resizeOutputImgPath = path.join(targetDir, "resize-output.png");
42
- const snapshotJsonPath = path.join(targetDir, "element-snapshot.json");
43
- const {
44
- compositeElementInfoImgBase64,
45
- compositeElementInfoImgWithoutTextBase64
46
- } = await processImageElementInfo({
47
- elementsPositionInfo,
48
- elementsPositionInfoWithoutText,
49
- inputImgBase64
50
- });
51
- const resizeImgBase64 = await resizeImg(inputImgBase64);
52
- const existingSnapshot = existsSync(snapshotJsonPath) ? JSON.parse(readFileSync(snapshotJsonPath, "utf-8")) : null;
53
- if (existingSnapshot && JSON.stringify(existingSnapshot) === JSON.stringify(captureElementSnapshot)) {
54
- console.log("skip save snapshot for ", targetDir);
55
- return;
56
- }
57
- if (!(saveImgType == null ? void 0 : saveImgType.disableSnapshot)) {
58
- writeFileSyncWithDir(
59
- snapshotJsonPath,
60
- JSON.stringify(captureElementSnapshot, null, 2)
61
- );
62
- }
63
- if (!(saveImgType == null ? void 0 : saveImgType.disableInputImage)) {
64
- await saveBase64Image({
65
- base64Data: inputImgBase64,
66
- outputPath: inputImagePath
67
- });
68
- }
69
- if (!(saveImgType == null ? void 0 : saveImgType.disableOutputImage)) {
70
- await saveBase64Image({
71
- base64Data: compositeElementInfoImgBase64,
72
- outputPath: outputImagePath
1
+ (function(global, factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") factory(exports, require("fs"), require("path"), require("@midscene/shared/constants"), require("@midscene/shared/img"));
3
+ else if (typeof define === "function" && define.amd) define([
4
+ "exports",
5
+ "fs",
6
+ "path",
7
+ "@midscene/shared/constants",
8
+ "@midscene/shared/img"
9
+ ], factory);
10
+ else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.debug = {}, global.fs, global.path, global.constants, global.img);
11
+ })(this, function(exports, _fs, _path, _constants, _img) {
12
+ "use strict";
13
+ Object.defineProperty(exports, "__esModule", {
14
+ value: true
73
15
  });
74
- }
75
- if (!(saveImgType == null ? void 0 : saveImgType.disableOutputWithoutTextImg)) {
76
- await saveBase64Image({
77
- base64Data: compositeElementInfoImgWithoutTextBase64,
78
- outputPath: outputWithoutTextImgPath
16
+ function _export(target, all) {
17
+ for(var name in all)Object.defineProperty(target, name, {
18
+ enumerable: true,
19
+ get: all[name]
20
+ });
21
+ }
22
+ _export(exports, {
23
+ generateExtractData: function() {
24
+ return generateExtractData;
25
+ },
26
+ generateTestDataPath: function() {
27
+ return generateTestDataPath;
28
+ },
29
+ getElementInfos: function() {
30
+ return getElementInfos;
31
+ },
32
+ writeFileSyncWithDir: function() {
33
+ return writeFileSyncWithDir;
34
+ }
79
35
  });
80
- }
81
- if (!(saveImgType == null ? void 0 : saveImgType.disableResizeOutputImg)) {
82
- await saveBase64Image({
83
- base64Data: resizeImgBase64,
84
- outputPath: resizeOutputImgPath
36
+ _path = /*#__PURE__*/ _interop_require_default(_path);
37
+ function _interop_require_default(obj) {
38
+ return obj && obj.__esModule ? obj : {
39
+ default: obj
40
+ };
41
+ }
42
+ var __require = /* @__PURE__ */ ((x)=>typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
43
+ get: (a, b)=>(typeof require !== "undefined" ? require : a)[b]
44
+ }) : x)(function(x) {
45
+ if (typeof require !== "undefined") return require.apply(this, arguments);
46
+ throw Error('Dynamic require of "' + x + '" is not supported');
85
47
  });
86
- }
87
- }
88
- function generateTestDataPath(testDataName) {
89
- const modulePath = __require.resolve("@midscene/core").replace("dist/lib/index.js", "");
90
- const midsceneTestDataPath = path.join(
91
- modulePath,
92
- `tests/ai/evaluate/test-data/${testDataName}`
93
- );
94
- return midsceneTestDataPath;
95
- }
96
- function ensureDirectoryExistence(filePath) {
97
- const dirname = path.dirname(filePath);
98
- if (existsSync(dirname)) {
99
- return;
100
- }
101
- ensureDirectoryExistence(dirname);
102
- mkdirSync(dirname);
103
- }
104
- function writeFileSyncWithDir(filePath, content, options = {}) {
105
- ensureDirectoryExistence(filePath);
106
- writeFileSync(filePath, content, options);
107
- }
108
- async function getElementInfos(page) {
109
- const captureElementSnapshot = await page.getElementInfos();
110
- const elementsPositionInfoWithoutText = captureElementSnapshot.filter(
111
- (elementInfo) => {
112
- if (elementInfo.attributes.nodeType === NodeType.TEXT) {
113
- return false;
114
- }
115
- return true;
48
+ var __async = (__this, __arguments, generator)=>{
49
+ return new Promise((resolve, reject)=>{
50
+ var fulfilled = (value)=>{
51
+ try {
52
+ step(generator.next(value));
53
+ } catch (e) {
54
+ reject(e);
55
+ }
56
+ };
57
+ var rejected = (value)=>{
58
+ try {
59
+ step(generator.throw(value));
60
+ } catch (e) {
61
+ reject(e);
62
+ }
63
+ };
64
+ var step = (x)=>x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
65
+ step((generator = generator.apply(__this, __arguments)).next());
66
+ });
67
+ };
68
+ function generateExtractData(page, targetDir, saveImgType) {
69
+ return __async(this, null, function*() {
70
+ const inputImgBase64 = yield page.screenshotBase64();
71
+ const { elementsPositionInfo, captureElementSnapshot, elementsPositionInfoWithoutText } = yield getElementInfos(page);
72
+ const inputImagePath = _path.default.join(targetDir, "input.png");
73
+ const outputImagePath = _path.default.join(targetDir, "output.png");
74
+ const outputWithoutTextImgPath = _path.default.join(targetDir, "output_without_text.png");
75
+ const resizeOutputImgPath = _path.default.join(targetDir, "resize-output.png");
76
+ const snapshotJsonPath = _path.default.join(targetDir, "element-snapshot.json");
77
+ const { compositeElementInfoImgBase64, compositeElementInfoImgWithoutTextBase64 } = yield (0, _img.processImageElementInfo)({
78
+ elementsPositionInfo,
79
+ elementsPositionInfoWithoutText,
80
+ inputImgBase64
81
+ });
82
+ const resizedImg = yield (0, _img.resizeImgBase64)(inputImgBase64, void 0);
83
+ const existingSnapshot = (0, _fs.existsSync)(snapshotJsonPath) ? JSON.parse((0, _fs.readFileSync)(snapshotJsonPath, "utf-8")) : null;
84
+ if (existingSnapshot && JSON.stringify(existingSnapshot) === JSON.stringify(captureElementSnapshot)) {
85
+ console.log("skip save snapshot for ", targetDir);
86
+ return;
87
+ }
88
+ if (!(saveImgType == null ? void 0 : saveImgType.disableSnapshot)) {
89
+ writeFileSyncWithDir(snapshotJsonPath, JSON.stringify(captureElementSnapshot, null, 2));
90
+ }
91
+ if (!(saveImgType == null ? void 0 : saveImgType.disableInputImage)) {
92
+ yield (0, _img.saveBase64Image)({
93
+ base64Data: inputImgBase64,
94
+ outputPath: inputImagePath
95
+ });
96
+ }
97
+ if (!(saveImgType == null ? void 0 : saveImgType.disableOutputImage)) {
98
+ yield (0, _img.saveBase64Image)({
99
+ base64Data: compositeElementInfoImgBase64,
100
+ outputPath: outputImagePath
101
+ });
102
+ }
103
+ if (!(saveImgType == null ? void 0 : saveImgType.disableOutputWithoutTextImg)) {
104
+ yield (0, _img.saveBase64Image)({
105
+ base64Data: compositeElementInfoImgWithoutTextBase64,
106
+ outputPath: outputWithoutTextImgPath
107
+ });
108
+ }
109
+ if (!(saveImgType == null ? void 0 : saveImgType.disableResizeOutputImg)) {
110
+ yield (0, _img.saveBase64Image)({
111
+ base64Data: resizedImg,
112
+ outputPath: resizeOutputImgPath
113
+ });
114
+ }
115
+ });
116
+ }
117
+ function generateTestDataPath(testDataName) {
118
+ const modulePath = __require.resolve("@midscene/core").replace("dist/lib/index.js", "");
119
+ const midsceneTestDataPath = _path.default.join(modulePath, `tests/ai/evaluate/test-data/${testDataName}`);
120
+ return midsceneTestDataPath;
116
121
  }
117
- );
118
- return {
119
- elementsPositionInfo: captureElementSnapshot,
120
- captureElementSnapshot,
121
- elementsPositionInfoWithoutText
122
- };
123
- }
124
- export {
125
- generateExtractData,
126
- generateTestDataPath,
127
- getElementInfos,
128
- writeFileSyncWithDir
129
- };
122
+ function ensureDirectoryExistence(filePath) {
123
+ const dirname = _path.default.dirname(filePath);
124
+ if ((0, _fs.existsSync)(dirname)) {
125
+ return;
126
+ }
127
+ ensureDirectoryExistence(dirname);
128
+ (0, _fs.mkdirSync)(dirname);
129
+ }
130
+ function writeFileSyncWithDir(filePath, content, options = {}) {
131
+ ensureDirectoryExistence(filePath);
132
+ (0, _fs.writeFileSync)(filePath, content, options);
133
+ }
134
+ function getElementInfos(page) {
135
+ return __async(this, null, function*() {
136
+ const captureElementSnapshot = yield page.getElementInfos();
137
+ const elementsPositionInfoWithoutText = captureElementSnapshot.filter((elementInfo)=>{
138
+ if (elementInfo.attributes.nodeType === _constants.NodeType.TEXT) {
139
+ return false;
140
+ }
141
+ return true;
142
+ });
143
+ return {
144
+ elementsPositionInfo: captureElementSnapshot,
145
+ captureElementSnapshot,
146
+ elementsPositionInfoWithoutText
147
+ };
148
+ });
149
+ }
150
+ });