@reconcrap/boss-recommend-mcp 2.0.53 → 2.0.55

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.
@@ -1,1668 +1,1701 @@
1
- import {
2
- clearFocusedInput,
3
- clickNodeCenter,
4
- clickPoint,
5
- DETERMINISTIC_CLICK_OPTIONS,
6
- getFrameDocumentNodeId,
7
- getAttributesMap,
8
- getNodeBox,
9
- getOuterHTML,
10
- insertText,
11
- pressKey,
12
- querySelectorAll,
13
- sleep
14
- } from "../../core/browser/index.js";
1
+ import {
2
+ clearFocusedInput,
3
+ clickNodeCenter,
4
+ clickPoint,
5
+ DETERMINISTIC_CLICK_OPTIONS,
6
+ getFrameDocumentNodeId,
7
+ getAttributesMap,
8
+ getNodeBox,
9
+ getOuterHTML,
10
+ insertText,
11
+ pressKey,
12
+ querySelectorAll,
13
+ sleep
14
+ } from "../../core/browser/index.js";
15
15
  import {
16
16
  buildScreeningCandidateFromDetail,
17
17
  htmlToText
18
18
  } from "../../core/screening/index.js";
19
+ import {
20
+ closeBossAccountRightsBlockingPanel,
21
+ findBossAccountRightsBlockingPanel
22
+ } from "../common/account-rights-panel.js";
19
23
  import {
20
24
  CHAT_ACTIVE_CANDIDATE_SELECTORS,
21
25
  CHAT_ASK_RESUME_BUTTON_SELECTORS,
22
- CHAT_ATTACHMENT_RESUME_BUTTON_SELECTORS,
23
- CHAT_CONFIRM_REQUEST_RESUME_SELECTORS,
24
- CHAT_EDITOR_SELECTORS,
25
- CHAT_MESSAGE_FILTER_SELECTORS,
26
- CHAT_MESSAGE_LIST_SELECTORS,
27
- CHAT_ONLINE_RESUME_BUTTON_SELECTORS,
28
- CHAT_PRIMARY_LABEL_SELECTORS,
29
- CHAT_PROFILE_NETWORK_PATTERNS,
30
- CHAT_RESUME_CLOSE_SELECTORS,
31
- CHAT_RESUME_CONTENT_SELECTORS,
32
- CHAT_RESUME_FAST_MODAL_SELECTORS,
33
- CHAT_RESUME_IFRAME_SELECTORS,
34
- CHAT_RESUME_MODAL_SELECTORS,
35
- CHAT_SEND_BUTTON_SELECTORS
36
- } from "./constants.js";
37
- import {
38
- getChatRoots,
39
- queryFirstAcrossChatRoots
40
- } from "./roots.js";
41
- import {
42
- assertChatShellNotResumeTopLevel,
43
- getChatTopLevelState,
44
- isForbiddenChatResumeTopLevelUrl,
45
- makeForbiddenChatResumeNavigationError
46
- } from "./page-guard.js";
47
-
48
- export const CHAT_UNSAFE_ONLINE_RESUME_LINK_CODE = "CHAT_UNSAFE_ONLINE_RESUME_LINK";
49
-
50
- const CHAT_CONVERSATION_CONTROL_SCOPE_SELECTORS = Object.freeze([
51
- ".conversation-main",
52
- ".conversation-editor",
53
- ".chat-message-list",
54
- ".toolbar-box-right",
55
- ".operate-exchange-left",
56
- ".operate-icon-item",
57
- ".exchange-tooltip",
58
- ".boss-popup__wrapper",
59
- ".boss-dialog",
60
- ".dialog-wrap.active",
61
- ".geek-detail-modal"
62
- ]);
63
-
64
- const CHAT_REQUESTED_RESUME_SCOPE_SELECTORS = Object.freeze([
65
- ".chat-message-list",
66
- ".conversation-editor",
67
- ".conversation-main",
68
- ".toolbar-box-right",
69
- ".operate-exchange-left",
70
- ".operate-icon-item",
71
- ".exchange-tooltip",
72
- ".boss-popup__wrapper",
73
- ".boss-dialog",
74
- ".dialog-wrap.active"
75
- ]);
76
-
77
- export function matchesChatProfileNetwork(url) {
78
- return CHAT_PROFILE_NETWORK_PATTERNS.some((pattern) => pattern.test(String(url || "")));
79
- }
80
-
81
- function looksLikeForbiddenChatResumePath(value = "") {
82
- const normalized = String(value || "");
83
- return isForbiddenChatResumeTopLevelUrl(normalized)
84
- || /(?:^|["'\s=])(?:https?:\/\/[^"'\s>]*zhipin\.com)?\/web\/frame\/c-resume(?:[/?#"' >]|$)/i
85
- .test(normalized);
86
- }
87
-
88
- function extractFirstHtmlAttribute(html = "", names = []) {
89
- const source = String(html || "");
90
- for (const name of names) {
91
- const escaped = String(name).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
92
- const regex = new RegExp(`${escaped}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'>]+))`, "i");
93
- const match = source.match(regex);
94
- if (match) return match[1] ?? match[2] ?? match[3] ?? "";
95
- }
96
- return "";
97
- }
98
-
99
- export function isUnsafeChatOnlineResumeTarget(target = {}, buttonHTML = "") {
100
- const attributes = target?.attributes || {};
101
- const href = attributes.href
102
- || attributes["data-href"]
103
- || attributes["data-url"]
104
- || attributes.url
105
- || extractFirstHtmlAttribute(buttonHTML, ["href", "data-href", "data-url", "url"]);
106
- return looksLikeForbiddenChatResumePath(href)
107
- || looksLikeForbiddenChatResumePath(buttonHTML);
108
- }
109
-
110
- export function makeUnsafeChatOnlineResumeLinkError(target = {}, buttonHTML = "") {
111
- const href = target?.attributes?.href
112
- || target?.attributes?.["data-href"]
113
- || target?.attributes?.["data-url"]
114
- || extractFirstHtmlAttribute(buttonHTML, ["href", "data-href", "data-url", "url"])
115
- || null;
116
- const error = new Error("CHAT_UNSAFE_ONLINE_RESUME_LINK: refusing to click an online resume link that can navigate the chat tab to /web/frame/c-resume/");
117
- error.code = CHAT_UNSAFE_ONLINE_RESUME_LINK_CODE;
118
- error.href = href;
119
- error.button_selector = target?.selector || null;
120
- error.button_text = htmlToText(buttonHTML).slice(0, 120);
121
- error.button_html_length = String(buttonHTML || "").length;
122
- return error;
123
- }
124
-
125
- export function isUnsafeChatOnlineResumeLinkError(error) {
126
- return error?.code === CHAT_UNSAFE_ONLINE_RESUME_LINK_CODE
127
- || /CHAT_UNSAFE_ONLINE_RESUME_LINK/i.test(String(error?.message || error || ""));
128
- }
129
-
130
- export function createChatProfileNetworkRecorder(client) {
131
- const events = [];
132
- client.Network.responseReceived((event) => {
133
- const url = event?.response?.url || "";
134
- if (!matchesChatProfileNetwork(url)) return;
135
- events.push({
136
- requestId: event.requestId,
137
- url,
138
- status: event.response?.status,
139
- mimeType: event.response?.mimeType,
140
- type: event.type
141
- });
142
- });
143
- if (typeof client.Network.loadingFinished === "function") {
144
- client.Network.loadingFinished((event) => {
145
- const found = events.find((item) => item.requestId === event.requestId);
146
- if (!found) return;
147
- found.loading_finished = true;
148
- found.encodedDataLength = event.encodedDataLength;
149
- });
150
- }
151
- if (typeof client.Network.loadingFailed === "function") {
152
- client.Network.loadingFailed((event) => {
153
- const found = events.find((item) => item.requestId === event.requestId);
154
- if (!found) return;
155
- found.loading_failed = true;
156
- found.loading_error = event.errorText || event.blockedReason || "Network loading failed";
157
- });
158
- }
159
- return {
160
- events,
161
- clear() {
162
- events.length = 0;
163
- }
164
- };
165
- }
166
-
167
- export async function waitForChatProfileNetworkEvents(recorder, {
168
- minCount = 1,
169
- requireLoaded = true,
170
- timeoutMs = 8000,
171
- intervalMs = 120
172
- } = {}) {
173
- const started = Date.now();
174
- const events = Array.isArray(recorder) ? recorder : recorder?.events || [];
175
- let matching = [];
176
- while (Date.now() - started <= timeoutMs) {
177
- matching = events.filter((event) => (
178
- !requireLoaded
179
- || event.loading_finished === true
180
- || event.loading_failed === true
181
- ));
182
- if (matching.length >= minCount) {
183
- return {
184
- ok: true,
185
- elapsed_ms: Date.now() - started,
186
- count: matching.length,
187
- events: matching
188
- };
189
- }
190
- await sleep(intervalMs);
191
- }
192
- return {
193
- ok: false,
194
- elapsed_ms: Date.now() - started,
195
- count: matching.length,
196
- events: matching,
197
- total_event_count: events.length
198
- };
199
- }
200
-
201
- export async function readChatProfileNetworkBodies(client, events = [], {
202
- limit = 20
203
- } = {}) {
204
- const bodies = [];
205
- for (const event of events.slice(0, limit)) {
206
- try {
207
- const body = await client.Network.getResponseBody({ requestId: event.requestId });
208
- bodies.push({
209
- ...event,
210
- body,
211
- body_length: String(body?.body || "").length
212
- });
213
- } catch (error) {
214
- bodies.push({
215
- ...event,
216
- body_error: error?.message || String(error)
217
- });
218
- }
219
- }
220
- return bodies;
221
- }
222
-
223
- function normalizeDetailText(value = "") {
224
- return String(value || "").replace(/\s+/g, " ").trim();
225
- }
226
-
227
- function chatCandidateIdFromAttributes(attributes = {}) {
228
- return normalizeDetailText(
229
- attributes["data-id"]
230
- || attributes["data-geekid"]
231
- || attributes["data-geek"]
232
- || attributes["data-uid"]
233
- || attributes.key
234
- || attributes.id
235
- || ""
236
- );
237
- }
238
-
239
- async function hydrateActiveChatCandidate(client, activeCandidate = null) {
240
- if (!activeCandidate?.node_id) return activeCandidate;
241
- let attributes = {};
242
- let outerHTML = "";
243
- try {
244
- [attributes, outerHTML] = await Promise.all([
245
- getAttributesMap(client, activeCandidate.node_id),
246
- getOuterHTML(client, activeCandidate.node_id)
247
- ]);
248
- } catch {}
249
- return {
250
- ...activeCandidate,
251
- attributes,
252
- candidate_id: chatCandidateIdFromAttributes(attributes) || null,
253
- label: normalizeDetailText(htmlToText(outerHTML)),
254
- outer_html_length: outerHTML.length
255
- };
256
- }
257
-
258
- export async function waitForChatOnlineResumeButton(client, {
259
- timeoutMs = 12000,
260
- intervalMs = 250,
261
- expectedCandidateId = ""
26
+ CHAT_ATTACHMENT_RESUME_BUTTON_SELECTORS,
27
+ CHAT_BLOCKING_PANEL_CLOSE_SELECTORS,
28
+ CHAT_BLOCKING_PANEL_TEXT_QUERIES,
29
+ CHAT_CONFIRM_REQUEST_RESUME_SELECTORS,
30
+ CHAT_EDITOR_SELECTORS,
31
+ CHAT_MESSAGE_FILTER_SELECTORS,
32
+ CHAT_MESSAGE_LIST_SELECTORS,
33
+ CHAT_ONLINE_RESUME_BUTTON_SELECTORS,
34
+ CHAT_PRIMARY_LABEL_SELECTORS,
35
+ CHAT_PROFILE_NETWORK_PATTERNS,
36
+ CHAT_RESUME_CLOSE_SELECTORS,
37
+ CHAT_RESUME_CONTENT_SELECTORS,
38
+ CHAT_RESUME_FAST_MODAL_SELECTORS,
39
+ CHAT_RESUME_IFRAME_SELECTORS,
40
+ CHAT_RESUME_MODAL_SELECTORS,
41
+ CHAT_SEND_BUTTON_SELECTORS
42
+ } from "./constants.js";
43
+ import {
44
+ getChatRoots,
45
+ queryFirstAcrossChatRoots
46
+ } from "./roots.js";
47
+ import {
48
+ assertChatShellNotResumeTopLevel,
49
+ getChatTopLevelState,
50
+ isForbiddenChatResumeTopLevelUrl,
51
+ makeForbiddenChatResumeNavigationError
52
+ } from "./page-guard.js";
53
+
54
+ export const CHAT_UNSAFE_ONLINE_RESUME_LINK_CODE = "CHAT_UNSAFE_ONLINE_RESUME_LINK";
55
+
56
+ const CHAT_CONVERSATION_CONTROL_SCOPE_SELECTORS = Object.freeze([
57
+ ".conversation-main",
58
+ ".conversation-editor",
59
+ ".chat-message-list",
60
+ ".toolbar-box-right",
61
+ ".operate-exchange-left",
62
+ ".operate-icon-item",
63
+ ".exchange-tooltip",
64
+ ".boss-popup__wrapper",
65
+ ".boss-dialog",
66
+ ".dialog-wrap.active",
67
+ ".geek-detail-modal"
68
+ ]);
69
+
70
+ const CHAT_REQUESTED_RESUME_SCOPE_SELECTORS = Object.freeze([
71
+ ".chat-message-list",
72
+ ".conversation-editor",
73
+ ".conversation-main",
74
+ ".toolbar-box-right",
75
+ ".operate-exchange-left",
76
+ ".operate-icon-item",
77
+ ".exchange-tooltip",
78
+ ".boss-popup__wrapper",
79
+ ".boss-dialog",
80
+ ".dialog-wrap.active"
81
+ ]);
82
+
83
+ export function matchesChatProfileNetwork(url) {
84
+ return CHAT_PROFILE_NETWORK_PATTERNS.some((pattern) => pattern.test(String(url || "")));
85
+ }
86
+
87
+ function looksLikeForbiddenChatResumePath(value = "") {
88
+ const normalized = String(value || "");
89
+ return isForbiddenChatResumeTopLevelUrl(normalized)
90
+ || /(?:^|["'\s=])(?:https?:\/\/[^"'\s>]*zhipin\.com)?\/web\/frame\/c-resume(?:[/?#"' >]|$)/i
91
+ .test(normalized);
92
+ }
93
+
94
+ function extractFirstHtmlAttribute(html = "", names = []) {
95
+ const source = String(html || "");
96
+ for (const name of names) {
97
+ const escaped = String(name).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
98
+ const regex = new RegExp(`${escaped}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'>]+))`, "i");
99
+ const match = source.match(regex);
100
+ if (match) return match[1] ?? match[2] ?? match[3] ?? "";
101
+ }
102
+ return "";
103
+ }
104
+
105
+ export function isUnsafeChatOnlineResumeTarget(target = {}, buttonHTML = "") {
106
+ const attributes = target?.attributes || {};
107
+ const href = attributes.href
108
+ || attributes["data-href"]
109
+ || attributes["data-url"]
110
+ || attributes.url
111
+ || extractFirstHtmlAttribute(buttonHTML, ["href", "data-href", "data-url", "url"]);
112
+ return looksLikeForbiddenChatResumePath(href)
113
+ || looksLikeForbiddenChatResumePath(buttonHTML);
114
+ }
115
+
116
+ export function makeUnsafeChatOnlineResumeLinkError(target = {}, buttonHTML = "") {
117
+ const href = target?.attributes?.href
118
+ || target?.attributes?.["data-href"]
119
+ || target?.attributes?.["data-url"]
120
+ || extractFirstHtmlAttribute(buttonHTML, ["href", "data-href", "data-url", "url"])
121
+ || null;
122
+ const error = new Error("CHAT_UNSAFE_ONLINE_RESUME_LINK: refusing to click an online resume link that can navigate the chat tab to /web/frame/c-resume/");
123
+ error.code = CHAT_UNSAFE_ONLINE_RESUME_LINK_CODE;
124
+ error.href = href;
125
+ error.button_selector = target?.selector || null;
126
+ error.button_text = htmlToText(buttonHTML).slice(0, 120);
127
+ error.button_html_length = String(buttonHTML || "").length;
128
+ return error;
129
+ }
130
+
131
+ export function isUnsafeChatOnlineResumeLinkError(error) {
132
+ return error?.code === CHAT_UNSAFE_ONLINE_RESUME_LINK_CODE
133
+ || /CHAT_UNSAFE_ONLINE_RESUME_LINK/i.test(String(error?.message || error || ""));
134
+ }
135
+
136
+ export function createChatProfileNetworkRecorder(client) {
137
+ const events = [];
138
+ client.Network.responseReceived((event) => {
139
+ const url = event?.response?.url || "";
140
+ if (!matchesChatProfileNetwork(url)) return;
141
+ events.push({
142
+ requestId: event.requestId,
143
+ url,
144
+ status: event.response?.status,
145
+ mimeType: event.response?.mimeType,
146
+ type: event.type
147
+ });
148
+ });
149
+ if (typeof client.Network.loadingFinished === "function") {
150
+ client.Network.loadingFinished((event) => {
151
+ const found = events.find((item) => item.requestId === event.requestId);
152
+ if (!found) return;
153
+ found.loading_finished = true;
154
+ found.encodedDataLength = event.encodedDataLength;
155
+ });
156
+ }
157
+ if (typeof client.Network.loadingFailed === "function") {
158
+ client.Network.loadingFailed((event) => {
159
+ const found = events.find((item) => item.requestId === event.requestId);
160
+ if (!found) return;
161
+ found.loading_failed = true;
162
+ found.loading_error = event.errorText || event.blockedReason || "Network loading failed";
163
+ });
164
+ }
165
+ return {
166
+ events,
167
+ clear() {
168
+ events.length = 0;
169
+ }
170
+ };
171
+ }
172
+
173
+ export async function waitForChatProfileNetworkEvents(recorder, {
174
+ minCount = 1,
175
+ requireLoaded = true,
176
+ timeoutMs = 8000,
177
+ intervalMs = 120
178
+ } = {}) {
179
+ const started = Date.now();
180
+ const events = Array.isArray(recorder) ? recorder : recorder?.events || [];
181
+ let matching = [];
182
+ while (Date.now() - started <= timeoutMs) {
183
+ matching = events.filter((event) => (
184
+ !requireLoaded
185
+ || event.loading_finished === true
186
+ || event.loading_failed === true
187
+ ));
188
+ if (matching.length >= minCount) {
189
+ return {
190
+ ok: true,
191
+ elapsed_ms: Date.now() - started,
192
+ count: matching.length,
193
+ events: matching
194
+ };
195
+ }
196
+ await sleep(intervalMs);
197
+ }
198
+ return {
199
+ ok: false,
200
+ elapsed_ms: Date.now() - started,
201
+ count: matching.length,
202
+ events: matching,
203
+ total_event_count: events.length
204
+ };
205
+ }
206
+
207
+ export async function readChatProfileNetworkBodies(client, events = [], {
208
+ limit = 20
209
+ } = {}) {
210
+ const bodies = [];
211
+ for (const event of events.slice(0, limit)) {
212
+ try {
213
+ const body = await client.Network.getResponseBody({ requestId: event.requestId });
214
+ bodies.push({
215
+ ...event,
216
+ body,
217
+ body_length: String(body?.body || "").length
218
+ });
219
+ } catch (error) {
220
+ bodies.push({
221
+ ...event,
222
+ body_error: error?.message || String(error)
223
+ });
224
+ }
225
+ }
226
+ return bodies;
227
+ }
228
+
229
+ function normalizeDetailText(value = "") {
230
+ return String(value || "").replace(/\s+/g, " ").trim();
231
+ }
232
+
233
+ function chatCandidateIdFromAttributes(attributes = {}) {
234
+ return normalizeDetailText(
235
+ attributes["data-id"]
236
+ || attributes["data-geekid"]
237
+ || attributes["data-geek"]
238
+ || attributes["data-uid"]
239
+ || attributes.key
240
+ || attributes.id
241
+ || ""
242
+ );
243
+ }
244
+
245
+ async function hydrateActiveChatCandidate(client, activeCandidate = null) {
246
+ if (!activeCandidate?.node_id) return activeCandidate;
247
+ let attributes = {};
248
+ let outerHTML = "";
249
+ try {
250
+ [attributes, outerHTML] = await Promise.all([
251
+ getAttributesMap(client, activeCandidate.node_id),
252
+ getOuterHTML(client, activeCandidate.node_id)
253
+ ]);
254
+ } catch {}
255
+ return {
256
+ ...activeCandidate,
257
+ attributes,
258
+ candidate_id: chatCandidateIdFromAttributes(attributes) || null,
259
+ label: normalizeDetailText(htmlToText(outerHTML)),
260
+ outer_html_length: outerHTML.length
261
+ };
262
+ }
263
+
264
+ export async function waitForChatOnlineResumeButton(client, {
265
+ timeoutMs = 12000,
266
+ intervalMs = 250,
267
+ expectedCandidateId = ""
268
+ } = {}) {
269
+ const started = Date.now();
270
+ let lastState = null;
271
+ const expectedId = chatCandidateIdFromAttributes({ "data-id": expectedCandidateId });
272
+ while (Date.now() - started <= timeoutMs) {
273
+ const topLevelState = await getChatTopLevelState(client);
274
+ if (topLevelState.is_forbidden_resume_top_level) {
275
+ return {
276
+ forbidden_top_level_navigation: true,
277
+ top_level_state: topLevelState
278
+ };
279
+ }
280
+ const rootState = await getChatRoots(client);
281
+ const target = await findVisibleTarget(client, rootState.roots, CHAT_ONLINE_RESUME_BUTTON_SELECTORS);
282
+ const activeCandidate = await hydrateActiveChatCandidate(
283
+ client,
284
+ await queryFirstAcrossChatRoots(client, rootState.roots, CHAT_ACTIVE_CANDIDATE_SELECTORS)
285
+ );
286
+ const activeCandidateId = activeCandidate?.candidate_id || "";
287
+ const candidateSelectionVerified = expectedId
288
+ ? activeCandidateId === expectedId
289
+ : undefined;
290
+ lastState = {
291
+ roots: rootState.roots,
292
+ target,
293
+ activeCandidate,
294
+ expected_candidate_id: expectedId || null,
295
+ active_candidate_id: activeCandidateId || null,
296
+ candidate_selection_verified: candidateSelectionVerified
297
+ };
298
+ if (target && (!expectedId || candidateSelectionVerified)) {
299
+ return {
300
+ ok: true,
301
+ elapsed_ms: Date.now() - started,
302
+ ...lastState
303
+ };
304
+ }
305
+ await sleep(intervalMs);
306
+ }
307
+ return {
308
+ ok: false,
309
+ reason: expectedId && lastState?.candidate_selection_verified === false
310
+ ? "active_candidate_mismatch"
311
+ : "online_resume_button_unavailable",
312
+ elapsed_ms: Date.now() - started,
313
+ ...lastState
314
+ };
315
+ }
316
+
317
+ export async function selectChatCandidate(client, cardNodeId, {
318
+ timeoutMs = 12000,
319
+ settleMs = 1200
320
+ } = {}) {
321
+ const cardBox = await clickNodeCenter(client, cardNodeId, {
322
+ scrollIntoView: true
323
+ });
324
+ if (settleMs > 0) await sleep(settleMs);
325
+ const ready = await waitForChatOnlineResumeButton(client, { timeoutMs });
326
+ return {
327
+ card_box: cardBox,
328
+ ready
329
+ };
330
+ }
331
+
332
+ function hasActiveSignal(attributes = {}, outerHTML = "") {
333
+ return /\b(active|selected|current|curr)\b/i.test(String(attributes.class || ""))
334
+ || normalizeDetailText(attributes["aria-selected"]).toLowerCase() === "true"
335
+ || normalizeDetailText(attributes["data-active"]).toLowerCase() === "true"
336
+ || /\b(active|selected|current|curr)\b/i.test(String(outerHTML || "").slice(0, 500));
337
+ }
338
+
339
+ function isDisabledSignal(attributes = {}, outerHTML = "") {
340
+ return attributes.disabled !== undefined
341
+ || normalizeDetailText(attributes["aria-disabled"]).toLowerCase() === "true"
342
+ || /\b(disabled|disable|is-disabled)\b/i.test([
343
+ attributes.class,
344
+ String(outerHTML || "").slice(0, 500)
345
+ ].join(" "));
346
+ }
347
+
348
+ function isAskResumeText(text = "") {
349
+ const normalized = normalizeDetailText(text);
350
+ return Boolean(
351
+ normalized === "求简历"
352
+ || normalized === "索要简历"
353
+ || normalized === "求附件简历"
354
+ || normalized.includes("求简历")
355
+ || normalized.includes("索要简历")
356
+ || normalized.includes("求附件简历")
357
+ );
358
+ }
359
+
360
+ function isRequestedResumeText(text = "") {
361
+ const normalized = normalizeDetailText(text);
362
+ return Boolean(
363
+ normalized === "已求简历"
364
+ || normalized === "已索要简历"
365
+ || normalized.includes("已求简历")
366
+ || normalized.includes("已索要简历")
367
+ || normalized.includes("简历请求已发送")
368
+ || normalized.includes("已发送简历")
369
+ || (normalized.includes("已申请") && normalized.includes("简历"))
370
+ );
371
+ }
372
+
373
+ function isResumeRequestSentMessageText(text = "") {
374
+ const normalized = normalizeDetailText(text);
375
+ return Boolean(
376
+ normalized.includes("简历请求已发送")
377
+ || normalized.includes("已发送简历")
378
+ || normalized.includes("已求简历")
379
+ || normalized.includes("已索要简历")
380
+ || normalized.includes("已发送")
381
+ );
382
+ }
383
+
384
+ function countTextOccurrences(text = "", needle = "") {
385
+ if (!needle) return 0;
386
+ let count = 0;
387
+ let index = 0;
388
+ while (index < text.length) {
389
+ const found = text.indexOf(needle, index);
390
+ if (found < 0) break;
391
+ count += 1;
392
+ index = found + needle.length;
393
+ }
394
+ return count;
395
+ }
396
+
397
+ function countResumeRequestSentMessageMarkers(lines = []) {
398
+ const markers = ["简历请求已发送", "已发送简历", "已求简历", "已索要简历", "已发送"];
399
+ return lines.reduce((total, line) => (
400
+ total + markers.reduce((lineTotal, marker) => (
401
+ lineTotal + countTextOccurrences(line, marker)
402
+ ), 0)
403
+ ), 0);
404
+ }
405
+
406
+ function isResumeAttachmentMessageText(text = "") {
407
+ const normalized = normalizeDetailText(text);
408
+ return Boolean(
409
+ /点击.*附件简历/.test(normalized)
410
+ || /预览附件简历/.test(normalized)
411
+ || /查看附件简历/.test(normalized)
412
+ || /(?:简历|resume)[^\s]*\.(?:pdf|docx?|jpg|jpeg|png)\b/i.test(normalized)
413
+ );
414
+ }
415
+
416
+ function countResumeAttachmentMessageMarkers(lines = []) {
417
+ return lines.reduce((total, line) => total + (isResumeAttachmentMessageText(line) ? 1 : 0), 0);
418
+ }
419
+
420
+ function isRequestedResumeControlTarget(target = {}) {
421
+ const label = normalizeDetailText(target.label);
422
+ const className = String(target.attributes?.class || "");
423
+ const selector = String(target.selector || "");
424
+ const controlLike = /\boperate-btn\b|operate|resume|button|btn/i.test(`${selector} ${className}`);
425
+ if (isRequestedResumeText(label)) return true;
426
+ return controlLike && Boolean(
427
+ label === "已申请"
428
+ || label === "已发送"
429
+ || label.includes("已申请")
430
+ || label.includes("已发送")
431
+ );
432
+ }
433
+
434
+ function isAttachmentResumeText(text = "") {
435
+ const normalized = normalizeDetailText(text);
436
+ return Boolean(
437
+ normalized === "附件简历"
438
+ || (normalized.includes("附件简历") && !normalized.includes("求附件简历"))
439
+ );
440
+ }
441
+
442
+ function isAttachmentResumeTarget(target = {}) {
443
+ return isAttachmentResumeText(target.label)
444
+ || /resume-btn-file/i.test(String(target.attributes?.class || target.selector || ""));
445
+ }
446
+
447
+ function isConfirmText(text = "") {
448
+ const normalized = normalizeDetailText(text);
449
+ return Boolean(
450
+ normalized === "确定"
451
+ || normalized === "确认"
452
+ || normalized === "提交"
453
+ || normalized === "继续"
454
+ || normalized.includes("确定")
455
+ || normalized.includes("确认")
456
+ );
457
+ }
458
+
459
+ function isSendText(text = "") {
460
+ const normalized = normalizeDetailText(text);
461
+ return normalized === "发送" || normalized.includes("发送");
462
+ }
463
+
464
+ function isRecoverableNodeError(error) {
465
+ return /(?:Could not find node|No node with given id|Cannot find node|Could not compute box model)/i
466
+ .test(String(error?.message || error || ""));
467
+ }
468
+
469
+ async function readTarget(client, root, selector, nodeId) {
470
+ let attributes = {};
471
+ let outerHTML = "";
472
+ let readError = "";
473
+ try {
474
+ [attributes, outerHTML] = await Promise.all([
475
+ getAttributesMap(client, nodeId),
476
+ getOuterHTML(client, nodeId)
477
+ ]);
478
+ } catch (error) {
479
+ readError = error?.message || String(error);
480
+ }
481
+ const label = normalizeDetailText(htmlToText(outerHTML));
482
+ let box = null;
483
+ try {
484
+ box = await getNodeBox(client, nodeId);
485
+ } catch {}
486
+ return {
487
+ root: root.name,
488
+ root_node_id: root.nodeId,
489
+ selector,
490
+ node_id: nodeId,
491
+ label,
492
+ attributes,
493
+ disabled: isDisabledSignal(attributes, outerHTML),
494
+ active: hasActiveSignal(attributes, outerHTML),
495
+ visible: Boolean(box && box.rect.width > 2 && box.rect.height > 2),
496
+ center: box?.center || null,
497
+ rect: box?.rect || null,
498
+ outer_html_length: outerHTML.length,
499
+ read_error: readError || null
500
+ };
501
+ }
502
+
503
+ async function findVisibleMatchingTarget(client, roots, selectors, predicate) {
504
+ for (const root of roots) {
505
+ if (!root?.nodeId) continue;
506
+ for (const selector of selectors) {
507
+ const nodeIds = await querySelectorAll(client, root.nodeId, selector);
508
+ for (const nodeId of nodeIds) {
509
+ const target = await readTarget(client, root, selector, nodeId);
510
+ if (!target.visible) continue;
511
+ if (predicate(target)) return target;
512
+ }
513
+ }
514
+ }
515
+ return null;
516
+ }
517
+
518
+ async function resolveScopedRoots(client, roots = [], selectors = [], {
519
+ fallbackToRoots = true
520
+ } = {}) {
521
+ const scoped = [];
522
+ const seen = new Set();
523
+ for (const root of roots) {
524
+ if (!root?.nodeId) continue;
525
+ for (const selector of selectors) {
526
+ let nodeIds = [];
527
+ try {
528
+ nodeIds = await querySelectorAll(client, root.nodeId, selector);
529
+ } catch {
530
+ nodeIds = [];
531
+ }
532
+ for (const nodeId of nodeIds) {
533
+ const key = `${root.name}:${nodeId}`;
534
+ if (seen.has(key)) continue;
535
+ seen.add(key);
536
+ scoped.push({
537
+ name: `${root.name}:${selector}`,
538
+ nodeId
539
+ });
540
+ }
541
+ }
542
+ }
543
+ if (scoped.length || !fallbackToRoots) return scoped;
544
+ return roots;
545
+ }
546
+
547
+ export async function selectChatPrimaryLabel(client, {
548
+ label = "全部",
549
+ timeoutMs = 8000,
550
+ intervalMs = 300,
551
+ settleMs = 700
552
+ } = {}) {
553
+ const started = Date.now();
554
+ let lastCandidates = [];
555
+ while (Date.now() - started <= timeoutMs) {
556
+ const rootState = await getChatRoots(client);
557
+ const candidates = [];
558
+ for (const root of rootState.roots) {
559
+ for (const selector of CHAT_PRIMARY_LABEL_SELECTORS) {
560
+ const nodeIds = await querySelectorAll(client, root.nodeId, selector);
561
+ for (const nodeId of nodeIds) {
562
+ const target = await readTarget(client, root, selector, nodeId);
563
+ if (target.visible) candidates.push(target);
564
+ }
565
+ }
566
+ }
567
+ lastCandidates = candidates;
568
+ const matched = candidates.find((target) => (
569
+ target.label === label || target.label.startsWith(`${label}(`)
570
+ ));
571
+ if (matched?.active) {
572
+ return {
573
+ ok: true,
574
+ changed: false,
575
+ verified: true,
576
+ active_label: matched.label,
577
+ control: matched
578
+ };
579
+ }
580
+ if (matched) {
581
+ if (matched.center) {
582
+ await clickPoint(client, matched.center.x, matched.center.y, DETERMINISTIC_CLICK_OPTIONS);
583
+ } else {
584
+ await clickNodeCenter(client, matched.node_id, {
585
+ ...DETERMINISTIC_CLICK_OPTIONS,
586
+ scrollIntoView: true
587
+ });
588
+ }
589
+ if (settleMs > 0) await sleep(settleMs);
590
+ return {
591
+ ok: true,
592
+ changed: true,
593
+ verified: true,
594
+ active_label: label,
595
+ control: matched
596
+ };
597
+ }
598
+ await sleep(intervalMs);
599
+ }
600
+ return {
601
+ ok: false,
602
+ error: `CHAT_PRIMARY_LABEL_NOT_FOUND:${label}`,
603
+ candidates: lastCandidates.map((item) => ({
604
+ label: item.label,
605
+ selector: item.selector,
606
+ active: item.active
607
+ }))
608
+ };
609
+ }
610
+
611
+ export async function selectChatMessageFilter(client, {
612
+ startFrom = "unread",
613
+ timeoutMs = 8000,
614
+ intervalMs = 300,
615
+ settleMs = 900
616
+ } = {}) {
617
+ const label = startFrom === "all" ? "全部" : "未读";
618
+ const started = Date.now();
619
+ let lastCandidates = [];
620
+ while (Date.now() - started <= timeoutMs) {
621
+ const rootState = await getChatRoots(client);
622
+ const candidates = [];
623
+ for (const root of rootState.roots) {
624
+ for (const selector of CHAT_MESSAGE_FILTER_SELECTORS) {
625
+ const nodeIds = await querySelectorAll(client, root.nodeId, selector);
626
+ for (const nodeId of nodeIds) {
627
+ const target = await readTarget(client, root, selector, nodeId);
628
+ if (target.visible && target.label === label) candidates.push(target);
629
+ }
630
+ }
631
+ }
632
+ lastCandidates = candidates;
633
+ const active = candidates.find((target) => target.active);
634
+ if (active) {
635
+ return {
636
+ ok: true,
637
+ changed: false,
638
+ verified: true,
639
+ active_label: active.label,
640
+ control: active
641
+ };
642
+ }
643
+ const matched = candidates[0];
644
+ if (matched) {
645
+ if (matched.center) {
646
+ await clickPoint(client, matched.center.x, matched.center.y, DETERMINISTIC_CLICK_OPTIONS);
647
+ } else {
648
+ await clickNodeCenter(client, matched.node_id, {
649
+ ...DETERMINISTIC_CLICK_OPTIONS,
650
+ scrollIntoView: true
651
+ });
652
+ }
653
+ if (settleMs > 0) await sleep(settleMs);
654
+ return {
655
+ ok: true,
656
+ changed: true,
657
+ verified: true,
658
+ active_label: label,
659
+ control: matched
660
+ };
661
+ }
662
+ await sleep(intervalMs);
663
+ }
664
+ return {
665
+ ok: false,
666
+ error: `CHAT_MESSAGE_FILTER_NOT_FOUND:${label}`,
667
+ candidates: lastCandidates.map((item) => ({
668
+ label: item.label,
669
+ selector: item.selector,
670
+ active: item.active
671
+ }))
672
+ };
673
+ }
674
+
675
+ export async function waitForChatResumeModal(client, {
676
+ timeoutMs = 12000,
677
+ intervalMs = 250
678
+ } = {}) {
679
+ const started = Date.now();
680
+ let lastState = null;
681
+ while (Date.now() - started <= timeoutMs) {
682
+ const topLevelState = await getChatTopLevelState(client);
683
+ if (topLevelState.is_forbidden_resume_top_level) {
684
+ return {
685
+ forbidden_top_level_navigation: true,
686
+ top_level_state: topLevelState
687
+ };
688
+ }
689
+ const rootState = await getChatRoots(client);
690
+ const popup = await findVisibleTarget(client, rootState.roots, CHAT_RESUME_MODAL_SELECTORS);
691
+ const content = await findVisibleTarget(client, rootState.roots, CHAT_RESUME_CONTENT_SELECTORS);
692
+ const resumeIframe = await findVisibleTarget(client, rootState.roots, CHAT_RESUME_IFRAME_SELECTORS);
693
+ lastState = {
694
+ roots: rootState.roots,
695
+ popup,
696
+ content,
697
+ resumeIframe
698
+ };
699
+ if (popup || content || resumeIframe) return lastState;
700
+ await sleep(intervalMs);
701
+ }
702
+ return lastState;
703
+ }
704
+
705
+ export async function quickChatResumeModalOpenProbe(client, {
706
+ selectors = CHAT_RESUME_FAST_MODAL_SELECTORS
707
+ } = {}) {
708
+ const rootState = await getChatRoots(client);
709
+ for (const root of rootState.roots) {
710
+ if (!root?.nodeId) continue;
711
+ for (const selector of selectors) {
712
+ let nodeIds = [];
713
+ try {
714
+ nodeIds = await querySelectorAll(client, root.nodeId, selector);
715
+ } catch {
716
+ nodeIds = [];
717
+ }
718
+ for (const nodeId of nodeIds.slice(0, 4)) {
719
+ try {
720
+ const box = await getNodeBox(client, nodeId);
721
+ if (box?.rect?.width > 8 && box?.rect?.height > 8) {
722
+ return {
723
+ open: true,
724
+ root: root.name,
725
+ selector,
726
+ node_id: nodeId,
727
+ rect: box.rect,
728
+ center: box.center
729
+ };
730
+ }
731
+ } catch {
732
+ // Hidden or stale modal probes are ignored.
733
+ }
734
+ }
735
+ }
736
+ }
737
+ return {
738
+ open: false
739
+ };
740
+ }
741
+
742
+ export async function findChatBlockingPanel(client, {
743
+ textQueries = CHAT_BLOCKING_PANEL_TEXT_QUERIES
262
744
  } = {}) {
263
- const started = Date.now();
264
- let lastState = null;
265
- const expectedId = chatCandidateIdFromAttributes({ "data-id": expectedCandidateId });
266
- while (Date.now() - started <= timeoutMs) {
267
- const topLevelState = await getChatTopLevelState(client);
268
- if (topLevelState.is_forbidden_resume_top_level) {
269
- return {
270
- forbidden_top_level_navigation: true,
271
- top_level_state: topLevelState
272
- };
273
- }
274
- const rootState = await getChatRoots(client);
275
- const target = await findVisibleTarget(client, rootState.roots, CHAT_ONLINE_RESUME_BUTTON_SELECTORS);
276
- const activeCandidate = await hydrateActiveChatCandidate(
277
- client,
278
- await queryFirstAcrossChatRoots(client, rootState.roots, CHAT_ACTIVE_CANDIDATE_SELECTORS)
279
- );
280
- const activeCandidateId = activeCandidate?.candidate_id || "";
281
- const candidateSelectionVerified = expectedId
282
- ? activeCandidateId === expectedId
283
- : undefined;
284
- lastState = {
285
- roots: rootState.roots,
286
- target,
287
- activeCandidate,
288
- expected_candidate_id: expectedId || null,
289
- active_candidate_id: activeCandidateId || null,
290
- candidate_selection_verified: candidateSelectionVerified
291
- };
292
- if (target && (!expectedId || candidateSelectionVerified)) {
293
- return {
294
- ok: true,
295
- elapsed_ms: Date.now() - started,
296
- ...lastState
297
- };
298
- }
299
- await sleep(intervalMs);
300
- }
301
- return {
302
- ok: false,
303
- reason: expectedId && lastState?.candidate_selection_verified === false
304
- ? "active_candidate_mismatch"
305
- : "online_resume_button_unavailable",
306
- elapsed_ms: Date.now() - started,
307
- ...lastState
308
- };
745
+ const panel = await findBossAccountRightsBlockingPanel(client, { textQueries });
746
+ return panel.open
747
+ ? { ...panel, reason: "blocking_panel_text_visible" }
748
+ : panel;
309
749
  }
310
750
 
311
- export async function selectChatCandidate(client, cardNodeId, {
312
- timeoutMs = 12000,
313
- settleMs = 1200
751
+ export async function closeChatBlockingPanels(client, {
752
+ attemptsLimit = 2,
753
+ closeSelectors = CHAT_BLOCKING_PANEL_CLOSE_SELECTORS,
754
+ textQueries = CHAT_BLOCKING_PANEL_TEXT_QUERIES
314
755
  } = {}) {
315
- const cardBox = await clickNodeCenter(client, cardNodeId, {
316
- scrollIntoView: true
756
+ return closeBossAccountRightsBlockingPanel(client, {
757
+ attemptsLimit,
758
+ closeSelectors,
759
+ resolveRoots: getChatRoots,
760
+ textQueries
317
761
  });
318
- if (settleMs > 0) await sleep(settleMs);
319
- const ready = await waitForChatOnlineResumeButton(client, { timeoutMs });
320
- return {
321
- card_box: cardBox,
322
- ready
323
- };
324
- }
325
-
326
- function hasActiveSignal(attributes = {}, outerHTML = "") {
327
- return /\b(active|selected|current|curr)\b/i.test(String(attributes.class || ""))
328
- || normalizeDetailText(attributes["aria-selected"]).toLowerCase() === "true"
329
- || normalizeDetailText(attributes["data-active"]).toLowerCase() === "true"
330
- || /\b(active|selected|current|curr)\b/i.test(String(outerHTML || "").slice(0, 500));
331
- }
332
-
333
- function isDisabledSignal(attributes = {}, outerHTML = "") {
334
- return attributes.disabled !== undefined
335
- || normalizeDetailText(attributes["aria-disabled"]).toLowerCase() === "true"
336
- || /\b(disabled|disable|is-disabled)\b/i.test([
337
- attributes.class,
338
- String(outerHTML || "").slice(0, 500)
339
- ].join(" "));
340
- }
341
-
342
- function isAskResumeText(text = "") {
343
- const normalized = normalizeDetailText(text);
344
- return Boolean(
345
- normalized === "求简历"
346
- || normalized === "索要简历"
347
- || normalized === "求附件简历"
348
- || normalized.includes("求简历")
349
- || normalized.includes("索要简历")
350
- || normalized.includes("求附件简历")
351
- );
352
- }
353
-
354
- function isRequestedResumeText(text = "") {
355
- const normalized = normalizeDetailText(text);
356
- return Boolean(
357
- normalized === "已求简历"
358
- || normalized === "已索要简历"
359
- || normalized.includes("已求简历")
360
- || normalized.includes("已索要简历")
361
- || normalized.includes("简历请求已发送")
362
- || normalized.includes("已发送简历")
363
- || (normalized.includes("已申请") && normalized.includes("简历"))
364
- );
365
- }
366
-
367
- function isResumeRequestSentMessageText(text = "") {
368
- const normalized = normalizeDetailText(text);
369
- return Boolean(
370
- normalized.includes("简历请求已发送")
371
- || normalized.includes("已发送简历")
372
- || normalized.includes("已求简历")
373
- || normalized.includes("已索要简历")
374
- || normalized.includes("已发送")
375
- );
376
- }
377
-
378
- function countTextOccurrences(text = "", needle = "") {
379
- if (!needle) return 0;
380
- let count = 0;
381
- let index = 0;
382
- while (index < text.length) {
383
- const found = text.indexOf(needle, index);
384
- if (found < 0) break;
385
- count += 1;
386
- index = found + needle.length;
387
- }
388
- return count;
389
- }
390
-
391
- function countResumeRequestSentMessageMarkers(lines = []) {
392
- const markers = ["简历请求已发送", "已发送简历", "已求简历", "已索要简历", "已发送"];
393
- return lines.reduce((total, line) => (
394
- total + markers.reduce((lineTotal, marker) => (
395
- lineTotal + countTextOccurrences(line, marker)
396
- ), 0)
397
- ), 0);
398
- }
399
-
400
- function isResumeAttachmentMessageText(text = "") {
401
- const normalized = normalizeDetailText(text);
402
- return Boolean(
403
- /点击.*附件简历/.test(normalized)
404
- || /预览附件简历/.test(normalized)
405
- || /查看附件简历/.test(normalized)
406
- || /(?:简历|resume)[^\s]*\.(?:pdf|docx?|jpg|jpeg|png)\b/i.test(normalized)
407
- );
408
- }
409
-
410
- function countResumeAttachmentMessageMarkers(lines = []) {
411
- return lines.reduce((total, line) => total + (isResumeAttachmentMessageText(line) ? 1 : 0), 0);
412
- }
413
-
414
- function isRequestedResumeControlTarget(target = {}) {
415
- const label = normalizeDetailText(target.label);
416
- const className = String(target.attributes?.class || "");
417
- const selector = String(target.selector || "");
418
- const controlLike = /\boperate-btn\b|operate|resume|button|btn/i.test(`${selector} ${className}`);
419
- if (isRequestedResumeText(label)) return true;
420
- return controlLike && Boolean(
421
- label === "已申请"
422
- || label === "已发送"
423
- || label.includes("已申请")
424
- || label.includes("已发送")
425
- );
426
- }
427
-
428
- function isAttachmentResumeText(text = "") {
429
- const normalized = normalizeDetailText(text);
430
- return Boolean(
431
- normalized === "附件简历"
432
- || (normalized.includes("附件简历") && !normalized.includes("求附件简历"))
433
- );
434
- }
435
-
436
- function isAttachmentResumeTarget(target = {}) {
437
- return isAttachmentResumeText(target.label)
438
- || /resume-btn-file/i.test(String(target.attributes?.class || target.selector || ""));
439
- }
440
-
441
- function isConfirmText(text = "") {
442
- const normalized = normalizeDetailText(text);
443
- return Boolean(
444
- normalized === "确定"
445
- || normalized === "确认"
446
- || normalized === "提交"
447
- || normalized === "继续"
448
- || normalized.includes("确定")
449
- || normalized.includes("确认")
450
- );
451
- }
452
-
453
- function isSendText(text = "") {
454
- const normalized = normalizeDetailText(text);
455
- return normalized === "发送" || normalized.includes("发送");
456
- }
457
-
458
- function isRecoverableNodeError(error) {
459
- return /(?:Could not find node|No node with given id|Cannot find node|Could not compute box model)/i
460
- .test(String(error?.message || error || ""));
461
- }
462
-
463
- async function readTarget(client, root, selector, nodeId) {
464
- let attributes = {};
465
- let outerHTML = "";
466
- let readError = "";
467
- try {
468
- [attributes, outerHTML] = await Promise.all([
469
- getAttributesMap(client, nodeId),
470
- getOuterHTML(client, nodeId)
471
- ]);
472
- } catch (error) {
473
- readError = error?.message || String(error);
474
- }
475
- const label = normalizeDetailText(htmlToText(outerHTML));
476
- let box = null;
477
- try {
478
- box = await getNodeBox(client, nodeId);
479
- } catch {}
480
- return {
481
- root: root.name,
482
- root_node_id: root.nodeId,
483
- selector,
484
- node_id: nodeId,
485
- label,
486
- attributes,
487
- disabled: isDisabledSignal(attributes, outerHTML),
488
- active: hasActiveSignal(attributes, outerHTML),
489
- visible: Boolean(box && box.rect.width > 2 && box.rect.height > 2),
490
- center: box?.center || null,
491
- rect: box?.rect || null,
492
- outer_html_length: outerHTML.length,
493
- read_error: readError || null
494
- };
495
- }
496
-
497
- async function findVisibleMatchingTarget(client, roots, selectors, predicate) {
498
- for (const root of roots) {
499
- if (!root?.nodeId) continue;
500
- for (const selector of selectors) {
501
- const nodeIds = await querySelectorAll(client, root.nodeId, selector);
502
- for (const nodeId of nodeIds) {
503
- const target = await readTarget(client, root, selector, nodeId);
504
- if (!target.visible) continue;
505
- if (predicate(target)) return target;
506
- }
507
- }
508
- }
509
- return null;
510
- }
511
-
512
- async function resolveScopedRoots(client, roots = [], selectors = [], {
513
- fallbackToRoots = true
514
- } = {}) {
515
- const scoped = [];
516
- const seen = new Set();
517
- for (const root of roots) {
518
- if (!root?.nodeId) continue;
519
- for (const selector of selectors) {
520
- let nodeIds = [];
521
- try {
522
- nodeIds = await querySelectorAll(client, root.nodeId, selector);
523
- } catch {
524
- nodeIds = [];
525
- }
526
- for (const nodeId of nodeIds) {
527
- const key = `${root.name}:${nodeId}`;
528
- if (seen.has(key)) continue;
529
- seen.add(key);
530
- scoped.push({
531
- name: `${root.name}:${selector}`,
532
- nodeId
533
- });
534
- }
535
- }
536
- }
537
- if (scoped.length || !fallbackToRoots) return scoped;
538
- return roots;
539
- }
540
-
541
- export async function selectChatPrimaryLabel(client, {
542
- label = "全部",
543
- timeoutMs = 8000,
544
- intervalMs = 300,
545
- settleMs = 700
546
- } = {}) {
547
- const started = Date.now();
548
- let lastCandidates = [];
549
- while (Date.now() - started <= timeoutMs) {
550
- const rootState = await getChatRoots(client);
551
- const candidates = [];
552
- for (const root of rootState.roots) {
553
- for (const selector of CHAT_PRIMARY_LABEL_SELECTORS) {
554
- const nodeIds = await querySelectorAll(client, root.nodeId, selector);
555
- for (const nodeId of nodeIds) {
556
- const target = await readTarget(client, root, selector, nodeId);
557
- if (target.visible) candidates.push(target);
558
- }
559
- }
560
- }
561
- lastCandidates = candidates;
562
- const matched = candidates.find((target) => (
563
- target.label === label || target.label.startsWith(`${label}(`)
564
- ));
565
- if (matched?.active) {
566
- return {
567
- ok: true,
568
- changed: false,
569
- verified: true,
570
- active_label: matched.label,
571
- control: matched
572
- };
573
- }
574
- if (matched) {
575
- if (matched.center) {
576
- await clickPoint(client, matched.center.x, matched.center.y, DETERMINISTIC_CLICK_OPTIONS);
577
- } else {
578
- await clickNodeCenter(client, matched.node_id, {
579
- ...DETERMINISTIC_CLICK_OPTIONS,
580
- scrollIntoView: true
581
- });
582
- }
583
- if (settleMs > 0) await sleep(settleMs);
584
- return {
585
- ok: true,
586
- changed: true,
587
- verified: true,
588
- active_label: label,
589
- control: matched
590
- };
591
- }
592
- await sleep(intervalMs);
593
- }
594
- return {
595
- ok: false,
596
- error: `CHAT_PRIMARY_LABEL_NOT_FOUND:${label}`,
597
- candidates: lastCandidates.map((item) => ({
598
- label: item.label,
599
- selector: item.selector,
600
- active: item.active
601
- }))
602
- };
603
- }
604
-
605
- export async function selectChatMessageFilter(client, {
606
- startFrom = "unread",
607
- timeoutMs = 8000,
608
- intervalMs = 300,
609
- settleMs = 900
610
- } = {}) {
611
- const label = startFrom === "all" ? "全部" : "未读";
612
- const started = Date.now();
613
- let lastCandidates = [];
614
- while (Date.now() - started <= timeoutMs) {
615
- const rootState = await getChatRoots(client);
616
- const candidates = [];
617
- for (const root of rootState.roots) {
618
- for (const selector of CHAT_MESSAGE_FILTER_SELECTORS) {
619
- const nodeIds = await querySelectorAll(client, root.nodeId, selector);
620
- for (const nodeId of nodeIds) {
621
- const target = await readTarget(client, root, selector, nodeId);
622
- if (target.visible && target.label === label) candidates.push(target);
623
- }
624
- }
625
- }
626
- lastCandidates = candidates;
627
- const active = candidates.find((target) => target.active);
628
- if (active) {
629
- return {
630
- ok: true,
631
- changed: false,
632
- verified: true,
633
- active_label: active.label,
634
- control: active
635
- };
636
- }
637
- const matched = candidates[0];
638
- if (matched) {
639
- if (matched.center) {
640
- await clickPoint(client, matched.center.x, matched.center.y, DETERMINISTIC_CLICK_OPTIONS);
641
- } else {
642
- await clickNodeCenter(client, matched.node_id, {
643
- ...DETERMINISTIC_CLICK_OPTIONS,
644
- scrollIntoView: true
645
- });
646
- }
647
- if (settleMs > 0) await sleep(settleMs);
648
- return {
649
- ok: true,
650
- changed: true,
651
- verified: true,
652
- active_label: label,
653
- control: matched
654
- };
655
- }
656
- await sleep(intervalMs);
657
- }
658
- return {
659
- ok: false,
660
- error: `CHAT_MESSAGE_FILTER_NOT_FOUND:${label}`,
661
- candidates: lastCandidates.map((item) => ({
662
- label: item.label,
663
- selector: item.selector,
664
- active: item.active
665
- }))
666
- };
667
- }
668
-
669
- export async function waitForChatResumeModal(client, {
670
- timeoutMs = 12000,
671
- intervalMs = 250
672
- } = {}) {
673
- const started = Date.now();
674
- let lastState = null;
675
- while (Date.now() - started <= timeoutMs) {
676
- const topLevelState = await getChatTopLevelState(client);
677
- if (topLevelState.is_forbidden_resume_top_level) {
678
- return {
679
- forbidden_top_level_navigation: true,
680
- top_level_state: topLevelState
681
- };
682
- }
683
- const rootState = await getChatRoots(client);
684
- const popup = await findVisibleTarget(client, rootState.roots, CHAT_RESUME_MODAL_SELECTORS);
685
- const content = await findVisibleTarget(client, rootState.roots, CHAT_RESUME_CONTENT_SELECTORS);
686
- const resumeIframe = await findVisibleTarget(client, rootState.roots, CHAT_RESUME_IFRAME_SELECTORS);
687
- lastState = {
688
- roots: rootState.roots,
689
- popup,
690
- content,
691
- resumeIframe
692
- };
693
- if (popup || content || resumeIframe) return lastState;
694
- await sleep(intervalMs);
695
- }
696
- return lastState;
697
- }
698
-
699
- export async function quickChatResumeModalOpenProbe(client, {
700
- selectors = CHAT_RESUME_FAST_MODAL_SELECTORS
701
- } = {}) {
702
- const rootState = await getChatRoots(client);
703
- for (const root of rootState.roots) {
704
- if (!root?.nodeId) continue;
705
- for (const selector of selectors) {
706
- let nodeIds = [];
707
- try {
708
- nodeIds = await querySelectorAll(client, root.nodeId, selector);
709
- } catch {
710
- nodeIds = [];
711
- }
712
- for (const nodeId of nodeIds.slice(0, 4)) {
713
- try {
714
- const box = await getNodeBox(client, nodeId);
715
- if (box?.rect?.width > 8 && box?.rect?.height > 8) {
716
- return {
717
- open: true,
718
- root: root.name,
719
- selector,
720
- node_id: nodeId,
721
- rect: box.rect,
722
- center: box.center
723
- };
724
- }
725
- } catch {
726
- // Hidden or stale modal probes are ignored.
727
- }
728
- }
729
- }
730
- }
731
- return {
732
- open: false
733
- };
734
762
  }
735
763
 
736
764
  export async function readChatResumeHtml(client, resumeState) {
737
- let popupHTML = "";
738
- let contentHTML = "";
739
- let resumeIframeHTML = "";
740
- let resumeIframeDocumentNodeId = null;
741
-
742
- if (resumeState?.popup?.node_id) {
743
- popupHTML = await getOuterHTML(client, resumeState.popup.node_id);
744
- }
745
-
746
- if (resumeState?.content?.node_id && resumeState.content.node_id !== resumeState?.popup?.node_id) {
747
- contentHTML = await getOuterHTML(client, resumeState.content.node_id);
748
- }
749
-
750
- if (resumeState?.resumeIframe?.node_id) {
751
- resumeIframeDocumentNodeId = await getFrameDocumentNodeId(client, resumeState.resumeIframe.node_id);
752
- resumeIframeHTML = await getOuterHTML(client, resumeIframeDocumentNodeId);
753
- }
754
-
755
- return {
756
- popupHTML,
757
- contentHTML,
758
- resumeIframeHTML,
759
- resumeIframeDocumentNodeId,
760
- popupText: htmlToText(popupHTML),
761
- contentText: htmlToText(contentHTML),
762
- resumeIframeText: htmlToText(resumeIframeHTML)
763
- };
764
- }
765
-
766
- function emptyChatResumeHtml(readError = null) {
767
- return {
768
- popupHTML: "",
769
- contentHTML: "",
770
- resumeIframeHTML: "",
771
- resumeIframeDocumentNodeId: null,
772
- popupText: "",
773
- contentText: "",
774
- resumeIframeText: "",
775
- readError: readError?.message || null
776
- };
777
- }
778
-
779
- export async function waitForChatResumeContent(client, {
780
- minTextLength = 120,
781
- timeoutMs = 15000,
782
- intervalMs = 250
783
- } = {}) {
784
- const started = Date.now();
785
- let lastState = null;
786
- let lastHtml = null;
787
- let lastError = null;
788
- while (Date.now() - started <= timeoutMs) {
789
- try {
790
- lastState = await waitForChatResumeModal(client, {
791
- timeoutMs: 700,
792
- intervalMs: 100
793
- });
794
- if (lastState?.popup || lastState?.content || lastState?.resumeIframe) {
795
- lastHtml = await readChatResumeHtml(client, lastState);
796
- const textLength = [
797
- lastHtml.popupText,
798
- lastHtml.contentText,
799
- lastHtml.resumeIframeText
800
- ].join("\n").length;
801
- if (textLength >= minTextLength) {
802
- return {
803
- ok: true,
804
- elapsed_ms: Date.now() - started,
805
- text_length: textLength,
806
- resume_state: lastState,
807
- resume_html: lastHtml
808
- };
809
- }
810
- }
811
- } catch (error) {
812
- lastError = error;
813
- }
814
- await sleep(intervalMs);
815
- }
816
-
817
- const textLength = [
818
- lastHtml?.popupText,
819
- lastHtml?.contentText,
820
- lastHtml?.resumeIframeText
821
- ].filter(Boolean).join("\n").length;
822
- return {
823
- ok: false,
824
- elapsed_ms: Date.now() - started,
825
- text_length: textLength,
826
- resume_state: lastState,
827
- resume_html: lastHtml,
828
- error: lastError?.message || null
829
- };
830
- }
831
-
832
- export async function openChatOnlineResume(client, {
833
- timeoutMs = 15000,
834
- attemptsLimit = 3,
835
- settleMs = 1200
836
- } = {}) {
837
- const attempts = [];
838
- for (let index = 0; index < attemptsLimit; index += 1) {
839
- if (settleMs > 0) await sleep(settleMs);
840
- await assertChatShellNotResumeTopLevel(client, {
841
- context: "openChatOnlineResume:before_existing_modal_check"
842
- });
843
- const existingResumeState = await waitForChatResumeModal(client, {
844
- timeoutMs: 500,
845
- intervalMs: 100
846
- });
847
- if (existingResumeState?.forbidden_top_level_navigation) {
848
- throw makeForbiddenChatResumeNavigationError(existingResumeState.top_level_state);
849
- }
850
- if (
851
- existingResumeState?.popup
852
- || existingResumeState?.content
853
- || existingResumeState?.resumeIframe
854
- ) {
855
- attempts.push({
856
- attempt: index + 1,
857
- ok: true,
858
- reused_existing_modal: true,
859
- resume_popup_selector: existingResumeState?.popup?.selector || null,
860
- resume_content_selector: existingResumeState?.content?.selector || null,
861
- resume_iframe_selector: existingResumeState?.resumeIframe?.selector || null
862
- });
863
- return {
864
- button: null,
865
- button_html: "",
866
- resume_state: existingResumeState,
867
- attempts
868
- };
869
- }
870
-
871
- const buttonState = await waitForChatOnlineResumeButton(client, {
872
- timeoutMs: Math.min(timeoutMs, 8000)
873
- });
874
- if (!buttonState?.target?.node_id) {
875
- attempts.push({
876
- attempt: index + 1,
877
- ok: false,
878
- error: "ONLINE_RESUME_BUTTON_NOT_FOUND"
879
- });
880
- continue;
881
- }
882
-
883
- let buttonHTML = "";
884
- try {
885
- buttonHTML = await getOuterHTML(client, buttonState.target.node_id);
886
- } catch {}
887
-
888
- if (isUnsafeChatOnlineResumeTarget(buttonState.target, buttonHTML)) {
889
- const error = makeUnsafeChatOnlineResumeLinkError(buttonState.target, buttonHTML);
890
- attempts.push({
891
- attempt: index + 1,
892
- ok: false,
893
- error: error.code,
894
- blocked_pre_click: true,
895
- button_selector: buttonState.target.selector,
896
- button_text: error.button_text,
897
- button_href: error.href,
898
- button_html_length: buttonHTML.length
899
- });
900
- error.attempts = attempts;
901
- throw error;
902
- }
903
-
904
- try {
905
- if (buttonState.target.center) {
906
- await clickPoint(client, buttonState.target.center.x, buttonState.target.center.y);
907
- } else {
908
- await clickNodeCenter(client, buttonState.target.node_id, {
909
- scrollIntoView: true
910
- });
911
- }
912
- } catch (error) {
913
- attempts.push({
914
- attempt: index + 1,
915
- ok: false,
916
- error: error?.message || String(error),
917
- recoverable_stale_node: isRecoverableNodeError(error),
918
- button_selector: buttonState.target.selector,
919
- button_text: htmlToText(buttonHTML).slice(0, 120),
920
- button_html_length: buttonHTML.length
921
- });
922
- if (isRecoverableNodeError(error)) {
923
- await sleep(350);
924
- continue;
925
- }
926
- throw error;
927
- }
928
- await assertChatShellNotResumeTopLevel(client, {
929
- context: "openChatOnlineResume:after_online_resume_click"
930
- });
931
- const resumeState = await waitForChatResumeModal(client, {
932
- timeoutMs: Math.max(2500, Math.floor(timeoutMs / attemptsLimit))
933
- });
934
- if (resumeState?.forbidden_top_level_navigation) {
935
- throw makeForbiddenChatResumeNavigationError(resumeState.top_level_state);
936
- }
937
- attempts.push({
938
- attempt: index + 1,
939
- ok: Boolean(resumeState?.popup || resumeState?.content || resumeState?.resumeIframe),
940
- button_selector: buttonState.target.selector,
941
- button_text: htmlToText(buttonHTML).slice(0, 120),
942
- button_html_length: buttonHTML.length,
943
- resume_popup_selector: resumeState?.popup?.selector || null,
944
- resume_content_selector: resumeState?.content?.selector || null,
945
- resume_iframe_selector: resumeState?.resumeIframe?.selector || null
946
- });
947
- if (resumeState?.popup || resumeState?.content || resumeState?.resumeIframe) {
948
- return {
949
- button: buttonState.target,
950
- button_html: buttonHTML,
951
- resume_state: resumeState,
952
- attempts
953
- };
954
- }
955
- }
956
-
957
- const error = new Error("Chat online resume modal did not open");
958
- error.attempts = attempts;
959
- throw error;
960
- }
961
-
962
- export async function readChatConversationReadyState(client) {
963
- const rootState = await getChatRoots(client);
964
- const scopedControlRoots = await resolveScopedRoots(
965
- client,
966
- rootState.roots,
967
- CHAT_CONVERSATION_CONTROL_SCOPE_SELECTORS,
968
- { fallbackToRoots: false }
969
- );
970
- const scopedRequestedRoots = await resolveScopedRoots(
971
- client,
972
- rootState.roots,
973
- CHAT_REQUESTED_RESUME_SCOPE_SELECTORS,
974
- { fallbackToRoots: false }
975
- );
976
- const controlRoots = scopedControlRoots.length ? scopedControlRoots : rootState.roots;
977
- const requestedRoots = scopedRequestedRoots.length ? scopedRequestedRoots : rootState.roots;
978
- const onlineResume = await findVisibleMatchingTarget(
979
- client,
980
- controlRoots,
981
- CHAT_ONLINE_RESUME_BUTTON_SELECTORS,
982
- (target) => target.label.includes("在线简历") && !target.disabled
983
- );
984
- const attachmentResume = await findVisibleMatchingTarget(
985
- client,
986
- controlRoots,
987
- CHAT_ATTACHMENT_RESUME_BUTTON_SELECTORS,
988
- (target) => isAttachmentResumeText(target.label)
989
- );
990
- const askResume = await findVisibleMatchingTarget(
991
- client,
992
- controlRoots,
993
- CHAT_ASK_RESUME_BUTTON_SELECTORS,
994
- (target) => isAskResumeText(target.label) && !isAttachmentResumeTarget(target)
995
- );
996
- const requestedResume = await findVisibleMatchingTarget(
997
- client,
998
- requestedRoots,
999
- CHAT_ASK_RESUME_BUTTON_SELECTORS,
1000
- (target) => isRequestedResumeControlTarget(target)
1001
- );
1002
- const editor = await findVisibleMatchingTarget(
1003
- client,
1004
- controlRoots,
1005
- CHAT_EDITOR_SELECTORS,
1006
- () => true
1007
- );
1008
- const sendButton = await findVisibleMatchingTarget(
1009
- client,
1010
- controlRoots,
1011
- CHAT_SEND_BUTTON_SELECTORS,
1012
- (target) => isSendText(target.label) || /submit/i.test(String(target.attributes?.class || ""))
1013
- );
1014
- const resumeState = await waitForChatResumeModal(client, { timeoutMs: 300 });
1015
- return {
1016
- has_online_resume: Boolean(onlineResume),
1017
- online_resume: onlineResume,
1018
- has_ask_resume: Boolean(askResume),
1019
- ask_resume: askResume,
1020
- already_requested_resume: Boolean(requestedResume),
1021
- requested_resume: requestedResume,
1022
- has_attachment_resume: Boolean(attachmentResume),
1023
- attachment_resume_enabled: Boolean(attachmentResume && !attachmentResume.disabled),
1024
- attachment_resume: attachmentResume,
1025
- editor_visible: Boolean(editor),
1026
- editor,
1027
- send_button_visible: Boolean(sendButton),
1028
- send_button: sendButton,
1029
- resume_modal_open: Boolean(resumeState?.popup || resumeState?.content || resumeState?.resumeIframe),
1030
- panels_closed: !Boolean(resumeState?.popup || resumeState?.content || resumeState?.resumeIframe)
1031
- };
1032
- }
1033
-
1034
- export async function setChatEditorMessage(client, message, {
1035
- timeoutMs = 8000
1036
- } = {}) {
1037
- const started = Date.now();
1038
- let lastState = null;
1039
- while (Date.now() - started <= timeoutMs) {
1040
- const state = await readChatConversationReadyState(client);
1041
- lastState = state;
1042
- if (state.editor?.node_id) {
1043
- try {
1044
- if (state.editor.center) {
1045
- await clickPoint(client, state.editor.center.x, state.editor.center.y);
1046
- } else {
1047
- await clickNodeCenter(client, state.editor.node_id, { scrollIntoView: true });
1048
- }
1049
- await sleep(120);
1050
- await clearFocusedInput(client);
1051
- await sleep(80);
1052
- await insertText(client, message);
1053
- await sleep(250);
1054
- const afterState = await readChatConversationReadyState(client);
1055
- const editorText = normalizeDetailText(afterState.editor?.label || "");
1056
- if (editorText.includes(normalizeDetailText(message))) {
1057
- return {
1058
- ok: true,
1059
- value: editorText,
1060
- editor: afterState.editor || state.editor
1061
- };
1062
- }
1063
- lastState = {
1064
- ...afterState,
1065
- editor_message_mismatch: true,
1066
- editor_text: editorText
1067
- };
1068
- } catch (error) {
1069
- if (!isRecoverableNodeError(error)) throw error;
1070
- lastState = {
1071
- ...state,
1072
- recoverable_error: error?.message || String(error),
1073
- recoverable_phase: "set_editor_message"
1074
- };
1075
- }
1076
- }
1077
- await sleep(250);
1078
- }
1079
- return {
1080
- ok: false,
1081
- error: "CHAT_EDITOR_NOT_FOUND",
1082
- state: lastState
1083
- };
1084
- }
1085
-
1086
- export async function sendChatMessage(client, expectedText = "", {
1087
- timeoutMs = 8000,
1088
- settleMs = 800
1089
- } = {}) {
1090
- const started = Date.now();
1091
- let lastState = null;
1092
- while (Date.now() - started <= timeoutMs) {
1093
- const state = await readChatConversationReadyState(client);
1094
- lastState = state;
1095
- if (state.send_button?.node_id && !state.send_button.disabled) {
1096
- try {
1097
- if (state.send_button.center) {
1098
- await clickPoint(client, state.send_button.center.x, state.send_button.center.y);
1099
- } else {
1100
- await clickNodeCenter(client, state.send_button.node_id, { scrollIntoView: true });
1101
- }
1102
- if (settleMs > 0) await sleep(settleMs);
1103
- return {
1104
- sent: true,
1105
- method: "send-button",
1106
- control: state.send_button,
1107
- expected_text: expectedText
1108
- };
1109
- } catch (error) {
1110
- if (!isRecoverableNodeError(error)) throw error;
1111
- lastState = {
1112
- ...state,
1113
- recoverable_error: error?.message || String(error),
1114
- recoverable_phase: "send_button_click"
1115
- };
1116
- await sleep(250);
1117
- continue;
1118
- }
1119
- }
1120
- if (state.editor?.node_id) {
1121
- await pressKey(client, "Enter", {
1122
- code: "Enter",
1123
- windowsVirtualKeyCode: 13,
1124
- nativeVirtualKeyCode: 13
1125
- });
1126
- if (settleMs > 0) await sleep(settleMs);
1127
- return {
1128
- sent: true,
1129
- method: "enter",
1130
- expected_text: expectedText
1131
- };
1132
- }
1133
- await sleep(250);
1134
- }
1135
- return {
1136
- sent: false,
1137
- method: "none",
1138
- error: "CHAT_SEND_CONTROL_NOT_FOUND",
1139
- state: lastState
1140
- };
1141
- }
1142
-
1143
- export async function clickChatAskResume(client, {
1144
- timeoutMs = 8000,
1145
- settleMs = 700
1146
- } = {}) {
1147
- const started = Date.now();
1148
- let lastState = null;
1149
- let lastDisabledAskResume = null;
1150
- while (Date.now() - started <= timeoutMs) {
1151
- const state = await readChatConversationReadyState(client);
1152
- lastState = state;
1153
- if (state.attachment_resume_enabled) {
1154
- return {
1155
- ok: true,
1156
- already_requested: true,
1157
- attachment_resume_available: true,
1158
- control: state.attachment_resume
1159
- };
1160
- }
1161
- if (state.ask_resume?.node_id && !state.ask_resume.disabled) {
1162
- try {
1163
- if (state.ask_resume.center) {
1164
- await clickPoint(client, state.ask_resume.center.x, state.ask_resume.center.y);
1165
- } else {
1166
- await clickNodeCenter(client, state.ask_resume.node_id, { scrollIntoView: true });
1167
- }
1168
- if (settleMs > 0) await sleep(settleMs);
1169
- return {
1170
- ok: true,
1171
- already_requested: false,
1172
- control: state.ask_resume
1173
- };
1174
- } catch (error) {
1175
- if (!isRecoverableNodeError(error)) throw error;
1176
- lastState = {
1177
- ...state,
1178
- recoverable_error: error?.message || String(error),
1179
- recoverable_phase: "ask_resume_click"
1180
- };
1181
- }
1182
- }
1183
- if (state.ask_resume?.node_id && state.ask_resume.disabled) {
1184
- lastDisabledAskResume = state.ask_resume;
1185
- }
1186
- if (state.already_requested_resume) {
1187
- return {
1188
- ok: true,
1189
- already_requested: true,
1190
- control: state.requested_resume
1191
- };
1192
- }
1193
- await sleep(250);
1194
- }
1195
- if (lastDisabledAskResume) {
1196
- return {
1197
- ok: false,
1198
- already_requested: true,
1199
- request_pending: true,
1200
- error: "ASK_RESUME_BUTTON_DISABLED",
1201
- control: lastDisabledAskResume,
1202
- state: lastState
1203
- };
1204
- }
1205
- return {
1206
- ok: false,
1207
- error: "ASK_RESUME_BUTTON_NOT_FOUND",
1208
- state: lastState
1209
- };
1210
- }
1211
-
1212
- export async function clickChatConfirmRequestResume(client, {
1213
- timeoutMs = 8000,
1214
- settleMs = 900
1215
- } = {}) {
1216
- const started = Date.now();
1217
- let lastTarget = null;
1218
- let lastState = null;
1219
- while (Date.now() - started <= timeoutMs) {
1220
- lastState = await readChatConversationReadyState(client);
1221
- const rootState = await getChatRoots(client);
1222
- const confirmRoots = await resolveScopedRoots(
1223
- client,
1224
- rootState.roots,
1225
- CHAT_CONVERSATION_CONTROL_SCOPE_SELECTORS
1226
- );
1227
- const target = await findVisibleMatchingTarget(
1228
- client,
1229
- confirmRoots,
1230
- CHAT_CONFIRM_REQUEST_RESUME_SELECTORS,
1231
- (item) => isConfirmText(item.label) && !item.disabled
1232
- );
1233
- lastTarget = target;
1234
- if (target?.node_id) {
1235
- try {
1236
- if (target.center) {
1237
- await clickPoint(client, target.center.x, target.center.y);
1238
- } else {
1239
- await clickNodeCenter(client, target.node_id, { scrollIntoView: true });
1240
- }
1241
- if (settleMs > 0) await sleep(settleMs);
1242
- const afterState = await readChatConversationReadyState(client);
1243
- return {
1244
- confirmed: true,
1245
- assumed_requested: Boolean(afterState.already_requested_resume),
1246
- control: target,
1247
- state: afterState
1248
- };
1249
- } catch (error) {
1250
- if (!isRecoverableNodeError(error)) throw error;
1251
- lastTarget = {
1252
- ...target,
1253
- recoverable_error: error?.message || String(error),
1254
- recoverable_phase: "confirm_request_resume_click"
1255
- };
1256
- }
1257
- }
1258
- await sleep(250);
1259
- }
1260
- return {
1261
- confirmed: false,
1262
- error: "CONFIRM_BUTTON_NOT_FOUND",
1263
- control: lastTarget,
1264
- state: lastState
1265
- };
1266
- }
1267
-
1268
- export async function getChatResumeRequestMessageState(client) {
1269
- const rootState = await getChatRoots(client);
1270
- let messageRoot = null;
1271
- for (const root of rootState.roots) {
1272
- for (const selector of CHAT_MESSAGE_LIST_SELECTORS) {
1273
- const nodeIds = await querySelectorAll(client, root.nodeId, selector);
1274
- if (nodeIds.length) {
1275
- messageRoot = {
1276
- root,
1277
- selector,
1278
- node_id: nodeIds[0]
1279
- };
1280
- break;
1281
- }
1282
- }
1283
- if (messageRoot) break;
1284
- }
1285
- const nodeId = messageRoot?.node_id || rootState.rootNodes.top;
1286
- let text = "";
1287
- try {
1288
- text = htmlToText(await getOuterHTML(client, nodeId));
1289
- } catch {}
1290
- const lines = text.split(/\r?\n/).map(normalizeDetailText).filter(Boolean);
1291
- const matching = lines.filter((line) => isResumeRequestSentMessageText(line));
1292
- const attachmentMatching = lines.filter((line) => isResumeAttachmentMessageText(line));
1293
- const count = countResumeRequestSentMessageMarkers(lines);
1294
- const resumeAttachmentCount = countResumeAttachmentMessageMarkers(lines);
1295
- return {
1296
- ok: Boolean(text),
1297
- selector: messageRoot?.selector || "top",
1298
- count,
1299
- resume_attachment_count: resumeAttachmentCount,
1300
- success_count: count + resumeAttachmentCount,
1301
- last_text: matching[matching.length - 1] || lines[lines.length - 1] || "",
1302
- last_resume_attachment_text: attachmentMatching[attachmentMatching.length - 1] || "",
1303
- last_success_text: matching[matching.length - 1] || attachmentMatching[attachmentMatching.length - 1] || "",
1304
- recent: lines.slice(-12)
1305
- };
1306
- }
1307
-
1308
- export async function waitForChatResumeRequestMessage(client, {
1309
- baselineCount = 0,
1310
- baselineResumeAttachmentCount = 0,
1311
- timeoutMs = 6500,
1312
- intervalMs = 260
1313
- } = {}) {
1314
- const started = Date.now();
1315
- let state = null;
1316
- while (Date.now() - started <= timeoutMs) {
1317
- state = await getChatResumeRequestMessageState(client);
1318
- const observed = (
1319
- state.count > baselineCount
1320
- || state.resume_attachment_count > baselineResumeAttachmentCount
1321
- );
1322
- if (observed) {
1323
- return {
1324
- observed: true,
1325
- elapsed_ms: Date.now() - started,
1326
- state
1327
- };
1328
- }
1329
- await sleep(intervalMs);
1330
- }
1331
- return {
1332
- observed: false,
1333
- elapsed_ms: Date.now() - started,
1334
- state
1335
- };
1336
- }
1337
-
1338
- export async function requestChatResumeForPassedCandidate(client, {
1339
- greetingText = "Hi同学,能麻烦发下简历吗?",
1340
- maxAttempts = 3,
1341
- askResumeTimeoutMs = 8000,
1342
- dryRun = false
1343
- } = {}) {
1344
- const effectiveGreetingText = normalizeDetailText(greetingText) || "Hi同学,能麻烦发下简历吗?";
1345
- const initialState = await readChatConversationReadyState(client);
1346
- if (initialState.attachment_resume_enabled) {
1347
- return {
1348
- requested: false,
1349
- skipped: true,
1350
- reason: "attachment_resume_already_available",
1351
- initial_state: initialState
1352
- };
1353
- }
1354
- if (dryRun) {
1355
- return {
1356
- requested: false,
1357
- skipped: false,
1358
- reason: "dry_run",
1359
- initial_state: initialState,
1360
- would_send_greeting: true,
1361
- would_click_ask_resume: true
1362
- };
1363
- }
1364
-
1365
- const closeBeforeGreeting = await closeChatResumeModal(client, { attemptsLimit: 3 });
1366
- if (!closeBeforeGreeting.closed) {
1367
- return {
1368
- requested: false,
1369
- skipped: true,
1370
- reason: "resume_modal_close_failed_before_request",
1371
- initial_state: initialState,
1372
- close_before_greeting: closeBeforeGreeting
1373
- };
1374
- }
1375
- const editorState = await setChatEditorMessage(client, effectiveGreetingText);
1376
- if (!editorState.ok) {
1377
- throw new Error("CHAT_EDITOR_MESSAGE_MISMATCH");
1378
- }
1379
- const sendResult = await sendChatMessage(client, effectiveGreetingText);
1380
- if (!sendResult.sent) {
1381
- throw new Error(`CHAT_GREETING_SEND_FAILED:${sendResult.error || sendResult.method || "unknown"}`);
1382
- }
1383
-
1384
- const attempts = [];
1385
- for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
1386
- const before = await getChatResumeRequestMessageState(client);
1387
- const askResult = await clickChatAskResume(client, {
1388
- timeoutMs: askResumeTimeoutMs
1389
- });
1390
- let confirmResult = {
1391
- confirmed: false,
1392
- assumed_requested: Boolean(askResult.already_requested),
1393
- skipped: true,
1394
- reason: askResult.attachment_resume_available
1395
- ? "attachment_resume_already_available"
1396
- : askResult.request_pending
1397
- ? "resume_request_already_pending"
1398
- : askResult.ok
1399
- ? "already_requested"
1400
- : (askResult.error || "ask_resume_not_clicked")
1401
- };
1402
- if (askResult.attachment_resume_available) {
1403
- attempts.push({
1404
- attempt: attempt + 1,
1405
- ask_result: askResult,
1406
- confirm_result: confirmResult,
1407
- message_before_count: before.count,
1408
- message_after_count: before.count,
1409
- message_observed: false,
1410
- message_last_text: before.last_text || ""
1411
- });
1412
- return {
1413
- requested: false,
1414
- skipped: true,
1415
- reason: "attachment_resume_already_available",
1416
- initial_state: initialState,
1417
- close_before_greeting: closeBeforeGreeting,
1418
- greeting_sent: true,
1419
- greeting_send_result: sendResult,
1420
- attempts
1421
- };
1422
- }
1423
- if (askResult.request_pending || askResult.already_requested) {
1424
- attempts.push({
1425
- attempt: attempt + 1,
1426
- ask_result: askResult,
1427
- confirm_result: confirmResult,
1428
- message_before_count: before.count,
1429
- message_after_count: before.count,
1430
- resume_attachment_before_count: before.resume_attachment_count || 0,
1431
- resume_attachment_after_count: before.resume_attachment_count || 0,
1432
- message_observed: false,
1433
- message_last_text: before.last_success_text || before.last_text || ""
1434
- });
1435
- return {
1436
- requested: false,
1437
- skipped: true,
1438
- reason: "resume_request_already_pending",
1439
- initial_state: initialState,
1440
- close_before_greeting: closeBeforeGreeting,
1441
- greeting_sent: true,
1442
- greeting_send_result: sendResult,
1443
- attempts
1444
- };
1445
- }
1446
- if (askResult.ok && !askResult.already_requested) {
1447
- confirmResult = await clickChatConfirmRequestResume(client);
1448
- }
1449
- const messageCheck = await waitForChatResumeRequestMessage(client, {
1450
- baselineCount: before.count,
1451
- baselineResumeAttachmentCount: before.resume_attachment_count
1452
- });
1453
- const messageObserved = Boolean(messageCheck.observed);
1454
- attempts.push({
1455
- attempt: attempt + 1,
1456
- ask_result: askResult,
1457
- confirm_result: confirmResult,
1458
- message_before_count: before.count,
1459
- message_after_count: messageCheck.state?.count || 0,
1460
- resume_attachment_before_count: before.resume_attachment_count || 0,
1461
- resume_attachment_after_count: messageCheck.state?.resume_attachment_count || 0,
1462
- message_observed: messageObserved,
1463
- message_last_text: messageCheck.state?.last_success_text || messageCheck.state?.last_text || ""
1464
- });
1465
- if (messageObserved) {
1466
- return {
1467
- requested: true,
1468
- skipped: false,
1469
- reason: "requested",
1470
- initial_state: initialState,
1471
- close_before_greeting: closeBeforeGreeting,
1472
- greeting_sent: true,
1473
- greeting_send_result: sendResult,
1474
- attempts
1475
- };
1476
- }
1477
- await sleep(900);
1478
- }
1479
-
1480
- return {
1481
- requested: false,
1482
- skipped: false,
1483
- reason: "resume_request_message_not_observed",
1484
- initial_state: initialState,
1485
- close_before_greeting: closeBeforeGreeting,
1486
- greeting_sent: true,
1487
- greeting_send_result: sendResult,
1488
- attempts
1489
- };
1490
- }
1491
-
1492
- export async function closeChatResumeModal(client, {
1493
- attemptsLimit = 3
1494
- } = {}) {
1495
- const attempts = [];
1496
- for (let index = 0; index < attemptsLimit; index += 1) {
1497
- const existingState = await waitForChatResumeModal(client, { timeoutMs: 500 });
1498
- if (!existingState?.popup && !existingState?.content && !existingState?.resumeIframe) {
1499
- return {
1500
- closed: true,
1501
- attempts
1502
- };
1503
- }
1504
-
1505
- const rootState = await getChatRoots(client);
1506
- const closeTarget = await findVisibleTarget(client, rootState.roots, CHAT_RESUME_CLOSE_SELECTORS);
1507
- if (closeTarget) {
1508
- try {
1509
- await clickPoint(client, closeTarget.center.x, closeTarget.center.y, DETERMINISTIC_CLICK_OPTIONS);
1510
- attempts.push({
1511
- mode: "close-selector",
1512
- selector: closeTarget.selector,
1513
- root: closeTarget.root
1514
- });
1515
- } catch (error) {
1516
- attempts.push({
1517
- mode: "close-selector-error",
1518
- selector: closeTarget.selector,
1519
- root: closeTarget.root,
1520
- error: error?.message || String(error)
1521
- });
1522
- }
1523
- await sleep(700);
1524
- } else {
1525
- await pressEscape(client);
1526
- attempts.push({ mode: "Escape" });
1527
- await sleep(700);
1528
- }
1529
-
1530
- let state = await waitForChatResumeModal(client, { timeoutMs: 1000 });
1531
- if (!state?.popup && !state?.content && !state?.resumeIframe) {
1532
- return {
1533
- closed: true,
1534
- attempts
1535
- };
1536
- }
1537
-
1538
- await pressEscape(client);
1539
- attempts.push({ mode: "Escape-fallback" });
1540
- await sleep(700);
1541
-
1542
- state = await waitForChatResumeModal(client, { timeoutMs: 1000 });
1543
- if (!state?.popup && !state?.content && !state?.resumeIframe) {
1544
- return {
1545
- closed: true,
1546
- attempts
1547
- };
1548
- }
1549
- }
1550
-
1551
- return {
1552
- closed: false,
1553
- attempts
1554
- };
1555
- }
1556
-
1557
- export async function extractChatProfileCandidate(client, {
1558
- cardCandidate,
1559
- cardNodeId,
1560
- resumeState,
1561
- resumeHtml: providedResumeHtml = null,
1562
- networkEvents = [],
1563
- targetUrl = "",
1564
- closeResume = true,
1565
- networkParseRetryMs = 1800,
1566
- networkParseIntervalMs = 250
1567
- } = {}) {
1568
- let resumeHtml = providedResumeHtml || null;
1569
- if (!resumeHtml) {
1570
- try {
1571
- resumeHtml = await readChatResumeHtml(client, resumeState);
1572
- } catch (error) {
1573
- if (!networkEvents.length) throw error;
1574
- resumeHtml = emptyChatResumeHtml(error);
1575
- }
1576
- }
1577
- const detailText = [
1578
- resumeHtml.popupText,
1579
- resumeHtml.contentText,
1580
- resumeHtml.resumeIframeText
1581
- ].filter(Boolean).join("\n\n");
1582
-
1583
- const parseStarted = Date.now();
1584
- let networkBodies = [];
1585
- let detailCandidateResult = null;
1586
- do {
1587
- networkBodies = await readChatProfileNetworkBodies(client, networkEvents);
1588
- detailCandidateResult = buildScreeningCandidateFromDetail({
1589
- domain: "chat",
1590
- source: "chat-live-cdp-profile",
1591
- cardCandidate,
1592
- detailText,
1593
- networkBodies,
1594
- metadata: {
1595
- target_url: targetUrl,
1596
- card_node_id: cardNodeId,
1597
- resume_popup_selector: resumeState?.popup?.selector || null,
1598
- resume_content_selector: resumeState?.content?.selector || null,
1599
- resume_iframe_selector: resumeState?.resumeIframe?.selector || null,
1600
- resume_iframe_document_node_id: resumeHtml.resumeIframeDocumentNodeId
1601
- }
1602
- });
1603
- if (detailCandidateResult.parsed_network_profiles.some((item) => item.ok)) break;
1604
- if (Date.now() - parseStarted >= Math.max(0, Number(networkParseRetryMs) || 0)) break;
1605
- await sleep(Math.max(50, Number(networkParseIntervalMs) || 250));
1606
- } while (true);
1607
-
1608
- let closeResult = null;
1609
- if (closeResume) {
1610
- closeResult = await closeChatResumeModal(client);
1611
- }
1612
-
1613
- return {
1614
- candidate: detailCandidateResult.candidate,
1615
- parsed_network_profiles: detailCandidateResult.parsed_network_profiles,
1616
- network_bodies: networkBodies,
1617
- network_parse_retry_elapsed_ms: Date.now() - parseStarted,
1618
- network_event_count: networkEvents.length,
1619
- detail: {
1620
- popup_text: resumeHtml.popupText,
1621
- content_text: resumeHtml.contentText,
1622
- resume_iframe_text: resumeHtml.resumeIframeText,
1623
- popup_html_length: resumeHtml.popupHTML.length,
1624
- content_html_length: resumeHtml.contentHTML.length,
1625
- resume_iframe_html_length: resumeHtml.resumeIframeHTML.length
1626
- },
1627
- resume_html_read_error: resumeHtml.readError || null,
1628
- close_result: closeResult
1629
- };
1630
- }
1631
-
1632
- async function findVisibleTarget(client, roots, selectors) {
1633
- let fallback = null;
1634
- for (const root of roots) {
1635
- if (!root?.nodeId) continue;
1636
- for (const selector of selectors) {
1637
- const nodeIds = await querySelectorAll(client, root.nodeId, selector);
1638
- for (const nodeId of nodeIds) {
1639
- const target = {
1640
- root: root.name,
1641
- root_node_id: root.nodeId,
1642
- selector,
1643
- node_id: nodeId
1644
- };
1645
- if (!fallback) fallback = target;
1646
- try {
1647
- const box = await getNodeBox(client, nodeId);
1648
- if (box.rect.width > 2 && box.rect.height > 2) {
1649
- return {
1650
- ...target,
1651
- center: box.center,
1652
- rect: box.rect
1653
- };
1654
- }
1655
- } catch {}
1656
- }
1657
- }
1658
- }
1659
- return fallback;
1660
- }
1661
-
1662
- async function pressEscape(client) {
1663
- await pressKey(client, "Escape", {
1664
- code: "Escape",
1665
- windowsVirtualKeyCode: 27,
1666
- nativeVirtualKeyCode: 27
1667
- });
1668
- }
765
+ let popupHTML = "";
766
+ let contentHTML = "";
767
+ let resumeIframeHTML = "";
768
+ let resumeIframeDocumentNodeId = null;
769
+
770
+ if (resumeState?.popup?.node_id) {
771
+ popupHTML = await getOuterHTML(client, resumeState.popup.node_id);
772
+ }
773
+
774
+ if (resumeState?.content?.node_id && resumeState.content.node_id !== resumeState?.popup?.node_id) {
775
+ contentHTML = await getOuterHTML(client, resumeState.content.node_id);
776
+ }
777
+
778
+ if (resumeState?.resumeIframe?.node_id) {
779
+ resumeIframeDocumentNodeId = await getFrameDocumentNodeId(client, resumeState.resumeIframe.node_id);
780
+ resumeIframeHTML = await getOuterHTML(client, resumeIframeDocumentNodeId);
781
+ }
782
+
783
+ return {
784
+ popupHTML,
785
+ contentHTML,
786
+ resumeIframeHTML,
787
+ resumeIframeDocumentNodeId,
788
+ popupText: htmlToText(popupHTML),
789
+ contentText: htmlToText(contentHTML),
790
+ resumeIframeText: htmlToText(resumeIframeHTML)
791
+ };
792
+ }
793
+
794
+ function emptyChatResumeHtml(readError = null) {
795
+ return {
796
+ popupHTML: "",
797
+ contentHTML: "",
798
+ resumeIframeHTML: "",
799
+ resumeIframeDocumentNodeId: null,
800
+ popupText: "",
801
+ contentText: "",
802
+ resumeIframeText: "",
803
+ readError: readError?.message || null
804
+ };
805
+ }
806
+
807
+ export async function waitForChatResumeContent(client, {
808
+ minTextLength = 120,
809
+ timeoutMs = 15000,
810
+ intervalMs = 250
811
+ } = {}) {
812
+ const started = Date.now();
813
+ let lastState = null;
814
+ let lastHtml = null;
815
+ let lastError = null;
816
+ while (Date.now() - started <= timeoutMs) {
817
+ try {
818
+ lastState = await waitForChatResumeModal(client, {
819
+ timeoutMs: 700,
820
+ intervalMs: 100
821
+ });
822
+ if (lastState?.popup || lastState?.content || lastState?.resumeIframe) {
823
+ lastHtml = await readChatResumeHtml(client, lastState);
824
+ const textLength = [
825
+ lastHtml.popupText,
826
+ lastHtml.contentText,
827
+ lastHtml.resumeIframeText
828
+ ].join("\n").length;
829
+ if (textLength >= minTextLength) {
830
+ return {
831
+ ok: true,
832
+ elapsed_ms: Date.now() - started,
833
+ text_length: textLength,
834
+ resume_state: lastState,
835
+ resume_html: lastHtml
836
+ };
837
+ }
838
+ }
839
+ } catch (error) {
840
+ lastError = error;
841
+ }
842
+ await sleep(intervalMs);
843
+ }
844
+
845
+ const textLength = [
846
+ lastHtml?.popupText,
847
+ lastHtml?.contentText,
848
+ lastHtml?.resumeIframeText
849
+ ].filter(Boolean).join("\n").length;
850
+ return {
851
+ ok: false,
852
+ elapsed_ms: Date.now() - started,
853
+ text_length: textLength,
854
+ resume_state: lastState,
855
+ resume_html: lastHtml,
856
+ error: lastError?.message || null
857
+ };
858
+ }
859
+
860
+ export async function openChatOnlineResume(client, {
861
+ timeoutMs = 15000,
862
+ attemptsLimit = 3,
863
+ settleMs = 1200
864
+ } = {}) {
865
+ const attempts = [];
866
+ for (let index = 0; index < attemptsLimit; index += 1) {
867
+ if (settleMs > 0) await sleep(settleMs);
868
+ await assertChatShellNotResumeTopLevel(client, {
869
+ context: "openChatOnlineResume:before_existing_modal_check"
870
+ });
871
+ const existingResumeState = await waitForChatResumeModal(client, {
872
+ timeoutMs: 500,
873
+ intervalMs: 100
874
+ });
875
+ if (existingResumeState?.forbidden_top_level_navigation) {
876
+ throw makeForbiddenChatResumeNavigationError(existingResumeState.top_level_state);
877
+ }
878
+ if (
879
+ existingResumeState?.popup
880
+ || existingResumeState?.content
881
+ || existingResumeState?.resumeIframe
882
+ ) {
883
+ attempts.push({
884
+ attempt: index + 1,
885
+ ok: true,
886
+ reused_existing_modal: true,
887
+ resume_popup_selector: existingResumeState?.popup?.selector || null,
888
+ resume_content_selector: existingResumeState?.content?.selector || null,
889
+ resume_iframe_selector: existingResumeState?.resumeIframe?.selector || null
890
+ });
891
+ return {
892
+ button: null,
893
+ button_html: "",
894
+ resume_state: existingResumeState,
895
+ attempts
896
+ };
897
+ }
898
+
899
+ const buttonState = await waitForChatOnlineResumeButton(client, {
900
+ timeoutMs: Math.min(timeoutMs, 8000)
901
+ });
902
+ if (!buttonState?.target?.node_id) {
903
+ attempts.push({
904
+ attempt: index + 1,
905
+ ok: false,
906
+ error: "ONLINE_RESUME_BUTTON_NOT_FOUND"
907
+ });
908
+ continue;
909
+ }
910
+
911
+ let buttonHTML = "";
912
+ try {
913
+ buttonHTML = await getOuterHTML(client, buttonState.target.node_id);
914
+ } catch {}
915
+
916
+ if (isUnsafeChatOnlineResumeTarget(buttonState.target, buttonHTML)) {
917
+ const error = makeUnsafeChatOnlineResumeLinkError(buttonState.target, buttonHTML);
918
+ attempts.push({
919
+ attempt: index + 1,
920
+ ok: false,
921
+ error: error.code,
922
+ blocked_pre_click: true,
923
+ button_selector: buttonState.target.selector,
924
+ button_text: error.button_text,
925
+ button_href: error.href,
926
+ button_html_length: buttonHTML.length
927
+ });
928
+ error.attempts = attempts;
929
+ throw error;
930
+ }
931
+
932
+ try {
933
+ if (buttonState.target.center) {
934
+ await clickPoint(client, buttonState.target.center.x, buttonState.target.center.y);
935
+ } else {
936
+ await clickNodeCenter(client, buttonState.target.node_id, {
937
+ scrollIntoView: true
938
+ });
939
+ }
940
+ } catch (error) {
941
+ attempts.push({
942
+ attempt: index + 1,
943
+ ok: false,
944
+ error: error?.message || String(error),
945
+ recoverable_stale_node: isRecoverableNodeError(error),
946
+ button_selector: buttonState.target.selector,
947
+ button_text: htmlToText(buttonHTML).slice(0, 120),
948
+ button_html_length: buttonHTML.length
949
+ });
950
+ if (isRecoverableNodeError(error)) {
951
+ await sleep(350);
952
+ continue;
953
+ }
954
+ throw error;
955
+ }
956
+ await assertChatShellNotResumeTopLevel(client, {
957
+ context: "openChatOnlineResume:after_online_resume_click"
958
+ });
959
+ const resumeState = await waitForChatResumeModal(client, {
960
+ timeoutMs: Math.max(2500, Math.floor(timeoutMs / attemptsLimit))
961
+ });
962
+ if (resumeState?.forbidden_top_level_navigation) {
963
+ throw makeForbiddenChatResumeNavigationError(resumeState.top_level_state);
964
+ }
965
+ attempts.push({
966
+ attempt: index + 1,
967
+ ok: Boolean(resumeState?.popup || resumeState?.content || resumeState?.resumeIframe),
968
+ button_selector: buttonState.target.selector,
969
+ button_text: htmlToText(buttonHTML).slice(0, 120),
970
+ button_html_length: buttonHTML.length,
971
+ resume_popup_selector: resumeState?.popup?.selector || null,
972
+ resume_content_selector: resumeState?.content?.selector || null,
973
+ resume_iframe_selector: resumeState?.resumeIframe?.selector || null
974
+ });
975
+ if (resumeState?.popup || resumeState?.content || resumeState?.resumeIframe) {
976
+ return {
977
+ button: buttonState.target,
978
+ button_html: buttonHTML,
979
+ resume_state: resumeState,
980
+ attempts
981
+ };
982
+ }
983
+ }
984
+
985
+ const error = new Error("Chat online resume modal did not open");
986
+ error.attempts = attempts;
987
+ throw error;
988
+ }
989
+
990
+ export async function readChatConversationReadyState(client) {
991
+ const rootState = await getChatRoots(client);
992
+ const scopedControlRoots = await resolveScopedRoots(
993
+ client,
994
+ rootState.roots,
995
+ CHAT_CONVERSATION_CONTROL_SCOPE_SELECTORS,
996
+ { fallbackToRoots: false }
997
+ );
998
+ const scopedRequestedRoots = await resolveScopedRoots(
999
+ client,
1000
+ rootState.roots,
1001
+ CHAT_REQUESTED_RESUME_SCOPE_SELECTORS,
1002
+ { fallbackToRoots: false }
1003
+ );
1004
+ const controlRoots = scopedControlRoots.length ? scopedControlRoots : rootState.roots;
1005
+ const requestedRoots = scopedRequestedRoots.length ? scopedRequestedRoots : rootState.roots;
1006
+ const onlineResume = await findVisibleMatchingTarget(
1007
+ client,
1008
+ controlRoots,
1009
+ CHAT_ONLINE_RESUME_BUTTON_SELECTORS,
1010
+ (target) => target.label.includes("在线简历") && !target.disabled
1011
+ );
1012
+ const attachmentResume = await findVisibleMatchingTarget(
1013
+ client,
1014
+ controlRoots,
1015
+ CHAT_ATTACHMENT_RESUME_BUTTON_SELECTORS,
1016
+ (target) => isAttachmentResumeText(target.label)
1017
+ );
1018
+ const askResume = await findVisibleMatchingTarget(
1019
+ client,
1020
+ controlRoots,
1021
+ CHAT_ASK_RESUME_BUTTON_SELECTORS,
1022
+ (target) => isAskResumeText(target.label) && !isAttachmentResumeTarget(target)
1023
+ );
1024
+ const requestedResume = await findVisibleMatchingTarget(
1025
+ client,
1026
+ requestedRoots,
1027
+ CHAT_ASK_RESUME_BUTTON_SELECTORS,
1028
+ (target) => isRequestedResumeControlTarget(target)
1029
+ );
1030
+ const editor = await findVisibleMatchingTarget(
1031
+ client,
1032
+ controlRoots,
1033
+ CHAT_EDITOR_SELECTORS,
1034
+ () => true
1035
+ );
1036
+ const sendButton = await findVisibleMatchingTarget(
1037
+ client,
1038
+ controlRoots,
1039
+ CHAT_SEND_BUTTON_SELECTORS,
1040
+ (target) => isSendText(target.label) || /submit/i.test(String(target.attributes?.class || ""))
1041
+ );
1042
+ const resumeState = await waitForChatResumeModal(client, { timeoutMs: 300 });
1043
+ const blockingPanel = await findChatBlockingPanel(client);
1044
+ const resumeModalOpen = Boolean(resumeState?.popup || resumeState?.content || resumeState?.resumeIframe);
1045
+ const blockingPanelOpen = Boolean(blockingPanel?.open);
1046
+ return {
1047
+ has_online_resume: Boolean(onlineResume),
1048
+ online_resume: onlineResume,
1049
+ has_ask_resume: Boolean(askResume),
1050
+ ask_resume: askResume,
1051
+ already_requested_resume: Boolean(requestedResume),
1052
+ requested_resume: requestedResume,
1053
+ has_attachment_resume: Boolean(attachmentResume),
1054
+ attachment_resume_enabled: Boolean(attachmentResume && !attachmentResume.disabled),
1055
+ attachment_resume: attachmentResume,
1056
+ editor_visible: Boolean(editor),
1057
+ editor,
1058
+ send_button_visible: Boolean(sendButton),
1059
+ send_button: sendButton,
1060
+ resume_modal_open: resumeModalOpen,
1061
+ blocking_panel_open: blockingPanelOpen,
1062
+ blocking_panel: blockingPanelOpen ? blockingPanel : null,
1063
+ panels_closed: !resumeModalOpen && !blockingPanelOpen
1064
+ };
1065
+ }
1066
+
1067
+ export async function setChatEditorMessage(client, message, {
1068
+ timeoutMs = 8000
1069
+ } = {}) {
1070
+ const started = Date.now();
1071
+ let lastState = null;
1072
+ while (Date.now() - started <= timeoutMs) {
1073
+ const state = await readChatConversationReadyState(client);
1074
+ lastState = state;
1075
+ if (state.editor?.node_id) {
1076
+ try {
1077
+ if (state.editor.center) {
1078
+ await clickPoint(client, state.editor.center.x, state.editor.center.y);
1079
+ } else {
1080
+ await clickNodeCenter(client, state.editor.node_id, { scrollIntoView: true });
1081
+ }
1082
+ await sleep(120);
1083
+ await clearFocusedInput(client);
1084
+ await sleep(80);
1085
+ await insertText(client, message);
1086
+ await sleep(250);
1087
+ const afterState = await readChatConversationReadyState(client);
1088
+ const editorText = normalizeDetailText(afterState.editor?.label || "");
1089
+ if (editorText.includes(normalizeDetailText(message))) {
1090
+ return {
1091
+ ok: true,
1092
+ value: editorText,
1093
+ editor: afterState.editor || state.editor
1094
+ };
1095
+ }
1096
+ lastState = {
1097
+ ...afterState,
1098
+ editor_message_mismatch: true,
1099
+ editor_text: editorText
1100
+ };
1101
+ } catch (error) {
1102
+ if (!isRecoverableNodeError(error)) throw error;
1103
+ lastState = {
1104
+ ...state,
1105
+ recoverable_error: error?.message || String(error),
1106
+ recoverable_phase: "set_editor_message"
1107
+ };
1108
+ }
1109
+ }
1110
+ await sleep(250);
1111
+ }
1112
+ return {
1113
+ ok: false,
1114
+ error: "CHAT_EDITOR_NOT_FOUND",
1115
+ state: lastState
1116
+ };
1117
+ }
1118
+
1119
+ export async function sendChatMessage(client, expectedText = "", {
1120
+ timeoutMs = 8000,
1121
+ settleMs = 800
1122
+ } = {}) {
1123
+ const started = Date.now();
1124
+ let lastState = null;
1125
+ while (Date.now() - started <= timeoutMs) {
1126
+ const state = await readChatConversationReadyState(client);
1127
+ lastState = state;
1128
+ if (state.send_button?.node_id && !state.send_button.disabled) {
1129
+ try {
1130
+ if (state.send_button.center) {
1131
+ await clickPoint(client, state.send_button.center.x, state.send_button.center.y);
1132
+ } else {
1133
+ await clickNodeCenter(client, state.send_button.node_id, { scrollIntoView: true });
1134
+ }
1135
+ if (settleMs > 0) await sleep(settleMs);
1136
+ return {
1137
+ sent: true,
1138
+ method: "send-button",
1139
+ control: state.send_button,
1140
+ expected_text: expectedText
1141
+ };
1142
+ } catch (error) {
1143
+ if (!isRecoverableNodeError(error)) throw error;
1144
+ lastState = {
1145
+ ...state,
1146
+ recoverable_error: error?.message || String(error),
1147
+ recoverable_phase: "send_button_click"
1148
+ };
1149
+ await sleep(250);
1150
+ continue;
1151
+ }
1152
+ }
1153
+ if (state.editor?.node_id) {
1154
+ await pressKey(client, "Enter", {
1155
+ code: "Enter",
1156
+ windowsVirtualKeyCode: 13,
1157
+ nativeVirtualKeyCode: 13
1158
+ });
1159
+ if (settleMs > 0) await sleep(settleMs);
1160
+ return {
1161
+ sent: true,
1162
+ method: "enter",
1163
+ expected_text: expectedText
1164
+ };
1165
+ }
1166
+ await sleep(250);
1167
+ }
1168
+ return {
1169
+ sent: false,
1170
+ method: "none",
1171
+ error: "CHAT_SEND_CONTROL_NOT_FOUND",
1172
+ state: lastState
1173
+ };
1174
+ }
1175
+
1176
+ export async function clickChatAskResume(client, {
1177
+ timeoutMs = 8000,
1178
+ settleMs = 700
1179
+ } = {}) {
1180
+ const started = Date.now();
1181
+ let lastState = null;
1182
+ let lastDisabledAskResume = null;
1183
+ while (Date.now() - started <= timeoutMs) {
1184
+ const state = await readChatConversationReadyState(client);
1185
+ lastState = state;
1186
+ if (state.attachment_resume_enabled) {
1187
+ return {
1188
+ ok: true,
1189
+ already_requested: true,
1190
+ attachment_resume_available: true,
1191
+ control: state.attachment_resume
1192
+ };
1193
+ }
1194
+ if (state.ask_resume?.node_id && !state.ask_resume.disabled) {
1195
+ try {
1196
+ if (state.ask_resume.center) {
1197
+ await clickPoint(client, state.ask_resume.center.x, state.ask_resume.center.y);
1198
+ } else {
1199
+ await clickNodeCenter(client, state.ask_resume.node_id, { scrollIntoView: true });
1200
+ }
1201
+ if (settleMs > 0) await sleep(settleMs);
1202
+ return {
1203
+ ok: true,
1204
+ already_requested: false,
1205
+ control: state.ask_resume
1206
+ };
1207
+ } catch (error) {
1208
+ if (!isRecoverableNodeError(error)) throw error;
1209
+ lastState = {
1210
+ ...state,
1211
+ recoverable_error: error?.message || String(error),
1212
+ recoverable_phase: "ask_resume_click"
1213
+ };
1214
+ }
1215
+ }
1216
+ if (state.ask_resume?.node_id && state.ask_resume.disabled) {
1217
+ lastDisabledAskResume = state.ask_resume;
1218
+ }
1219
+ if (state.already_requested_resume) {
1220
+ return {
1221
+ ok: true,
1222
+ already_requested: true,
1223
+ control: state.requested_resume
1224
+ };
1225
+ }
1226
+ await sleep(250);
1227
+ }
1228
+ if (lastDisabledAskResume) {
1229
+ return {
1230
+ ok: false,
1231
+ already_requested: true,
1232
+ request_pending: true,
1233
+ error: "ASK_RESUME_BUTTON_DISABLED",
1234
+ control: lastDisabledAskResume,
1235
+ state: lastState
1236
+ };
1237
+ }
1238
+ return {
1239
+ ok: false,
1240
+ error: "ASK_RESUME_BUTTON_NOT_FOUND",
1241
+ state: lastState
1242
+ };
1243
+ }
1244
+
1245
+ export async function clickChatConfirmRequestResume(client, {
1246
+ timeoutMs = 8000,
1247
+ settleMs = 900
1248
+ } = {}) {
1249
+ const started = Date.now();
1250
+ let lastTarget = null;
1251
+ let lastState = null;
1252
+ while (Date.now() - started <= timeoutMs) {
1253
+ lastState = await readChatConversationReadyState(client);
1254
+ const rootState = await getChatRoots(client);
1255
+ const confirmRoots = await resolveScopedRoots(
1256
+ client,
1257
+ rootState.roots,
1258
+ CHAT_CONVERSATION_CONTROL_SCOPE_SELECTORS
1259
+ );
1260
+ const target = await findVisibleMatchingTarget(
1261
+ client,
1262
+ confirmRoots,
1263
+ CHAT_CONFIRM_REQUEST_RESUME_SELECTORS,
1264
+ (item) => isConfirmText(item.label) && !item.disabled
1265
+ );
1266
+ lastTarget = target;
1267
+ if (target?.node_id) {
1268
+ try {
1269
+ if (target.center) {
1270
+ await clickPoint(client, target.center.x, target.center.y);
1271
+ } else {
1272
+ await clickNodeCenter(client, target.node_id, { scrollIntoView: true });
1273
+ }
1274
+ if (settleMs > 0) await sleep(settleMs);
1275
+ const afterState = await readChatConversationReadyState(client);
1276
+ return {
1277
+ confirmed: true,
1278
+ assumed_requested: Boolean(afterState.already_requested_resume),
1279
+ control: target,
1280
+ state: afterState
1281
+ };
1282
+ } catch (error) {
1283
+ if (!isRecoverableNodeError(error)) throw error;
1284
+ lastTarget = {
1285
+ ...target,
1286
+ recoverable_error: error?.message || String(error),
1287
+ recoverable_phase: "confirm_request_resume_click"
1288
+ };
1289
+ }
1290
+ }
1291
+ await sleep(250);
1292
+ }
1293
+ return {
1294
+ confirmed: false,
1295
+ error: "CONFIRM_BUTTON_NOT_FOUND",
1296
+ control: lastTarget,
1297
+ state: lastState
1298
+ };
1299
+ }
1300
+
1301
+ export async function getChatResumeRequestMessageState(client) {
1302
+ const rootState = await getChatRoots(client);
1303
+ let messageRoot = null;
1304
+ for (const root of rootState.roots) {
1305
+ for (const selector of CHAT_MESSAGE_LIST_SELECTORS) {
1306
+ const nodeIds = await querySelectorAll(client, root.nodeId, selector);
1307
+ if (nodeIds.length) {
1308
+ messageRoot = {
1309
+ root,
1310
+ selector,
1311
+ node_id: nodeIds[0]
1312
+ };
1313
+ break;
1314
+ }
1315
+ }
1316
+ if (messageRoot) break;
1317
+ }
1318
+ const nodeId = messageRoot?.node_id || rootState.rootNodes.top;
1319
+ let text = "";
1320
+ try {
1321
+ text = htmlToText(await getOuterHTML(client, nodeId));
1322
+ } catch {}
1323
+ const lines = text.split(/\r?\n/).map(normalizeDetailText).filter(Boolean);
1324
+ const matching = lines.filter((line) => isResumeRequestSentMessageText(line));
1325
+ const attachmentMatching = lines.filter((line) => isResumeAttachmentMessageText(line));
1326
+ const count = countResumeRequestSentMessageMarkers(lines);
1327
+ const resumeAttachmentCount = countResumeAttachmentMessageMarkers(lines);
1328
+ return {
1329
+ ok: Boolean(text),
1330
+ selector: messageRoot?.selector || "top",
1331
+ count,
1332
+ resume_attachment_count: resumeAttachmentCount,
1333
+ success_count: count + resumeAttachmentCount,
1334
+ last_text: matching[matching.length - 1] || lines[lines.length - 1] || "",
1335
+ last_resume_attachment_text: attachmentMatching[attachmentMatching.length - 1] || "",
1336
+ last_success_text: matching[matching.length - 1] || attachmentMatching[attachmentMatching.length - 1] || "",
1337
+ recent: lines.slice(-12)
1338
+ };
1339
+ }
1340
+
1341
+ export async function waitForChatResumeRequestMessage(client, {
1342
+ baselineCount = 0,
1343
+ baselineResumeAttachmentCount = 0,
1344
+ timeoutMs = 6500,
1345
+ intervalMs = 260
1346
+ } = {}) {
1347
+ const started = Date.now();
1348
+ let state = null;
1349
+ while (Date.now() - started <= timeoutMs) {
1350
+ state = await getChatResumeRequestMessageState(client);
1351
+ const observed = (
1352
+ state.count > baselineCount
1353
+ || state.resume_attachment_count > baselineResumeAttachmentCount
1354
+ );
1355
+ if (observed) {
1356
+ return {
1357
+ observed: true,
1358
+ elapsed_ms: Date.now() - started,
1359
+ state
1360
+ };
1361
+ }
1362
+ await sleep(intervalMs);
1363
+ }
1364
+ return {
1365
+ observed: false,
1366
+ elapsed_ms: Date.now() - started,
1367
+ state
1368
+ };
1369
+ }
1370
+
1371
+ export async function requestChatResumeForPassedCandidate(client, {
1372
+ greetingText = "Hi同学,能麻烦发下简历吗?",
1373
+ maxAttempts = 3,
1374
+ askResumeTimeoutMs = 8000,
1375
+ dryRun = false
1376
+ } = {}) {
1377
+ const effectiveGreetingText = normalizeDetailText(greetingText) || "Hi同学,能麻烦发下简历吗?";
1378
+ const initialState = await readChatConversationReadyState(client);
1379
+ if (initialState.attachment_resume_enabled) {
1380
+ return {
1381
+ requested: false,
1382
+ skipped: true,
1383
+ reason: "attachment_resume_already_available",
1384
+ initial_state: initialState
1385
+ };
1386
+ }
1387
+ if (dryRun) {
1388
+ return {
1389
+ requested: false,
1390
+ skipped: false,
1391
+ reason: "dry_run",
1392
+ initial_state: initialState,
1393
+ would_send_greeting: true,
1394
+ would_click_ask_resume: true
1395
+ };
1396
+ }
1397
+
1398
+ const closeBeforeGreeting = await closeChatResumeModal(client, { attemptsLimit: 3 });
1399
+ if (!closeBeforeGreeting.closed) {
1400
+ return {
1401
+ requested: false,
1402
+ skipped: true,
1403
+ reason: "resume_modal_close_failed_before_request",
1404
+ initial_state: initialState,
1405
+ close_before_greeting: closeBeforeGreeting
1406
+ };
1407
+ }
1408
+ const editorState = await setChatEditorMessage(client, effectiveGreetingText);
1409
+ if (!editorState.ok) {
1410
+ throw new Error("CHAT_EDITOR_MESSAGE_MISMATCH");
1411
+ }
1412
+ const sendResult = await sendChatMessage(client, effectiveGreetingText);
1413
+ if (!sendResult.sent) {
1414
+ throw new Error(`CHAT_GREETING_SEND_FAILED:${sendResult.error || sendResult.method || "unknown"}`);
1415
+ }
1416
+
1417
+ const attempts = [];
1418
+ for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
1419
+ const before = await getChatResumeRequestMessageState(client);
1420
+ const askResult = await clickChatAskResume(client, {
1421
+ timeoutMs: askResumeTimeoutMs
1422
+ });
1423
+ let confirmResult = {
1424
+ confirmed: false,
1425
+ assumed_requested: Boolean(askResult.already_requested),
1426
+ skipped: true,
1427
+ reason: askResult.attachment_resume_available
1428
+ ? "attachment_resume_already_available"
1429
+ : askResult.request_pending
1430
+ ? "resume_request_already_pending"
1431
+ : askResult.ok
1432
+ ? "already_requested"
1433
+ : (askResult.error || "ask_resume_not_clicked")
1434
+ };
1435
+ if (askResult.attachment_resume_available) {
1436
+ attempts.push({
1437
+ attempt: attempt + 1,
1438
+ ask_result: askResult,
1439
+ confirm_result: confirmResult,
1440
+ message_before_count: before.count,
1441
+ message_after_count: before.count,
1442
+ message_observed: false,
1443
+ message_last_text: before.last_text || ""
1444
+ });
1445
+ return {
1446
+ requested: false,
1447
+ skipped: true,
1448
+ reason: "attachment_resume_already_available",
1449
+ initial_state: initialState,
1450
+ close_before_greeting: closeBeforeGreeting,
1451
+ greeting_sent: true,
1452
+ greeting_send_result: sendResult,
1453
+ attempts
1454
+ };
1455
+ }
1456
+ if (askResult.request_pending || askResult.already_requested) {
1457
+ attempts.push({
1458
+ attempt: attempt + 1,
1459
+ ask_result: askResult,
1460
+ confirm_result: confirmResult,
1461
+ message_before_count: before.count,
1462
+ message_after_count: before.count,
1463
+ resume_attachment_before_count: before.resume_attachment_count || 0,
1464
+ resume_attachment_after_count: before.resume_attachment_count || 0,
1465
+ message_observed: false,
1466
+ message_last_text: before.last_success_text || before.last_text || ""
1467
+ });
1468
+ return {
1469
+ requested: false,
1470
+ skipped: true,
1471
+ reason: "resume_request_already_pending",
1472
+ initial_state: initialState,
1473
+ close_before_greeting: closeBeforeGreeting,
1474
+ greeting_sent: true,
1475
+ greeting_send_result: sendResult,
1476
+ attempts
1477
+ };
1478
+ }
1479
+ if (askResult.ok && !askResult.already_requested) {
1480
+ confirmResult = await clickChatConfirmRequestResume(client);
1481
+ }
1482
+ const messageCheck = await waitForChatResumeRequestMessage(client, {
1483
+ baselineCount: before.count,
1484
+ baselineResumeAttachmentCount: before.resume_attachment_count
1485
+ });
1486
+ const messageObserved = Boolean(messageCheck.observed);
1487
+ attempts.push({
1488
+ attempt: attempt + 1,
1489
+ ask_result: askResult,
1490
+ confirm_result: confirmResult,
1491
+ message_before_count: before.count,
1492
+ message_after_count: messageCheck.state?.count || 0,
1493
+ resume_attachment_before_count: before.resume_attachment_count || 0,
1494
+ resume_attachment_after_count: messageCheck.state?.resume_attachment_count || 0,
1495
+ message_observed: messageObserved,
1496
+ message_last_text: messageCheck.state?.last_success_text || messageCheck.state?.last_text || ""
1497
+ });
1498
+ if (messageObserved) {
1499
+ return {
1500
+ requested: true,
1501
+ skipped: false,
1502
+ reason: "requested",
1503
+ initial_state: initialState,
1504
+ close_before_greeting: closeBeforeGreeting,
1505
+ greeting_sent: true,
1506
+ greeting_send_result: sendResult,
1507
+ attempts
1508
+ };
1509
+ }
1510
+ await sleep(900);
1511
+ }
1512
+
1513
+ return {
1514
+ requested: false,
1515
+ skipped: false,
1516
+ reason: "resume_request_message_not_observed",
1517
+ initial_state: initialState,
1518
+ close_before_greeting: closeBeforeGreeting,
1519
+ greeting_sent: true,
1520
+ greeting_send_result: sendResult,
1521
+ attempts
1522
+ };
1523
+ }
1524
+
1525
+ export async function closeChatResumeModal(client, {
1526
+ attemptsLimit = 3
1527
+ } = {}) {
1528
+ const attempts = [];
1529
+ for (let index = 0; index < attemptsLimit; index += 1) {
1530
+ const existingState = await waitForChatResumeModal(client, { timeoutMs: 500 });
1531
+ if (!existingState?.popup && !existingState?.content && !existingState?.resumeIframe) {
1532
+ return {
1533
+ closed: true,
1534
+ attempts
1535
+ };
1536
+ }
1537
+
1538
+ const rootState = await getChatRoots(client);
1539
+ const closeTarget = await findVisibleTarget(client, rootState.roots, CHAT_RESUME_CLOSE_SELECTORS);
1540
+ if (closeTarget) {
1541
+ try {
1542
+ await clickPoint(client, closeTarget.center.x, closeTarget.center.y, DETERMINISTIC_CLICK_OPTIONS);
1543
+ attempts.push({
1544
+ mode: "close-selector",
1545
+ selector: closeTarget.selector,
1546
+ root: closeTarget.root
1547
+ });
1548
+ } catch (error) {
1549
+ attempts.push({
1550
+ mode: "close-selector-error",
1551
+ selector: closeTarget.selector,
1552
+ root: closeTarget.root,
1553
+ error: error?.message || String(error)
1554
+ });
1555
+ }
1556
+ await sleep(700);
1557
+ } else {
1558
+ await pressEscape(client);
1559
+ attempts.push({ mode: "Escape" });
1560
+ await sleep(700);
1561
+ }
1562
+
1563
+ let state = await waitForChatResumeModal(client, { timeoutMs: 1000 });
1564
+ if (!state?.popup && !state?.content && !state?.resumeIframe) {
1565
+ return {
1566
+ closed: true,
1567
+ attempts
1568
+ };
1569
+ }
1570
+
1571
+ await pressEscape(client);
1572
+ attempts.push({ mode: "Escape-fallback" });
1573
+ await sleep(700);
1574
+
1575
+ state = await waitForChatResumeModal(client, { timeoutMs: 1000 });
1576
+ if (!state?.popup && !state?.content && !state?.resumeIframe) {
1577
+ return {
1578
+ closed: true,
1579
+ attempts
1580
+ };
1581
+ }
1582
+ }
1583
+
1584
+ return {
1585
+ closed: false,
1586
+ attempts
1587
+ };
1588
+ }
1589
+
1590
+ export async function extractChatProfileCandidate(client, {
1591
+ cardCandidate,
1592
+ cardNodeId,
1593
+ resumeState,
1594
+ resumeHtml: providedResumeHtml = null,
1595
+ networkEvents = [],
1596
+ targetUrl = "",
1597
+ closeResume = true,
1598
+ networkParseRetryMs = 1800,
1599
+ networkParseIntervalMs = 250
1600
+ } = {}) {
1601
+ let resumeHtml = providedResumeHtml || null;
1602
+ if (!resumeHtml) {
1603
+ try {
1604
+ resumeHtml = await readChatResumeHtml(client, resumeState);
1605
+ } catch (error) {
1606
+ if (!networkEvents.length) throw error;
1607
+ resumeHtml = emptyChatResumeHtml(error);
1608
+ }
1609
+ }
1610
+ const detailText = [
1611
+ resumeHtml.popupText,
1612
+ resumeHtml.contentText,
1613
+ resumeHtml.resumeIframeText
1614
+ ].filter(Boolean).join("\n\n");
1615
+
1616
+ const parseStarted = Date.now();
1617
+ let networkBodies = [];
1618
+ let detailCandidateResult = null;
1619
+ do {
1620
+ networkBodies = await readChatProfileNetworkBodies(client, networkEvents);
1621
+ detailCandidateResult = buildScreeningCandidateFromDetail({
1622
+ domain: "chat",
1623
+ source: "chat-live-cdp-profile",
1624
+ cardCandidate,
1625
+ detailText,
1626
+ networkBodies,
1627
+ metadata: {
1628
+ target_url: targetUrl,
1629
+ card_node_id: cardNodeId,
1630
+ resume_popup_selector: resumeState?.popup?.selector || null,
1631
+ resume_content_selector: resumeState?.content?.selector || null,
1632
+ resume_iframe_selector: resumeState?.resumeIframe?.selector || null,
1633
+ resume_iframe_document_node_id: resumeHtml.resumeIframeDocumentNodeId
1634
+ }
1635
+ });
1636
+ if (detailCandidateResult.parsed_network_profiles.some((item) => item.ok)) break;
1637
+ if (Date.now() - parseStarted >= Math.max(0, Number(networkParseRetryMs) || 0)) break;
1638
+ await sleep(Math.max(50, Number(networkParseIntervalMs) || 250));
1639
+ } while (true);
1640
+
1641
+ let closeResult = null;
1642
+ if (closeResume) {
1643
+ closeResult = await closeChatResumeModal(client);
1644
+ }
1645
+
1646
+ return {
1647
+ candidate: detailCandidateResult.candidate,
1648
+ parsed_network_profiles: detailCandidateResult.parsed_network_profiles,
1649
+ network_bodies: networkBodies,
1650
+ network_parse_retry_elapsed_ms: Date.now() - parseStarted,
1651
+ network_event_count: networkEvents.length,
1652
+ detail: {
1653
+ popup_text: resumeHtml.popupText,
1654
+ content_text: resumeHtml.contentText,
1655
+ resume_iframe_text: resumeHtml.resumeIframeText,
1656
+ popup_html_length: resumeHtml.popupHTML.length,
1657
+ content_html_length: resumeHtml.contentHTML.length,
1658
+ resume_iframe_html_length: resumeHtml.resumeIframeHTML.length
1659
+ },
1660
+ resume_html_read_error: resumeHtml.readError || null,
1661
+ close_result: closeResult
1662
+ };
1663
+ }
1664
+
1665
+ async function findVisibleTarget(client, roots, selectors) {
1666
+ let fallback = null;
1667
+ for (const root of roots) {
1668
+ if (!root?.nodeId) continue;
1669
+ for (const selector of selectors) {
1670
+ const nodeIds = await querySelectorAll(client, root.nodeId, selector);
1671
+ for (const nodeId of nodeIds) {
1672
+ const target = {
1673
+ root: root.name,
1674
+ root_node_id: root.nodeId,
1675
+ selector,
1676
+ node_id: nodeId
1677
+ };
1678
+ if (!fallback) fallback = target;
1679
+ try {
1680
+ const box = await getNodeBox(client, nodeId);
1681
+ if (box.rect.width > 2 && box.rect.height > 2) {
1682
+ return {
1683
+ ...target,
1684
+ center: box.center,
1685
+ rect: box.rect
1686
+ };
1687
+ }
1688
+ } catch {}
1689
+ }
1690
+ }
1691
+ }
1692
+ return fallback;
1693
+ }
1694
+
1695
+ async function pressEscape(client) {
1696
+ await pressKey(client, "Escape", {
1697
+ code: "Escape",
1698
+ windowsVirtualKeyCode: 27,
1699
+ nativeVirtualKeyCode: 27
1700
+ });
1701
+ }