@dev-blinq/cucumber_client 1.0.1422-dev â 1.0.1422-stage
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/bin/assets/bundled_scripts/recorder.js +220 -0
- package/bin/assets/preload/css_gen.js +10 -10
- package/bin/assets/preload/toolbar.js +27 -29
- package/bin/assets/preload/unique_locators.js +1 -1
- package/bin/assets/preload/yaml.js +288 -275
- package/bin/assets/scripts/aria_snapshot.js +223 -220
- package/bin/assets/scripts/dom_attr.js +329 -329
- package/bin/assets/scripts/dom_parent.js +169 -174
- package/bin/assets/scripts/event_utils.js +94 -94
- package/bin/assets/scripts/pw.js +2050 -1949
- package/bin/assets/scripts/recorder.js +70 -45
- package/bin/assets/scripts/snapshot_capturer.js +147 -147
- package/bin/assets/scripts/unique_locators.js +163 -44
- package/bin/assets/scripts/yaml.js +796 -783
- package/bin/assets/templates/_hooks_template.txt +6 -2
- package/bin/assets/templates/utils_template.txt +16 -16
- package/bin/client/code_cleanup/find_step_definition_references.js +0 -1
- package/bin/client/code_gen/api_codegen.js +2 -2
- package/bin/client/code_gen/code_inversion.js +63 -2
- package/bin/client/code_gen/function_signature.js +4 -0
- package/bin/client/code_gen/page_reflection.js +48 -11
- package/bin/client/code_gen/playwright_codeget.js +25 -3
- package/bin/client/cucumber/feature_data.js +2 -2
- package/bin/client/cucumber/project_to_document.js +8 -2
- package/bin/client/cucumber/steps_definitions.js +19 -3
- package/bin/client/local_agent.js +3 -2
- package/bin/client/parse_feature_file.js +23 -26
- package/bin/client/playground/projects/env.json +2 -2
- package/bin/client/recorderv3/bvt_init.js +363 -0
- package/bin/client/recorderv3/bvt_recorder.js +1008 -47
- package/bin/client/recorderv3/implemented_steps.js +2 -0
- package/bin/client/recorderv3/index.js +4 -283
- package/bin/client/recorderv3/scriptTest.js +1 -1
- package/bin/client/recorderv3/services.js +810 -142
- package/bin/client/recorderv3/step_runner.js +37 -11
- package/bin/client/recorderv3/step_utils.js +509 -39
- package/bin/client/recorderv3/update_feature.js +32 -13
- package/bin/client/recorderv3/wbr_entry.js +61 -0
- package/bin/client/recording.js +1 -0
- package/bin/client/upload-service.js +4 -2
- package/bin/client/utils/socket_logger.js +1 -1
- package/bin/index.js +4 -1
- package/package.json +7 -5
|
@@ -2,150 +2,818 @@ import { readdirSync, readFileSync } from "fs";
|
|
|
2
2
|
import { getRunsServiceBaseURL } from "../utils/index.js";
|
|
3
3
|
import { axiosClient } from "../utils/axiosClient.js";
|
|
4
4
|
import path from "path";
|
|
5
|
-
|
|
5
|
+
import { EventEmitter } from "events";
|
|
6
|
+
import { v4 as uuidv4 } from "uuid"; // Import uuid
|
|
6
7
|
export class NamesService {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
8
|
+
screenshotMap;
|
|
9
|
+
TOKEN;
|
|
10
|
+
projectDir;
|
|
11
|
+
logger;
|
|
12
|
+
constructor({ screenshotMap, TOKEN, projectDir, logger }) {
|
|
13
|
+
this.screenshotMap = screenshotMap;
|
|
14
|
+
this.TOKEN = TOKEN;
|
|
15
|
+
this.projectDir = projectDir;
|
|
16
|
+
this.logger = logger;
|
|
17
|
+
}
|
|
18
|
+
//@ts-expect-error
|
|
19
|
+
async generateStepName({ commands, stepsNames, parameters, map }) {
|
|
20
|
+
let screenshot = this.screenshotMap.get(commands[commands.length - 1].inputID);
|
|
21
|
+
if (!screenshot && commands.length > 1) {
|
|
22
|
+
screenshot = this.screenshotMap.get(commands[commands.length - 2].inputID);
|
|
23
|
+
}
|
|
24
|
+
const url = `${getRunsServiceBaseURL()}/generate-step-information/generate`;
|
|
25
|
+
const TIMEOUT = 120; // 2 minutes
|
|
26
|
+
const { data } = await axiosClient({
|
|
27
|
+
url,
|
|
28
|
+
method: "POST",
|
|
29
|
+
data: {
|
|
30
|
+
commands,
|
|
31
|
+
stepsNames,
|
|
32
|
+
parameters,
|
|
33
|
+
//screenshot,
|
|
34
|
+
map,
|
|
35
|
+
},
|
|
36
|
+
headers: {
|
|
37
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
38
|
+
"X-Source": "recorder",
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
const actionUrl = `${getRunsServiceBaseURL()}/action/get-action?id=${data.id}`;
|
|
42
|
+
let result = { status: 500 };
|
|
43
|
+
for (let i = 0; i < TIMEOUT; i++) {
|
|
44
|
+
try {
|
|
45
|
+
const action = await axiosClient({
|
|
46
|
+
url: actionUrl,
|
|
47
|
+
method: "GET",
|
|
48
|
+
headers: {
|
|
49
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
50
|
+
"X-Source": "recorder",
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
if (action.data.status) {
|
|
54
|
+
result = action;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (i === TIMEOUT - 1) {
|
|
60
|
+
this.logger.error("Timeout while generating step details: ", error instanceof Error ? error : { message: JSON.stringify(error) });
|
|
61
|
+
console.error("Timeout while generating step details: ", error instanceof Error ? error.message : error);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
65
|
+
}
|
|
66
|
+
if (result.status !== 200) {
|
|
67
|
+
return { success: false, message: "Error while generating step details" };
|
|
68
|
+
}
|
|
69
|
+
return result.data;
|
|
70
|
+
}
|
|
71
|
+
async generateScenarioAndFeatureNames(scenarioAsText) {
|
|
72
|
+
if (!this.projectDir) {
|
|
73
|
+
throw new Error("Project directory not found");
|
|
74
|
+
}
|
|
75
|
+
// read all files with .feature extension into an object {files:[{name: "", content: ""}]}
|
|
76
|
+
const featureFiles = readdirSync(path.join(this.projectDir, "features"));
|
|
77
|
+
const featureFilesContent = featureFiles
|
|
78
|
+
.filter((file) => file.endsWith(".feature"))
|
|
79
|
+
.map((file) => {
|
|
80
|
+
return {
|
|
81
|
+
name: file,
|
|
82
|
+
content: readFileSync(path.join(this.projectDir, "features", file), "utf8"),
|
|
83
|
+
};
|
|
47
84
|
});
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
} catch (error) {
|
|
54
|
-
if (i === TIMEOUT - 1) {
|
|
55
|
-
this.logger.error("Timeout while generating step details: ", error);
|
|
56
|
-
console.error("Timeout while generating step details: ", error);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (result.status !== 200) {
|
|
64
|
-
return { success: false, message: "Error while generating step details" };
|
|
65
|
-
}
|
|
66
|
-
return result.data;
|
|
67
|
-
}
|
|
68
|
-
async generateScenarioAndFeatureNames(scenarioAsText) {
|
|
69
|
-
if (!this.projectDir) {
|
|
70
|
-
throw new Error("Project directory not found");
|
|
71
|
-
}
|
|
72
|
-
// read all files with .feature extension into an object {files:[{name: "", content: ""}]}
|
|
73
|
-
const featureFiles = readdirSync(path.join(this.projectDir, "features"));
|
|
74
|
-
const featureFilesContent = featureFiles
|
|
75
|
-
.filter((file) => file.endsWith(".feature"))
|
|
76
|
-
.map((file) => {
|
|
77
|
-
return {
|
|
78
|
-
name: file,
|
|
79
|
-
content: readFileSync(path.join(this.projectDir, "features", file), "utf8"),
|
|
85
|
+
const genObject = {
|
|
86
|
+
files: featureFilesContent,
|
|
87
|
+
scenario: scenarioAsText,
|
|
80
88
|
};
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
89
|
+
// get screenshot for the last command
|
|
90
|
+
const url = `${getRunsServiceBaseURL()}/generate-step-information/generate_scenario_feature`;
|
|
91
|
+
const TIMEOUT = 120; // 2 minutes
|
|
92
|
+
const { data } = await axiosClient({
|
|
93
|
+
url,
|
|
94
|
+
method: "POST",
|
|
95
|
+
data: {
|
|
96
|
+
...genObject,
|
|
97
|
+
},
|
|
98
|
+
headers: {
|
|
99
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
100
|
+
"X-Source": "recorder",
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
const actionUrl = `${getRunsServiceBaseURL()}/action/get-action?id=${data.id}`;
|
|
104
|
+
let result = { status: 500 };
|
|
105
|
+
for (let i = 0; i < TIMEOUT; i++) {
|
|
106
|
+
try {
|
|
107
|
+
const action = await axiosClient({
|
|
108
|
+
url: actionUrl,
|
|
109
|
+
method: "GET",
|
|
110
|
+
headers: {
|
|
111
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
112
|
+
"X-Source": "recorder",
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
if (action.data.status) {
|
|
116
|
+
result = action;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
if (i === TIMEOUT - 1) {
|
|
122
|
+
console.error("Timeout while generating step details: ", error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
126
|
+
}
|
|
127
|
+
if (result.status !== 200) {
|
|
128
|
+
return { success: false, message: "Error while generating step details" };
|
|
129
|
+
}
|
|
130
|
+
return result.data;
|
|
131
|
+
}
|
|
132
|
+
//@ts-expect-error
|
|
133
|
+
async generateCommandName({ command }) {
|
|
134
|
+
const url = `${getRunsServiceBaseURL()}/generate-step-information/generate-element-name`;
|
|
135
|
+
const screenshot = this.screenshotMap.get(command.inputID);
|
|
136
|
+
const result = await axiosClient({
|
|
137
|
+
url,
|
|
138
|
+
method: "POST",
|
|
139
|
+
data: { ...command, screenshot },
|
|
140
|
+
headers: {
|
|
141
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
142
|
+
"X-Source": "recorder",
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
if (result.status !== 200) {
|
|
146
|
+
return { success: false, message: "Error while generating command details" };
|
|
147
|
+
}
|
|
148
|
+
return result.data;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
export class PublishService {
|
|
152
|
+
TOKEN;
|
|
153
|
+
constructor(TOKEN) {
|
|
154
|
+
this.TOKEN = TOKEN;
|
|
155
|
+
}
|
|
156
|
+
async saveScenario({ scenario, featureName, override, branch, isEditing, projectId, env }) {
|
|
157
|
+
const runsURL = getRunsServiceBaseURL();
|
|
158
|
+
const workspaceURL = runsURL.replace("/runs", "/workspace");
|
|
159
|
+
const url = `${workspaceURL}/publish-recording`;
|
|
160
|
+
const result = await axiosClient({
|
|
161
|
+
url,
|
|
162
|
+
method: "POST",
|
|
163
|
+
data: {
|
|
164
|
+
scenario,
|
|
165
|
+
featureName,
|
|
166
|
+
override,
|
|
167
|
+
env,
|
|
168
|
+
},
|
|
169
|
+
params: {
|
|
170
|
+
branch,
|
|
171
|
+
},
|
|
172
|
+
headers: {
|
|
173
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
174
|
+
"X-Source": "recorder",
|
|
175
|
+
},
|
|
112
176
|
});
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
177
|
+
if (result.status !== 200) {
|
|
178
|
+
return { success: false, message: "Error while saving scenario" };
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
this.updateProjectMetadata({
|
|
182
|
+
featureName,
|
|
183
|
+
scenarioName: scenario.name,
|
|
184
|
+
projectId,
|
|
185
|
+
branch,
|
|
186
|
+
isEditing: override,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
catch (error) { }
|
|
190
|
+
return { success: true, data: result.data };
|
|
191
|
+
}
|
|
192
|
+
async updateProjectMetadata({ featureName, scenarioName, projectId, branch, isEditing }) {
|
|
193
|
+
try {
|
|
194
|
+
await axiosClient({
|
|
195
|
+
method: "POST",
|
|
196
|
+
url: `${getRunsServiceBaseURL()}/project/updateProjectMetadata`,
|
|
197
|
+
data: {
|
|
198
|
+
featureName,
|
|
199
|
+
scenarioName,
|
|
200
|
+
eventType: isEditing ? "editScenario" : "createScenario",
|
|
201
|
+
projectId,
|
|
202
|
+
branch: branch,
|
|
203
|
+
},
|
|
204
|
+
headers: {
|
|
205
|
+
Authorization: `Bearer ${this.TOKEN}`,
|
|
206
|
+
"X-Source": "recorder",
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
// logger.error("Failed to update project metadata: " + error.message);
|
|
212
|
+
// @ts-ignore
|
|
213
|
+
console.error("run_recorder", `Failed to update project metadata: ${error.message ?? error}`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
export class RemoteBrowserService extends EventEmitter {
|
|
218
|
+
CDP_CONNECT_URL;
|
|
219
|
+
context;
|
|
220
|
+
pages = new Map();
|
|
221
|
+
_selectedPageId = null;
|
|
222
|
+
wsUrlBase; // Store the base URL
|
|
223
|
+
hasClipboardData(payload) {
|
|
224
|
+
return Boolean(payload && (payload.text || payload.html || (Array.isArray(payload.files) && payload.files.length > 0)));
|
|
225
|
+
}
|
|
226
|
+
getSelectedPageInfo() {
|
|
227
|
+
if (!this._selectedPageId) {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
return this.pages.get(this._selectedPageId) ?? null;
|
|
231
|
+
}
|
|
232
|
+
constructor({ CDP_CONNECT_URL, context }) {
|
|
233
|
+
super();
|
|
234
|
+
this.CDP_CONNECT_URL = CDP_CONNECT_URL;
|
|
235
|
+
this.context = context;
|
|
236
|
+
this.wsUrlBase = this.CDP_CONNECT_URL.replace(/^http/, "ws") + "/devtools/page/";
|
|
237
|
+
this.log("đ RemoteBrowserService initialized", { CDP_CONNECT_URL });
|
|
238
|
+
this.context.grantPermissions(["clipboard-read", "clipboard-write"]).catch((error) => {
|
|
239
|
+
this.log("â ī¸ Failed to pre-grant clipboard permissions", { error });
|
|
240
|
+
});
|
|
241
|
+
this.initializeListeners();
|
|
242
|
+
}
|
|
243
|
+
log(message, data) {
|
|
244
|
+
const timestamp = new Date().toISOString();
|
|
245
|
+
console.log(`[${timestamp}] [RemoteBrowserService] ${message}`, data ? JSON.stringify(data, null, 2) : "");
|
|
246
|
+
}
|
|
247
|
+
isTransientPageDataError(error) {
|
|
248
|
+
const message = error instanceof Error ? error.message : typeof error === "string" ? error : undefined;
|
|
249
|
+
if (!message) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
const transientIndicators = [
|
|
253
|
+
"Execution context was destroyed",
|
|
254
|
+
"Execution context is not available",
|
|
255
|
+
"Cannot find context with specified id",
|
|
256
|
+
"Target closed",
|
|
257
|
+
"Navigation interrupted",
|
|
258
|
+
];
|
|
259
|
+
return transientIndicators.some((indicator) => message.includes(indicator));
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Gets the CDP Target ID for a page *directly* from the page.
|
|
263
|
+
* This is the only reliable method during restarts.
|
|
264
|
+
*/
|
|
265
|
+
async getCdpTargetId(page) {
|
|
266
|
+
try {
|
|
267
|
+
if (page.isClosed()) {
|
|
268
|
+
this.log("â ī¸ Attempted to get CDP ID from a closed page");
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
const cdpSession = await page.context().newCDPSession(page);
|
|
272
|
+
const { targetInfo } = await cdpSession.send("Target.getTargetInfo");
|
|
273
|
+
this.log("đ Retrieved TargetInfo via CDP session", targetInfo);
|
|
274
|
+
await cdpSession.detach();
|
|
275
|
+
if (targetInfo && targetInfo.targetId) {
|
|
276
|
+
this.log("â
Found CDP ID by session", { id: targetInfo.targetId, url: page.url() });
|
|
277
|
+
return targetInfo.targetId;
|
|
278
|
+
}
|
|
279
|
+
throw new Error("Target.getTargetInfo did not return a targetId");
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
this.log("â Error getting CDP ID by session", { url: page.url(), error });
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
async initializeListeners() {
|
|
287
|
+
this.log("đĄ Initializing listeners");
|
|
288
|
+
// Initialize with existing pages
|
|
289
|
+
const existingPages = this.context.pages();
|
|
290
|
+
this.log("đ Found existing pages", { count: existingPages.length });
|
|
291
|
+
for (const page of existingPages) {
|
|
292
|
+
const stableTabId = uuidv4();
|
|
293
|
+
const cdpTargetId = await this.getCdpTargetId(page);
|
|
294
|
+
this.pages.set(stableTabId, {
|
|
295
|
+
page,
|
|
296
|
+
cdpTargetId,
|
|
297
|
+
lastKnownTitle: undefined,
|
|
298
|
+
lastKnownUrl: page.url(),
|
|
299
|
+
lastKnownWsDebuggerUrl: cdpTargetId ? `${this.wsUrlBase}${cdpTargetId}` : undefined,
|
|
300
|
+
});
|
|
301
|
+
page.on("framenavigated", async () => {
|
|
302
|
+
try {
|
|
303
|
+
await this.ensureClipboardScriptLoaded(page);
|
|
304
|
+
}
|
|
305
|
+
catch (err) {
|
|
306
|
+
this.log("â Error injecting clipboard script on navigation", { stableTabId, error: err });
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
// this.log("â
Existing page added to map", { stableTabId, cdpTargetId, url: page.url() });
|
|
310
|
+
// this.attachPageLifecycleListeners(page, stableTabId);
|
|
311
|
+
// this.log("đ Attached lifecycle listeners to existing page", { stableTabId });
|
|
312
|
+
}
|
|
313
|
+
// if (this.pages.size > 0 && !this._selectedPageId) {
|
|
314
|
+
// this._selectedPageId = Array.from(this.pages.keys())[0];
|
|
315
|
+
// }
|
|
316
|
+
// await this.syncState();
|
|
317
|
+
this.context.on("page", async (page) => {
|
|
318
|
+
// const stableTabId = uuidv4();
|
|
319
|
+
// this.log("đ New page event triggered", { stableTabId, url: page.url() });
|
|
320
|
+
// // We get the ID immediately, but it might be null if the page is too new
|
|
321
|
+
// const cdpTargetId = await this.getCdpTargetId(page);
|
|
322
|
+
// this.pages.set(stableTabId, {
|
|
323
|
+
// page,
|
|
324
|
+
// cdpTargetId,
|
|
325
|
+
// lastKnownTitle: undefined,
|
|
326
|
+
// lastKnownUrl: page.url(),
|
|
327
|
+
// lastKnownWsDebuggerUrl: cdpTargetId ? `${this.wsUrlBase}${cdpTargetId}` : undefined,
|
|
328
|
+
// });
|
|
329
|
+
// if (cdpTargetId) {
|
|
330
|
+
// this.log("â
Page mapped to CDP ID", { stableTabId, cdpTargetId });
|
|
331
|
+
// } else {
|
|
332
|
+
// this.log("â ī¸ Could not find CDP ID for new page yet", { stableTabId });
|
|
333
|
+
// }
|
|
334
|
+
// if (!this._selectedPageId) {
|
|
335
|
+
// // Select the first page that opens
|
|
336
|
+
// this._selectedPageId = stableTabId;
|
|
337
|
+
// this.log("đ¯ Initial selected page set", { selectedPageId: this._selectedPageId });
|
|
338
|
+
// }
|
|
339
|
+
// this.attachPageLifecycleListeners(page, stableTabId);
|
|
340
|
+
// await this.syncState();
|
|
341
|
+
await this.ensureClipboardScriptLoaded(page);
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
// private attachPageLifecycleListeners(page: Page, stableTabId: string) {
|
|
345
|
+
// page.on("load", () => this.syncState());
|
|
346
|
+
// page.on("framenavigated", () => this.syncState());
|
|
347
|
+
// page.on("close", async () => {
|
|
348
|
+
// this.log("đī¸ Page close event", { stableTabId });
|
|
349
|
+
// this.pages.delete(stableTabId);
|
|
350
|
+
// if (this._selectedPageId === stableTabId) {
|
|
351
|
+
// this._selectedPageId = this.pages.size > 0 ? Array.from(this.pages.keys())[0] : null;
|
|
352
|
+
// this.log("đ Selected page changed after close", { newSelectedId: this._selectedPageId });
|
|
353
|
+
// }
|
|
354
|
+
// await this.syncState();
|
|
355
|
+
// });
|
|
356
|
+
// }
|
|
357
|
+
// private async syncState() {
|
|
358
|
+
// try {
|
|
359
|
+
// this.log("đ Starting state sync");
|
|
360
|
+
// const state = await this.getState();
|
|
361
|
+
// this.log("â
State sync complete", { pagesCount: state.pages.length, selectedPageId: state.selectedPageId });
|
|
362
|
+
// this.emit("BrowserService.stateSync", state);
|
|
363
|
+
// } catch (error) {
|
|
364
|
+
// this.log("â Error syncing state", { error });
|
|
365
|
+
// }
|
|
366
|
+
// }
|
|
367
|
+
// async getState(): Promise<BrowserState> {
|
|
368
|
+
// this.log("đ Getting current state");
|
|
369
|
+
// const pagesData: PageData[] = [];
|
|
370
|
+
// const pagesToDelete: string[] = []; // To clean up closed pages
|
|
371
|
+
// for (const [stableTabId, pageInfo] of this.pages.entries()) {
|
|
372
|
+
// const pageData: PageData = {
|
|
373
|
+
// id: stableTabId,
|
|
374
|
+
// title: pageInfo.lastKnownTitle ?? "",
|
|
375
|
+
// url: pageInfo.lastKnownUrl ?? pageInfo.page.url(),
|
|
376
|
+
// wsDebuggerUrl: pageInfo.lastKnownWsDebuggerUrl ?? "",
|
|
377
|
+
// };
|
|
378
|
+
// try {
|
|
379
|
+
// if (pageInfo.page.isClosed()) {
|
|
380
|
+
// this.log("đ§š Found closed page during getState, marking for deletion", { stableTabId });
|
|
381
|
+
// pagesToDelete.push(stableTabId);
|
|
382
|
+
// continue;
|
|
383
|
+
// }
|
|
384
|
+
// // Get the one, true, live CDP ID
|
|
385
|
+
// const currentCdpId = await this.getCdpTargetId(pageInfo.page);
|
|
386
|
+
// if (currentCdpId && pageInfo.cdpTargetId !== currentCdpId) {
|
|
387
|
+
// this.log("đ CDP ID changed", { stableTabId, old: pageInfo.cdpTargetId, new: currentCdpId });
|
|
388
|
+
// pageInfo.cdpTargetId = currentCdpId; // Update our internal reference
|
|
389
|
+
// }
|
|
390
|
+
// // Manually construct the WebSocket URL
|
|
391
|
+
// const wsDebuggerUrl = currentCdpId
|
|
392
|
+
// ? `${this.wsUrlBase}${currentCdpId}`
|
|
393
|
+
// : (pageInfo.lastKnownWsDebuggerUrl ?? "");
|
|
394
|
+
// if (!wsDebuggerUrl) {
|
|
395
|
+
// this.log("â ī¸ Could not get CDP ID, wsDebuggerUrl will be empty", { stableTabId });
|
|
396
|
+
// }
|
|
397
|
+
// const title = await pageInfo.page.title();
|
|
398
|
+
// const currentUrl = pageInfo.page.url();
|
|
399
|
+
// pageData.title = title;
|
|
400
|
+
// pageData.url = currentUrl;
|
|
401
|
+
// pageData.wsDebuggerUrl = wsDebuggerUrl; // Use the constructed URL
|
|
402
|
+
// pageInfo.lastKnownTitle = title;
|
|
403
|
+
// pageInfo.lastKnownUrl = currentUrl;
|
|
404
|
+
// pageInfo.lastKnownWsDebuggerUrl = wsDebuggerUrl;
|
|
405
|
+
// this.log("đĨPage data", pageData);
|
|
406
|
+
// } catch (error) {
|
|
407
|
+
// const message = error instanceof Error ? error.message : JSON.stringify(error);
|
|
408
|
+
// if (this.isTransientPageDataError(error)) {
|
|
409
|
+
// this.log("âŗ Transient error getting page data, will retry", { stableTabId, message });
|
|
410
|
+
// } else {
|
|
411
|
+
// this.log("â Error getting page data", { stableTabId, message });
|
|
412
|
+
// pagesToDelete.push(stableTabId); // Mark for deletion
|
|
413
|
+
// continue;
|
|
414
|
+
// }
|
|
415
|
+
// }
|
|
416
|
+
// if (!pageData.title) {
|
|
417
|
+
// pageData.title = pageInfo.page.url() === "about:blank" ? "" : "Loading...";
|
|
418
|
+
// }
|
|
419
|
+
// pagesData.push(pageData);
|
|
420
|
+
// }
|
|
421
|
+
// pagesToDelete.forEach((id) => this.pages.delete(id));
|
|
422
|
+
// if (this._selectedPageId && !this.pages.has(this._selectedPageId)) {
|
|
423
|
+
// this._selectedPageId = pagesData.length > 0 ? pagesData[0].id : null;
|
|
424
|
+
// this.log("đ Corrected selectedPageId", { new: this._selectedPageId });
|
|
425
|
+
// }
|
|
426
|
+
// if (!this._selectedPageId && pagesData.length > 0) {
|
|
427
|
+
// this._selectedPageId = pagesData[0].id;
|
|
428
|
+
// this.log("đ¯ Set default selectedPageId", { new: this._selectedPageId });
|
|
429
|
+
// }
|
|
430
|
+
// const state = {
|
|
431
|
+
// pages: pagesData,
|
|
432
|
+
// selectedPageId: this._selectedPageId,
|
|
433
|
+
// };
|
|
434
|
+
// this.log("đĻ Final state", state);
|
|
435
|
+
// return state;
|
|
436
|
+
// }
|
|
437
|
+
// async createTab(url: string = "about:blank"): Promise<void> {
|
|
438
|
+
// try {
|
|
439
|
+
// this.log("đ Creating new tab", { url });
|
|
440
|
+
// const page = await this.context.newPage(); // This will trigger the 'page' event
|
|
441
|
+
// if (url !== "about:blank") {
|
|
442
|
+
// await page.goto(url, { waitUntil: "domcontentloaded" });
|
|
443
|
+
// }
|
|
444
|
+
// for (const [stableTabId, pageInfo] of this.pages.entries()) {
|
|
445
|
+
// if (pageInfo.page === page) {
|
|
446
|
+
// this._selectedPageId = stableTabId;
|
|
447
|
+
// this.log("â
New tab created and selected", { stableTabId, url });
|
|
448
|
+
// break;
|
|
449
|
+
// }
|
|
450
|
+
// }
|
|
451
|
+
// await this.syncState();
|
|
452
|
+
// } catch (error) {
|
|
453
|
+
// this.log("â Error creating tab", { error });
|
|
454
|
+
// }
|
|
455
|
+
// }
|
|
456
|
+
// async closeTab(stableTabId: string): Promise<void> {
|
|
457
|
+
// try {
|
|
458
|
+
// this.log("đī¸ Closing tab", { stableTabId });
|
|
459
|
+
// const pageInfo = this.pages.get(stableTabId);
|
|
460
|
+
// if (pageInfo) {
|
|
461
|
+
// await pageInfo.page.close(); // This will trigger the 'close' event
|
|
462
|
+
// } else {
|
|
463
|
+
// this.log("â ī¸ Page not found for closing", { stableTabId });
|
|
464
|
+
// }
|
|
465
|
+
// } catch (error) {
|
|
466
|
+
// this.log("â Error closing tab", { error });
|
|
467
|
+
// }
|
|
468
|
+
// }
|
|
469
|
+
// async selectTab(stableTabId: string): Promise<void> {
|
|
470
|
+
// try {
|
|
471
|
+
// this.log("đ¯ Selecting tab", { stableTabId });
|
|
472
|
+
// const pageInfo = this.pages.get(stableTabId);
|
|
473
|
+
// if (pageInfo) {
|
|
474
|
+
// this._selectedPageId = stableTabId;
|
|
475
|
+
// await pageInfo.page.bringToFront();
|
|
476
|
+
// this.log("â
Tab selected successfully", { stableTabId });
|
|
477
|
+
// await this.syncState();
|
|
478
|
+
// } else {
|
|
479
|
+
// this.log("â ī¸ Page not found for selection", { stableTabId });
|
|
480
|
+
// }
|
|
481
|
+
// } catch (error) {
|
|
482
|
+
// this.log("â Error selecting tab", { error });
|
|
483
|
+
// }
|
|
484
|
+
// }
|
|
485
|
+
getPageInfo(stableTabId) {
|
|
486
|
+
if (!stableTabId) {
|
|
487
|
+
this.log("â ī¸ Operation requested without a selected tab");
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
const pageInfo = this.pages.get(stableTabId);
|
|
491
|
+
if (!pageInfo) {
|
|
492
|
+
this.log("â ī¸ Page not found for operation", { stableTabId });
|
|
493
|
+
return null;
|
|
494
|
+
}
|
|
495
|
+
return pageInfo;
|
|
496
|
+
}
|
|
497
|
+
// async navigateTab(stableTabId: string, url: string): Promise<void> {
|
|
498
|
+
// const pageInfo = this.getPageInfo(stableTabId);
|
|
499
|
+
// if (!pageInfo) {
|
|
500
|
+
// return;
|
|
501
|
+
// }
|
|
502
|
+
// try {
|
|
503
|
+
// this.log("đ Navigating tab", { stableTabId, url });
|
|
504
|
+
// await pageInfo.page.goto(url, { waitUntil: "domcontentloaded" });
|
|
505
|
+
// this._selectedPageId = stableTabId;
|
|
506
|
+
// await this.syncState();
|
|
507
|
+
// } catch (error) {
|
|
508
|
+
// this.log("â Error navigating tab", { stableTabId, url, error });
|
|
509
|
+
// throw error;
|
|
510
|
+
// }
|
|
511
|
+
// }
|
|
512
|
+
// async reloadTab(stableTabId: string): Promise<void> {
|
|
513
|
+
// const pageInfo = this.getPageInfo(stableTabId);
|
|
514
|
+
// if (!pageInfo) {
|
|
515
|
+
// return;
|
|
516
|
+
// }
|
|
517
|
+
// try {
|
|
518
|
+
// this.log("đ Reloading tab", { stableTabId, url: pageInfo.page.url() });
|
|
519
|
+
// await pageInfo.page.reload({ waitUntil: "domcontentloaded" });
|
|
520
|
+
// await this.syncState();
|
|
521
|
+
// } catch (error) {
|
|
522
|
+
// this.log("â Error reloading tab", { stableTabId, error });
|
|
523
|
+
// throw error;
|
|
524
|
+
// }
|
|
525
|
+
// }
|
|
526
|
+
// async goBack(stableTabId: string): Promise<void> {
|
|
527
|
+
// const pageInfo = this.getPageInfo(stableTabId);
|
|
528
|
+
// if (!pageInfo) {
|
|
529
|
+
// return;
|
|
530
|
+
// }
|
|
531
|
+
// try {
|
|
532
|
+
// this.log("âŦ
ī¸ Navigating back", { stableTabId });
|
|
533
|
+
// const response = await pageInfo.page.goBack({ waitUntil: "domcontentloaded" });
|
|
534
|
+
// if (!response) {
|
|
535
|
+
// this.log("âšī¸ No history entry to go back to", { stableTabId });
|
|
536
|
+
// }
|
|
537
|
+
// await this.syncState();
|
|
538
|
+
// } catch (error) {
|
|
539
|
+
// this.log("â Error navigating back", { stableTabId, error });
|
|
540
|
+
// throw error;
|
|
541
|
+
// }
|
|
542
|
+
// }
|
|
543
|
+
// async goForward(stableTabId: string): Promise<void> {
|
|
544
|
+
// const pageInfo = this.getPageInfo(stableTabId);
|
|
545
|
+
// if (!pageInfo) {
|
|
546
|
+
// return;
|
|
547
|
+
// }
|
|
548
|
+
// try {
|
|
549
|
+
// this.log("âĄī¸ Navigating forward", { stableTabId });
|
|
550
|
+
// const response = await pageInfo.page.goForward({ waitUntil: "domcontentloaded" });
|
|
551
|
+
// if (!response) {
|
|
552
|
+
// this.log("âšī¸ No history entry to go forward to", { stableTabId });
|
|
553
|
+
// }
|
|
554
|
+
// await this.syncState();
|
|
555
|
+
// } catch (error) {
|
|
556
|
+
// this.log("â Error navigating forward", { stableTabId, error });
|
|
557
|
+
// throw error;
|
|
558
|
+
// }
|
|
559
|
+
// }
|
|
560
|
+
async applyClipboardPayload(payload) {
|
|
561
|
+
this.log("đ applyClipboardPayload called", {
|
|
562
|
+
hasText: !!payload?.text,
|
|
563
|
+
hasHtml: !!payload?.html,
|
|
564
|
+
hasFiles: !!payload?.files,
|
|
565
|
+
textLength: payload?.text?.length,
|
|
566
|
+
htmlLength: payload?.html?.length,
|
|
567
|
+
filesCount: payload?.files?.length,
|
|
568
|
+
});
|
|
569
|
+
const pageInfo = this.getPageInfo(this._selectedPageId);
|
|
570
|
+
if (!pageInfo) {
|
|
571
|
+
this.log("â ī¸ No active page available for clipboard payload", {
|
|
572
|
+
selectedPageId: this._selectedPageId,
|
|
573
|
+
totalPages: this.pages.size,
|
|
574
|
+
});
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
this.log("đ Target page info", {
|
|
578
|
+
stableTabId: this._selectedPageId,
|
|
579
|
+
url: pageInfo.page.url(),
|
|
580
|
+
isClosed: pageInfo.page.isClosed(),
|
|
581
|
+
title: await pageInfo.page.title().catch(() => "unknown"),
|
|
582
|
+
});
|
|
583
|
+
try {
|
|
584
|
+
// Grant permissions first
|
|
585
|
+
this.log("đ Attempting to grant clipboard permissions");
|
|
586
|
+
await pageInfo.page
|
|
587
|
+
.context()
|
|
588
|
+
.grantPermissions(["clipboard-read", "clipboard-write"])
|
|
589
|
+
.then(() => {
|
|
590
|
+
this.log("â
Clipboard permissions granted");
|
|
591
|
+
})
|
|
592
|
+
.catch((error) => {
|
|
593
|
+
this.log("â ī¸ Failed to grant clipboard permissions", { error: error.message });
|
|
594
|
+
});
|
|
595
|
+
// Apply the clipboard data using the injected function
|
|
596
|
+
this.log("đ Executing clipboard application script", {
|
|
597
|
+
payloadPreview: {
|
|
598
|
+
text: payload?.text?.substring(0, 100),
|
|
599
|
+
html: payload?.html?.substring(0, 100),
|
|
600
|
+
},
|
|
601
|
+
});
|
|
602
|
+
const result = await pageInfo.page.evaluate((clipboardData) => {
|
|
603
|
+
console.log("[RemoteBrowser->Page] Starting clipboard application");
|
|
604
|
+
console.log("[RemoteBrowser->Page] Payload:", {
|
|
605
|
+
hasText: !!clipboardData?.text,
|
|
606
|
+
hasHtml: !!clipboardData?.html,
|
|
607
|
+
text: clipboardData?.text?.substring(0, 50),
|
|
608
|
+
html: clipboardData?.html?.substring(0, 50),
|
|
609
|
+
});
|
|
610
|
+
// Check if the function exists
|
|
611
|
+
if (typeof window.__bvt_applyClipboardData !== "function") {
|
|
612
|
+
console.error("[RemoteBrowser->Page] â __bvt_applyClipboardData function not found!");
|
|
613
|
+
console.log("[RemoteBrowser->Page] Available window properties:", Object.keys(window).filter((k) => k.includes("bvt")));
|
|
614
|
+
return false;
|
|
615
|
+
}
|
|
616
|
+
console.log("[RemoteBrowser->Page] â
__bvt_applyClipboardData function found, calling it");
|
|
617
|
+
try {
|
|
618
|
+
const result = window.__bvt_applyClipboardData(clipboardData);
|
|
619
|
+
console.log("[RemoteBrowser->Page] Function returned:", result);
|
|
620
|
+
return result;
|
|
621
|
+
}
|
|
622
|
+
catch (error) {
|
|
623
|
+
console.error("[RemoteBrowser->Page] â Error calling __bvt_applyClipboardData:", error);
|
|
624
|
+
return false;
|
|
625
|
+
}
|
|
626
|
+
}, payload);
|
|
627
|
+
this.log("đ Clipboard application result", {
|
|
628
|
+
success: result,
|
|
629
|
+
selectedPageId: this._selectedPageId,
|
|
630
|
+
});
|
|
631
|
+
if (!result) {
|
|
632
|
+
this.log("â ī¸ Clipboard script returned false - data may not have been applied");
|
|
633
|
+
}
|
|
634
|
+
else {
|
|
635
|
+
this.log("â
Clipboard payload applied successfully");
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
catch (error) {
|
|
639
|
+
this.log("â Error applying clipboard payload", {
|
|
640
|
+
error: error instanceof Error ? error.message : String(error),
|
|
641
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
642
|
+
selectedPageId: this._selectedPageId,
|
|
643
|
+
pageUrl: pageInfo.page.url(),
|
|
644
|
+
});
|
|
645
|
+
throw error;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
async readClipboardPayload() {
|
|
649
|
+
const pageInfo = this.getSelectedPageInfo();
|
|
650
|
+
if (!pageInfo) {
|
|
651
|
+
this.log("â ī¸ Cannot read clipboard - no active page", { selectedPageId: this._selectedPageId });
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
try {
|
|
655
|
+
await pageInfo.page
|
|
656
|
+
.context()
|
|
657
|
+
.grantPermissions(["clipboard-read", "clipboard-write"])
|
|
658
|
+
.catch((error) => {
|
|
659
|
+
this.log("â ī¸ Failed to ensure clipboard permissions before read", { error });
|
|
660
|
+
});
|
|
661
|
+
const payload = await pageInfo.page.evaluate(async () => {
|
|
662
|
+
const result = {};
|
|
663
|
+
if (typeof navigator === "undefined" || !navigator.clipboard) {
|
|
664
|
+
return result;
|
|
665
|
+
}
|
|
666
|
+
const arrayBufferToBase64 = (buffer) => {
|
|
667
|
+
let binary = "";
|
|
668
|
+
const bytes = new Uint8Array(buffer);
|
|
669
|
+
const chunkSize = 0x8000;
|
|
670
|
+
for (let index = 0; index < bytes.length; index += chunkSize) {
|
|
671
|
+
const chunk = bytes.subarray(index, index + chunkSize);
|
|
672
|
+
binary += String.fromCharCode(...chunk);
|
|
673
|
+
}
|
|
674
|
+
return btoa(binary);
|
|
675
|
+
};
|
|
676
|
+
const files = [];
|
|
677
|
+
if (typeof navigator.clipboard.read === "function") {
|
|
678
|
+
try {
|
|
679
|
+
const items = await navigator.clipboard.read();
|
|
680
|
+
for (const item of items) {
|
|
681
|
+
if (item.types.includes("text/html") && !result.html) {
|
|
682
|
+
const blob = await item.getType("text/html");
|
|
683
|
+
result.html = await blob.text();
|
|
684
|
+
}
|
|
685
|
+
if (item.types.includes("text/plain") && !result.text) {
|
|
686
|
+
const blob = await item.getType("text/plain");
|
|
687
|
+
result.text = await blob.text();
|
|
688
|
+
}
|
|
689
|
+
for (const type of item.types) {
|
|
690
|
+
if (type.startsWith("text/")) {
|
|
691
|
+
continue;
|
|
692
|
+
}
|
|
693
|
+
try {
|
|
694
|
+
const blob = await item.getType(type);
|
|
695
|
+
const buffer = await blob.arrayBuffer();
|
|
696
|
+
files.push({
|
|
697
|
+
name: `clipboard-file-${files.length + 1}`,
|
|
698
|
+
type,
|
|
699
|
+
lastModified: Date.now(),
|
|
700
|
+
data: arrayBufferToBase64(buffer),
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
catch (error) {
|
|
704
|
+
console.warn("[RemoteClipboard] Failed to serialize clipboard blob", { type, error });
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
catch (error) {
|
|
710
|
+
console.warn("[RemoteClipboard] navigator.clipboard.read failed", error);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
if (!result.text && typeof navigator.clipboard.readText === "function") {
|
|
714
|
+
try {
|
|
715
|
+
const text = await navigator.clipboard.readText();
|
|
716
|
+
if (text) {
|
|
717
|
+
result.text = text;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
catch (error) {
|
|
721
|
+
console.warn("[RemoteClipboard] navigator.clipboard.readText failed", error);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
if (!result.text) {
|
|
725
|
+
const selection = window.getSelection?.()?.toString?.();
|
|
726
|
+
if (selection) {
|
|
727
|
+
result.text = selection;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
if (files.length > 0) {
|
|
731
|
+
result.files = files;
|
|
732
|
+
}
|
|
733
|
+
return result;
|
|
734
|
+
});
|
|
735
|
+
if (this.hasClipboardData(payload)) {
|
|
736
|
+
this.log("đ Remote clipboard payload captured", {
|
|
737
|
+
hasText: !!payload.text,
|
|
738
|
+
hasHtml: !!payload.html,
|
|
739
|
+
fileCount: payload.files?.length ?? 0,
|
|
740
|
+
});
|
|
741
|
+
return payload;
|
|
742
|
+
}
|
|
743
|
+
this.log("âšī¸ Remote clipboard read returned empty payload", {
|
|
744
|
+
selectedPageId: this._selectedPageId,
|
|
745
|
+
});
|
|
746
|
+
return null;
|
|
747
|
+
}
|
|
748
|
+
catch (error) {
|
|
749
|
+
this.log("â Error reading remote clipboard", {
|
|
750
|
+
error: error instanceof Error ? error.message : String(error),
|
|
751
|
+
});
|
|
752
|
+
throw error;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
// Add a helper method to verify clipboard script is loaded
|
|
756
|
+
async verifyClipboardScript(stableTabId) {
|
|
757
|
+
const pageInfo = this.pages.get(stableTabId);
|
|
758
|
+
if (!pageInfo) {
|
|
759
|
+
this.log("â ī¸ Cannot verify clipboard script - page not found", { stableTabId });
|
|
760
|
+
return false;
|
|
761
|
+
}
|
|
762
|
+
try {
|
|
763
|
+
const isLoaded = await pageInfo.page.evaluate(() => {
|
|
764
|
+
return (typeof window.__bvt_applyClipboardData === "function" && typeof window.__bvt_reportClipboard === "function");
|
|
765
|
+
});
|
|
766
|
+
this.log("đ Clipboard script verification", {
|
|
767
|
+
stableTabId,
|
|
768
|
+
isLoaded,
|
|
769
|
+
url: pageInfo.page.url(),
|
|
770
|
+
});
|
|
771
|
+
return isLoaded;
|
|
772
|
+
}
|
|
773
|
+
catch (error) {
|
|
774
|
+
this.log("â Error verifying clipboard script", {
|
|
775
|
+
stableTabId,
|
|
776
|
+
error: error instanceof Error ? error.message : String(error),
|
|
777
|
+
});
|
|
778
|
+
return false;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
// Call this after navigation to ensure script is loaded
|
|
782
|
+
async ensureClipboardScriptLoaded(page) {
|
|
783
|
+
try {
|
|
784
|
+
const isLoaded = await page.evaluate(() => {
|
|
785
|
+
return typeof window.__bvt_applyClipboardData === "function";
|
|
786
|
+
});
|
|
787
|
+
if (!isLoaded) {
|
|
788
|
+
this.log("â ī¸ Clipboard script not loaded, attempting to reload");
|
|
789
|
+
// The script should be in initScripts, so it will load on next navigation
|
|
790
|
+
// or you could manually inject it here
|
|
791
|
+
}
|
|
792
|
+
else {
|
|
793
|
+
this.log("â
Clipboard script confirmed loaded");
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
catch (error) {
|
|
797
|
+
this.log("â Error checking clipboard script", { error });
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
getSelectedPage() {
|
|
801
|
+
const pageInfo = this.getSelectedPageInfo();
|
|
802
|
+
this.log("đ Getting selected page", {
|
|
803
|
+
selectedPageId: this._selectedPageId,
|
|
804
|
+
found: !!pageInfo,
|
|
805
|
+
url: pageInfo?.page.url(),
|
|
806
|
+
});
|
|
807
|
+
return pageInfo?.page || null;
|
|
808
|
+
}
|
|
809
|
+
destroy() {
|
|
810
|
+
this.log("đĨ Destroying RemoteBrowserService");
|
|
811
|
+
this.context.removeAllListeners("page");
|
|
812
|
+
for (const [, pageInfo] of this.pages.entries()) {
|
|
813
|
+
pageInfo.page.removeAllListeners(); // Remove all listeners from each page
|
|
814
|
+
}
|
|
815
|
+
this.pages.clear();
|
|
816
|
+
this._selectedPageId = null;
|
|
817
|
+
this.removeAllListeners(); // Remove listeners on the emitter itself
|
|
818
|
+
}
|
|
151
819
|
}
|