@web-auto/camo 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +586 -586
- package/bin/browser-service.mjs +11 -11
- package/bin/camo.mjs +22 -22
- package/package.json +48 -48
- package/scripts/build.mjs +19 -19
- package/scripts/bump-version.mjs +34 -34
- package/scripts/check-file-size.mjs +80 -80
- package/scripts/file-size-policy.json +12 -2
- package/scripts/install.mjs +76 -76
- package/scripts/release.sh +54 -54
- package/src/autoscript/action-providers/index.mjs +6 -6
- package/src/autoscript/impact-engine.mjs +78 -78
- package/src/autoscript/runtime.mjs +1017 -1017
- package/src/autoscript/schema.mjs +376 -376
- package/src/cli.mjs +405 -405
- package/src/commands/attach.mjs +141 -141
- package/src/commands/autoscript.mjs +1011 -1011
- package/src/commands/browser.mjs +1255 -1255
- package/src/commands/container.mjs +401 -401
- package/src/commands/cookies.mjs +69 -69
- package/src/commands/create.mjs +98 -98
- package/src/commands/devtools.mjs +349 -349
- package/src/commands/events.mjs +152 -152
- package/src/commands/highlight-mode.mjs +24 -24
- package/src/commands/init.mjs +68 -68
- package/src/commands/lifecycle.mjs +275 -275
- package/src/commands/mouse.mjs +45 -45
- package/src/commands/profile.mjs +46 -46
- package/src/commands/record.mjs +115 -115
- package/src/commands/system.mjs +14 -14
- package/src/commands/window.mjs +123 -123
- package/src/container/change-notifier.mjs +362 -362
- package/src/container/element-filter.mjs +143 -143
- package/src/container/index.mjs +3 -3
- package/src/container/runtime-core/checkpoint.mjs +209 -209
- package/src/container/runtime-core/index.mjs +21 -21
- package/src/container/runtime-core/operations/index.mjs +774 -774
- package/src/container/runtime-core/operations/selector-scripts.mjs +277 -277
- package/src/container/runtime-core/operations/tab-pool.mjs +746 -746
- package/src/container/runtime-core/operations/viewport.mjs +189 -189
- package/src/container/runtime-core/search.mjs +190 -190
- package/src/container/runtime-core/subscription.mjs +224 -224
- package/src/container/runtime-core/utils.mjs +94 -94
- package/src/container/runtime-core/validation.mjs +127 -127
- package/src/container/runtime-core.mjs +1 -1
- package/src/container/subscription-registry.mjs +459 -459
- package/src/core/actions.mjs +561 -561
- package/src/core/browser.mjs +266 -266
- package/src/core/index.mjs +52 -52
- package/src/core/utils.mjs +91 -91
- package/src/events/daemon-entry.mjs +33 -33
- package/src/events/daemon.mjs +80 -80
- package/src/events/progress-log.mjs +109 -109
- package/src/events/ws-server.mjs +239 -239
- package/src/lib/client.mjs +200 -200
- package/src/lifecycle/cleanup.mjs +83 -83
- package/src/lifecycle/lock.mjs +126 -126
- package/src/lifecycle/session-registry.mjs +279 -279
- package/src/lifecycle/session-view.mjs +76 -76
- package/src/lifecycle/session-watchdog.mjs +281 -281
- package/src/services/browser-service/index.js +671 -671
- package/src/services/browser-service/internal/BrowserSession.input.test.js +389 -389
- package/src/services/browser-service/internal/BrowserSession.js +325 -304
- package/src/services/browser-service/internal/ElementRegistry.js +60 -60
- package/src/services/browser-service/internal/ProfileLock.js +84 -84
- package/src/services/browser-service/internal/SessionManager.js +184 -184
- package/src/services/browser-service/internal/SessionManager.test.js +39 -39
- package/src/services/browser-service/internal/browser-session/cookies.js +144 -144
- package/src/services/browser-service/internal/browser-session/input-ops.js +222 -222
- package/src/services/browser-service/internal/browser-session/input-pipeline.js +144 -144
- package/src/services/browser-service/internal/browser-session/logging.js +46 -46
- package/src/services/browser-service/internal/browser-session/navigation.js +38 -38
- package/src/services/browser-service/internal/browser-session/page-hooks.js +442 -442
- package/src/services/browser-service/internal/browser-session/page-management.js +302 -302
- package/src/services/browser-service/internal/browser-session/page-management.test.js +148 -148
- package/src/services/browser-service/internal/browser-session/recording.js +198 -198
- package/src/services/browser-service/internal/browser-session/runtime-events.js +61 -61
- package/src/services/browser-service/internal/browser-session/session-core.js +84 -84
- package/src/services/browser-service/internal/browser-session/session-state.js +38 -38
- package/src/services/browser-service/internal/browser-session/types.js +14 -14
- package/src/services/browser-service/internal/browser-session/utils.js +95 -95
- package/src/services/browser-service/internal/browser-session/viewport-manager.js +46 -46
- package/src/services/browser-service/internal/browser-session/viewport.js +215 -215
- package/src/services/browser-service/internal/container-matcher.js +851 -851
- package/src/services/browser-service/internal/container-registry.js +182 -182
- package/src/services/browser-service/internal/engine-manager.js +259 -259
- package/src/services/browser-service/internal/fingerprint.js +203 -203
- package/src/services/browser-service/internal/heartbeat.js +137 -137
- package/src/services/browser-service/internal/logging.js +46 -46
- package/src/services/browser-service/internal/page-runtime/runtime.js +1317 -1317
- package/src/services/browser-service/internal/pageRuntime.js +28 -28
- package/src/services/browser-service/internal/runtimeInjector.js +31 -31
- package/src/services/browser-service/internal/service-process-logger.js +140 -140
- package/src/services/browser-service/internal/state-bus.js +45 -45
- package/src/services/browser-service/internal/storage-paths.js +42 -42
- package/src/services/browser-service/internal/ws-server.js +1194 -1194
- package/src/services/browser-service/internal/ws-server.test.js +58 -58
- package/src/services/browser-service/server.mjs +6 -6
- package/src/services/controller/cli-bridge.js +93 -93
- package/src/services/controller/container-index.js +50 -50
- package/src/services/controller/container-storage.js +36 -36
- package/src/services/controller/controller-actions.js +207 -207
- package/src/services/controller/controller.js +1138 -1138
- package/src/services/controller/selectors.js +54 -54
- package/src/services/controller/transport.js +125 -125
- package/src/utils/args.mjs +26 -26
- package/src/utils/browser-service.mjs +544 -544
- package/src/utils/command-log.mjs +64 -64
- package/src/utils/config.mjs +214 -214
- package/src/utils/fingerprint.mjs +181 -181
- package/src/utils/help.mjs +216 -216
- package/src/utils/js-policy.mjs +13 -13
- package/src/utils/ws-client.mjs +30 -30
|
@@ -1,209 +1,209 @@
|
|
|
1
|
-
import { callAPI, getDomSnapshotByProfile } from '../../utils/browser-service.mjs';
|
|
2
|
-
import {
|
|
3
|
-
asErrorPayload,
|
|
4
|
-
buildSelectorCheck,
|
|
5
|
-
ensureActiveSession,
|
|
6
|
-
getCurrentUrl,
|
|
7
|
-
isCheckpointRiskUrl,
|
|
8
|
-
maybeSelector,
|
|
9
|
-
normalizeArray,
|
|
10
|
-
} from './utils.mjs';
|
|
11
|
-
|
|
12
|
-
export const XHS_CHECKPOINTS = {
|
|
13
|
-
search_ready: [
|
|
14
|
-
'#search-input',
|
|
15
|
-
'input.search-input',
|
|
16
|
-
'.search-result-list',
|
|
17
|
-
],
|
|
18
|
-
home_ready: [
|
|
19
|
-
'.feeds-page',
|
|
20
|
-
'.note-item',
|
|
21
|
-
],
|
|
22
|
-
detail_ready: [
|
|
23
|
-
'.note-scroller',
|
|
24
|
-
'.note-content',
|
|
25
|
-
'.interaction-container',
|
|
26
|
-
],
|
|
27
|
-
comments_ready: [
|
|
28
|
-
'.comments-container',
|
|
29
|
-
'.comment-item',
|
|
30
|
-
],
|
|
31
|
-
login_guard: [
|
|
32
|
-
'.login-container',
|
|
33
|
-
'.login-dialog',
|
|
34
|
-
'#login-container',
|
|
35
|
-
],
|
|
36
|
-
risk_control: [
|
|
37
|
-
'.qrcode-box',
|
|
38
|
-
'.captcha-container',
|
|
39
|
-
'[class*="captcha"]',
|
|
40
|
-
],
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export async function detectCheckpoint({ profileId, platform = 'generic' }) {
|
|
44
|
-
try {
|
|
45
|
-
const session = await ensureActiveSession(profileId);
|
|
46
|
-
const resolvedProfile = session.profileId || profileId;
|
|
47
|
-
|
|
48
|
-
if (platform !== 'xiaohongshu') {
|
|
49
|
-
const url = await getCurrentUrl(resolvedProfile);
|
|
50
|
-
return {
|
|
51
|
-
ok: true,
|
|
52
|
-
code: 'CHECKPOINT_DETECTED',
|
|
53
|
-
message: 'Checkpoint detected',
|
|
54
|
-
data: {
|
|
55
|
-
profileId: resolvedProfile,
|
|
56
|
-
platform,
|
|
57
|
-
checkpoint: 'unknown',
|
|
58
|
-
url,
|
|
59
|
-
signals: [],
|
|
60
|
-
selectorHits: {},
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const [url, snapshot] = await Promise.all([
|
|
66
|
-
getCurrentUrl(resolvedProfile),
|
|
67
|
-
getDomSnapshotByProfile(resolvedProfile),
|
|
68
|
-
]);
|
|
69
|
-
|
|
70
|
-
const signals = [];
|
|
71
|
-
const counter = {};
|
|
72
|
-
const addCount = (label, selectors) => {
|
|
73
|
-
for (const css of selectors) {
|
|
74
|
-
const count = buildSelectorCheck(snapshot, css).length;
|
|
75
|
-
if (count > 0) {
|
|
76
|
-
counter[css] = count;
|
|
77
|
-
signals.push(`${label}:${css}`);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
addCount('search_ready', XHS_CHECKPOINTS.search_ready);
|
|
83
|
-
addCount('home_ready', XHS_CHECKPOINTS.home_ready);
|
|
84
|
-
addCount('detail_ready', XHS_CHECKPOINTS.detail_ready);
|
|
85
|
-
addCount('comments_ready', XHS_CHECKPOINTS.comments_ready);
|
|
86
|
-
addCount('login_guard', XHS_CHECKPOINTS.login_guard);
|
|
87
|
-
addCount('risk_control', XHS_CHECKPOINTS.risk_control);
|
|
88
|
-
|
|
89
|
-
let checkpoint = 'unknown';
|
|
90
|
-
if (!url || !url.includes('xiaohongshu.com')) checkpoint = 'offsite';
|
|
91
|
-
else if (isCheckpointRiskUrl(url)) checkpoint = 'risk_control';
|
|
92
|
-
else if (signals.some((item) => item.startsWith('login_guard:'))) checkpoint = 'login_guard';
|
|
93
|
-
else if (signals.some((item) => item.startsWith('comments_ready:'))) checkpoint = 'comments_ready';
|
|
94
|
-
else if (signals.some((item) => item.startsWith('detail_ready:'))) checkpoint = 'detail_ready';
|
|
95
|
-
else if (signals.some((item) => item.startsWith('search_ready:'))) checkpoint = 'search_ready';
|
|
96
|
-
else if (signals.some((item) => item.startsWith('home_ready:'))) checkpoint = 'home_ready';
|
|
97
|
-
|
|
98
|
-
return {
|
|
99
|
-
ok: true,
|
|
100
|
-
code: 'CHECKPOINT_DETECTED',
|
|
101
|
-
message: 'Checkpoint detected',
|
|
102
|
-
data: {
|
|
103
|
-
profileId: resolvedProfile,
|
|
104
|
-
platform,
|
|
105
|
-
checkpoint,
|
|
106
|
-
url,
|
|
107
|
-
signals,
|
|
108
|
-
selectorHits: counter,
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
} catch (err) {
|
|
112
|
-
return asErrorPayload('CHECKPOINT_DETECT_FAILED', err?.message || String(err));
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export async function captureCheckpoint({
|
|
117
|
-
profileId,
|
|
118
|
-
containerId = null,
|
|
119
|
-
selector = null,
|
|
120
|
-
platform = 'generic',
|
|
121
|
-
}) {
|
|
122
|
-
try {
|
|
123
|
-
const session = await ensureActiveSession(profileId);
|
|
124
|
-
const resolvedProfile = session.profileId || profileId;
|
|
125
|
-
const checkpointRes = await detectCheckpoint({ profileId: resolvedProfile, platform });
|
|
126
|
-
const effectiveSelector = maybeSelector({ profileId: resolvedProfile, containerId, selector });
|
|
127
|
-
const snapshot = await getDomSnapshotByProfile(resolvedProfile);
|
|
128
|
-
const matched = effectiveSelector ? buildSelectorCheck(snapshot, effectiveSelector) : [];
|
|
129
|
-
|
|
130
|
-
return {
|
|
131
|
-
ok: true,
|
|
132
|
-
code: 'CHECKPOINT_CAPTURED',
|
|
133
|
-
message: 'Checkpoint captured',
|
|
134
|
-
data: {
|
|
135
|
-
profileId: resolvedProfile,
|
|
136
|
-
checkpoint: checkpointRes?.data?.checkpoint || 'unknown',
|
|
137
|
-
checkpointUrl: checkpointRes?.data?.url || '',
|
|
138
|
-
containerId,
|
|
139
|
-
selector: effectiveSelector,
|
|
140
|
-
selectorCount: matched.length,
|
|
141
|
-
capturedAt: new Date().toISOString(),
|
|
142
|
-
},
|
|
143
|
-
};
|
|
144
|
-
} catch (err) {
|
|
145
|
-
return asErrorPayload('CHECKPOINT_CAPTURE_FAILED', err?.message || String(err));
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export async function restoreCheckpoint({
|
|
150
|
-
profileId,
|
|
151
|
-
checkpoint = null,
|
|
152
|
-
action,
|
|
153
|
-
containerId = null,
|
|
154
|
-
selector = null,
|
|
155
|
-
targetCheckpoint = null,
|
|
156
|
-
platform = 'generic',
|
|
157
|
-
}) {
|
|
158
|
-
try {
|
|
159
|
-
const session = await ensureActiveSession(profileId);
|
|
160
|
-
const resolvedProfile = session.profileId || profileId;
|
|
161
|
-
const effectiveSelector = maybeSelector({ profileId: resolvedProfile, containerId, selector });
|
|
162
|
-
let actionResult = null;
|
|
163
|
-
|
|
164
|
-
if (action === 'requery_container') {
|
|
165
|
-
if (!effectiveSelector) return asErrorPayload('CONTAINER_NOT_FOUND', 'Selector is required for requery_container');
|
|
166
|
-
const snapshot = await getDomSnapshotByProfile(resolvedProfile);
|
|
167
|
-
const matches = buildSelectorCheck(snapshot, effectiveSelector);
|
|
168
|
-
if (matches.length === 0) return asErrorPayload('CONTAINER_NOT_FOUND', `Container selector not found: ${effectiveSelector}`);
|
|
169
|
-
actionResult = { selector: effectiveSelector, count: matches.length };
|
|
170
|
-
} else if (action === 'scroll_into_view') {
|
|
171
|
-
if (!effectiveSelector) return asErrorPayload('CONTAINER_NOT_FOUND', 'Selector is required for scroll_into_view');
|
|
172
|
-
actionResult = await callAPI('evaluate', {
|
|
173
|
-
profileId: resolvedProfile,
|
|
174
|
-
script: `(async () => {
|
|
175
|
-
const el = document.querySelector(${JSON.stringify(effectiveSelector)});
|
|
176
|
-
if (!el) throw new Error('Element not found');
|
|
177
|
-
el.scrollIntoView({ behavior: 'auto', block: 'center', inline: 'nearest' });
|
|
178
|
-
return { ok: true, selector: ${JSON.stringify(effectiveSelector)} };
|
|
179
|
-
})()`,
|
|
180
|
-
});
|
|
181
|
-
} else if (action === 'page_back') {
|
|
182
|
-
actionResult = await callAPI('page:back', { profileId: resolvedProfile });
|
|
183
|
-
} else if (action === 'goto_checkpoint_url') {
|
|
184
|
-
const url = checkpoint?.checkpointUrl || checkpoint?.url || '';
|
|
185
|
-
if (!url) return asErrorPayload('CHECKPOINT_RESTORE_FAILED', 'checkpointUrl is required for goto_checkpoint_url');
|
|
186
|
-
actionResult = await callAPI('goto', { profileId: resolvedProfile, url });
|
|
187
|
-
} else {
|
|
188
|
-
return asErrorPayload('UNSUPPORTED_RECOVERY_ACTION', `Unsupported recovery action: ${action}`);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const checkpointAfter = await detectCheckpoint({ profileId: resolvedProfile, platform });
|
|
192
|
-
const reached = checkpointAfter?.data?.checkpoint || 'unknown';
|
|
193
|
-
const targetMatched = targetCheckpoint ? reached === targetCheckpoint : true;
|
|
194
|
-
return {
|
|
195
|
-
ok: true,
|
|
196
|
-
code: targetMatched ? 'CHECKPOINT_RESTORED' : 'CHECKPOINT_RESTORE_PARTIAL',
|
|
197
|
-
message: targetMatched ? 'Checkpoint restored' : 'Recovery action completed but target checkpoint not reached',
|
|
198
|
-
data: {
|
|
199
|
-
profileId: resolvedProfile,
|
|
200
|
-
action,
|
|
201
|
-
actionResult,
|
|
202
|
-
reachedCheckpoint: reached,
|
|
203
|
-
targetCheckpoint,
|
|
204
|
-
},
|
|
205
|
-
};
|
|
206
|
-
} catch (err) {
|
|
207
|
-
return asErrorPayload('CHECKPOINT_RESTORE_FAILED', err?.message || String(err));
|
|
208
|
-
}
|
|
209
|
-
}
|
|
1
|
+
import { callAPI, getDomSnapshotByProfile } from '../../utils/browser-service.mjs';
|
|
2
|
+
import {
|
|
3
|
+
asErrorPayload,
|
|
4
|
+
buildSelectorCheck,
|
|
5
|
+
ensureActiveSession,
|
|
6
|
+
getCurrentUrl,
|
|
7
|
+
isCheckpointRiskUrl,
|
|
8
|
+
maybeSelector,
|
|
9
|
+
normalizeArray,
|
|
10
|
+
} from './utils.mjs';
|
|
11
|
+
|
|
12
|
+
export const XHS_CHECKPOINTS = {
|
|
13
|
+
search_ready: [
|
|
14
|
+
'#search-input',
|
|
15
|
+
'input.search-input',
|
|
16
|
+
'.search-result-list',
|
|
17
|
+
],
|
|
18
|
+
home_ready: [
|
|
19
|
+
'.feeds-page',
|
|
20
|
+
'.note-item',
|
|
21
|
+
],
|
|
22
|
+
detail_ready: [
|
|
23
|
+
'.note-scroller',
|
|
24
|
+
'.note-content',
|
|
25
|
+
'.interaction-container',
|
|
26
|
+
],
|
|
27
|
+
comments_ready: [
|
|
28
|
+
'.comments-container',
|
|
29
|
+
'.comment-item',
|
|
30
|
+
],
|
|
31
|
+
login_guard: [
|
|
32
|
+
'.login-container',
|
|
33
|
+
'.login-dialog',
|
|
34
|
+
'#login-container',
|
|
35
|
+
],
|
|
36
|
+
risk_control: [
|
|
37
|
+
'.qrcode-box',
|
|
38
|
+
'.captcha-container',
|
|
39
|
+
'[class*="captcha"]',
|
|
40
|
+
],
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export async function detectCheckpoint({ profileId, platform = 'generic' }) {
|
|
44
|
+
try {
|
|
45
|
+
const session = await ensureActiveSession(profileId);
|
|
46
|
+
const resolvedProfile = session.profileId || profileId;
|
|
47
|
+
|
|
48
|
+
if (platform !== 'xiaohongshu') {
|
|
49
|
+
const url = await getCurrentUrl(resolvedProfile);
|
|
50
|
+
return {
|
|
51
|
+
ok: true,
|
|
52
|
+
code: 'CHECKPOINT_DETECTED',
|
|
53
|
+
message: 'Checkpoint detected',
|
|
54
|
+
data: {
|
|
55
|
+
profileId: resolvedProfile,
|
|
56
|
+
platform,
|
|
57
|
+
checkpoint: 'unknown',
|
|
58
|
+
url,
|
|
59
|
+
signals: [],
|
|
60
|
+
selectorHits: {},
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const [url, snapshot] = await Promise.all([
|
|
66
|
+
getCurrentUrl(resolvedProfile),
|
|
67
|
+
getDomSnapshotByProfile(resolvedProfile),
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
const signals = [];
|
|
71
|
+
const counter = {};
|
|
72
|
+
const addCount = (label, selectors) => {
|
|
73
|
+
for (const css of selectors) {
|
|
74
|
+
const count = buildSelectorCheck(snapshot, css).length;
|
|
75
|
+
if (count > 0) {
|
|
76
|
+
counter[css] = count;
|
|
77
|
+
signals.push(`${label}:${css}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
addCount('search_ready', XHS_CHECKPOINTS.search_ready);
|
|
83
|
+
addCount('home_ready', XHS_CHECKPOINTS.home_ready);
|
|
84
|
+
addCount('detail_ready', XHS_CHECKPOINTS.detail_ready);
|
|
85
|
+
addCount('comments_ready', XHS_CHECKPOINTS.comments_ready);
|
|
86
|
+
addCount('login_guard', XHS_CHECKPOINTS.login_guard);
|
|
87
|
+
addCount('risk_control', XHS_CHECKPOINTS.risk_control);
|
|
88
|
+
|
|
89
|
+
let checkpoint = 'unknown';
|
|
90
|
+
if (!url || !url.includes('xiaohongshu.com')) checkpoint = 'offsite';
|
|
91
|
+
else if (isCheckpointRiskUrl(url)) checkpoint = 'risk_control';
|
|
92
|
+
else if (signals.some((item) => item.startsWith('login_guard:'))) checkpoint = 'login_guard';
|
|
93
|
+
else if (signals.some((item) => item.startsWith('comments_ready:'))) checkpoint = 'comments_ready';
|
|
94
|
+
else if (signals.some((item) => item.startsWith('detail_ready:'))) checkpoint = 'detail_ready';
|
|
95
|
+
else if (signals.some((item) => item.startsWith('search_ready:'))) checkpoint = 'search_ready';
|
|
96
|
+
else if (signals.some((item) => item.startsWith('home_ready:'))) checkpoint = 'home_ready';
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
ok: true,
|
|
100
|
+
code: 'CHECKPOINT_DETECTED',
|
|
101
|
+
message: 'Checkpoint detected',
|
|
102
|
+
data: {
|
|
103
|
+
profileId: resolvedProfile,
|
|
104
|
+
platform,
|
|
105
|
+
checkpoint,
|
|
106
|
+
url,
|
|
107
|
+
signals,
|
|
108
|
+
selectorHits: counter,
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
} catch (err) {
|
|
112
|
+
return asErrorPayload('CHECKPOINT_DETECT_FAILED', err?.message || String(err));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function captureCheckpoint({
|
|
117
|
+
profileId,
|
|
118
|
+
containerId = null,
|
|
119
|
+
selector = null,
|
|
120
|
+
platform = 'generic',
|
|
121
|
+
}) {
|
|
122
|
+
try {
|
|
123
|
+
const session = await ensureActiveSession(profileId);
|
|
124
|
+
const resolvedProfile = session.profileId || profileId;
|
|
125
|
+
const checkpointRes = await detectCheckpoint({ profileId: resolvedProfile, platform });
|
|
126
|
+
const effectiveSelector = maybeSelector({ profileId: resolvedProfile, containerId, selector });
|
|
127
|
+
const snapshot = await getDomSnapshotByProfile(resolvedProfile);
|
|
128
|
+
const matched = effectiveSelector ? buildSelectorCheck(snapshot, effectiveSelector) : [];
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
ok: true,
|
|
132
|
+
code: 'CHECKPOINT_CAPTURED',
|
|
133
|
+
message: 'Checkpoint captured',
|
|
134
|
+
data: {
|
|
135
|
+
profileId: resolvedProfile,
|
|
136
|
+
checkpoint: checkpointRes?.data?.checkpoint || 'unknown',
|
|
137
|
+
checkpointUrl: checkpointRes?.data?.url || '',
|
|
138
|
+
containerId,
|
|
139
|
+
selector: effectiveSelector,
|
|
140
|
+
selectorCount: matched.length,
|
|
141
|
+
capturedAt: new Date().toISOString(),
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
} catch (err) {
|
|
145
|
+
return asErrorPayload('CHECKPOINT_CAPTURE_FAILED', err?.message || String(err));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function restoreCheckpoint({
|
|
150
|
+
profileId,
|
|
151
|
+
checkpoint = null,
|
|
152
|
+
action,
|
|
153
|
+
containerId = null,
|
|
154
|
+
selector = null,
|
|
155
|
+
targetCheckpoint = null,
|
|
156
|
+
platform = 'generic',
|
|
157
|
+
}) {
|
|
158
|
+
try {
|
|
159
|
+
const session = await ensureActiveSession(profileId);
|
|
160
|
+
const resolvedProfile = session.profileId || profileId;
|
|
161
|
+
const effectiveSelector = maybeSelector({ profileId: resolvedProfile, containerId, selector });
|
|
162
|
+
let actionResult = null;
|
|
163
|
+
|
|
164
|
+
if (action === 'requery_container') {
|
|
165
|
+
if (!effectiveSelector) return asErrorPayload('CONTAINER_NOT_FOUND', 'Selector is required for requery_container');
|
|
166
|
+
const snapshot = await getDomSnapshotByProfile(resolvedProfile);
|
|
167
|
+
const matches = buildSelectorCheck(snapshot, effectiveSelector);
|
|
168
|
+
if (matches.length === 0) return asErrorPayload('CONTAINER_NOT_FOUND', `Container selector not found: ${effectiveSelector}`);
|
|
169
|
+
actionResult = { selector: effectiveSelector, count: matches.length };
|
|
170
|
+
} else if (action === 'scroll_into_view') {
|
|
171
|
+
if (!effectiveSelector) return asErrorPayload('CONTAINER_NOT_FOUND', 'Selector is required for scroll_into_view');
|
|
172
|
+
actionResult = await callAPI('evaluate', {
|
|
173
|
+
profileId: resolvedProfile,
|
|
174
|
+
script: `(async () => {
|
|
175
|
+
const el = document.querySelector(${JSON.stringify(effectiveSelector)});
|
|
176
|
+
if (!el) throw new Error('Element not found');
|
|
177
|
+
el.scrollIntoView({ behavior: 'auto', block: 'center', inline: 'nearest' });
|
|
178
|
+
return { ok: true, selector: ${JSON.stringify(effectiveSelector)} };
|
|
179
|
+
})()`,
|
|
180
|
+
});
|
|
181
|
+
} else if (action === 'page_back') {
|
|
182
|
+
actionResult = await callAPI('page:back', { profileId: resolvedProfile });
|
|
183
|
+
} else if (action === 'goto_checkpoint_url') {
|
|
184
|
+
const url = checkpoint?.checkpointUrl || checkpoint?.url || '';
|
|
185
|
+
if (!url) return asErrorPayload('CHECKPOINT_RESTORE_FAILED', 'checkpointUrl is required for goto_checkpoint_url');
|
|
186
|
+
actionResult = await callAPI('goto', { profileId: resolvedProfile, url });
|
|
187
|
+
} else {
|
|
188
|
+
return asErrorPayload('UNSUPPORTED_RECOVERY_ACTION', `Unsupported recovery action: ${action}`);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const checkpointAfter = await detectCheckpoint({ profileId: resolvedProfile, platform });
|
|
192
|
+
const reached = checkpointAfter?.data?.checkpoint || 'unknown';
|
|
193
|
+
const targetMatched = targetCheckpoint ? reached === targetCheckpoint : true;
|
|
194
|
+
return {
|
|
195
|
+
ok: true,
|
|
196
|
+
code: targetMatched ? 'CHECKPOINT_RESTORED' : 'CHECKPOINT_RESTORE_PARTIAL',
|
|
197
|
+
message: targetMatched ? 'Checkpoint restored' : 'Recovery action completed but target checkpoint not reached',
|
|
198
|
+
data: {
|
|
199
|
+
profileId: resolvedProfile,
|
|
200
|
+
action,
|
|
201
|
+
actionResult,
|
|
202
|
+
reachedCheckpoint: reached,
|
|
203
|
+
targetCheckpoint,
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
} catch (err) {
|
|
207
|
+
return asErrorPayload('CHECKPOINT_RESTORE_FAILED', err?.message || String(err));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
export {
|
|
2
|
-
ensureActiveSession,
|
|
3
|
-
asErrorPayload,
|
|
4
|
-
normalizeArray,
|
|
5
|
-
extractPageList,
|
|
6
|
-
getCurrentUrl,
|
|
7
|
-
maybeSelector,
|
|
8
|
-
buildSelectorCheck,
|
|
9
|
-
isCheckpointRiskUrl,
|
|
10
|
-
} from './utils.mjs';
|
|
11
|
-
|
|
12
|
-
export {
|
|
13
|
-
XHS_CHECKPOINTS,
|
|
14
|
-
detectCheckpoint,
|
|
15
|
-
captureCheckpoint,
|
|
16
|
-
restoreCheckpoint,
|
|
17
|
-
} from './checkpoint.mjs';
|
|
18
|
-
|
|
19
|
-
export { validateOperation } from './validation.mjs';
|
|
20
|
-
export { executeOperation } from './operations/index.mjs';
|
|
21
|
-
export { watchSubscriptions } from './subscription.mjs';
|
|
1
|
+
export {
|
|
2
|
+
ensureActiveSession,
|
|
3
|
+
asErrorPayload,
|
|
4
|
+
normalizeArray,
|
|
5
|
+
extractPageList,
|
|
6
|
+
getCurrentUrl,
|
|
7
|
+
maybeSelector,
|
|
8
|
+
buildSelectorCheck,
|
|
9
|
+
isCheckpointRiskUrl,
|
|
10
|
+
} from './utils.mjs';
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
XHS_CHECKPOINTS,
|
|
14
|
+
detectCheckpoint,
|
|
15
|
+
captureCheckpoint,
|
|
16
|
+
restoreCheckpoint,
|
|
17
|
+
} from './checkpoint.mjs';
|
|
18
|
+
|
|
19
|
+
export { validateOperation } from './validation.mjs';
|
|
20
|
+
export { executeOperation } from './operations/index.mjs';
|
|
21
|
+
export { watchSubscriptions } from './subscription.mjs';
|