@web-auto/camo 0.1.2 → 0.1.4
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/README.md +137 -0
- package/package.json +7 -3
- package/scripts/check-file-size.mjs +80 -0
- package/scripts/file-size-policy.json +8 -0
- package/src/autoscript/action-providers/index.mjs +9 -0
- package/src/autoscript/action-providers/xhs/comments.mjs +412 -0
- package/src/autoscript/action-providers/xhs/common.mjs +77 -0
- package/src/autoscript/action-providers/xhs/detail.mjs +181 -0
- package/src/autoscript/action-providers/xhs/interaction.mjs +466 -0
- package/src/autoscript/action-providers/xhs/like-rules.mjs +57 -0
- package/src/autoscript/action-providers/xhs/persistence.mjs +167 -0
- package/src/autoscript/action-providers/xhs/search.mjs +174 -0
- package/src/autoscript/action-providers/xhs.mjs +133 -0
- package/src/autoscript/impact-engine.mjs +78 -0
- package/src/autoscript/runtime.mjs +1015 -0
- package/src/autoscript/schema.mjs +370 -0
- package/src/autoscript/xhs-unified-template.mjs +931 -0
- package/src/cli.mjs +190 -78
- package/src/commands/autoscript.mjs +1100 -0
- package/src/commands/browser.mjs +20 -4
- package/src/commands/container.mjs +401 -0
- package/src/commands/events.mjs +152 -0
- package/src/commands/lifecycle.mjs +17 -3
- package/src/commands/window.mjs +32 -1
- package/src/container/change-notifier.mjs +311 -0
- package/src/container/element-filter.mjs +143 -0
- package/src/container/index.mjs +3 -0
- package/src/container/runtime-core/checkpoint.mjs +195 -0
- package/src/container/runtime-core/index.mjs +21 -0
- package/src/container/runtime-core/operations/index.mjs +351 -0
- package/src/container/runtime-core/operations/selector-scripts.mjs +68 -0
- package/src/container/runtime-core/operations/tab-pool.mjs +544 -0
- package/src/container/runtime-core/operations/viewport.mjs +143 -0
- package/src/container/runtime-core/subscription.mjs +87 -0
- package/src/container/runtime-core/utils.mjs +94 -0
- package/src/container/runtime-core/validation.mjs +127 -0
- package/src/container/runtime-core.mjs +1 -0
- package/src/container/subscription-registry.mjs +459 -0
- package/src/core/actions.mjs +573 -0
- package/src/core/browser.mjs +270 -0
- package/src/core/index.mjs +53 -0
- package/src/core/utils.mjs +87 -0
- package/src/events/daemon-entry.mjs +33 -0
- package/src/events/daemon.mjs +80 -0
- package/src/events/progress-log.mjs +109 -0
- package/src/events/ws-server.mjs +239 -0
- package/src/lib/client.mjs +200 -0
- package/src/lifecycle/session-registry.mjs +8 -4
- package/src/lifecycle/session-watchdog.mjs +220 -0
- package/src/utils/browser-service.mjs +232 -9
- package/src/utils/help.mjs +28 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { getDomSnapshotByProfile } from '../../utils/browser-service.mjs';
|
|
2
|
+
import { ChangeNotifier } from '../change-notifier.mjs';
|
|
3
|
+
import { ensureActiveSession, normalizeArray } from './utils.mjs';
|
|
4
|
+
|
|
5
|
+
export async function watchSubscriptions({
|
|
6
|
+
profileId,
|
|
7
|
+
subscriptions,
|
|
8
|
+
throttle = 500,
|
|
9
|
+
onEvent = () => {},
|
|
10
|
+
onError = () => {},
|
|
11
|
+
}) {
|
|
12
|
+
const session = await ensureActiveSession(profileId);
|
|
13
|
+
const resolvedProfile = session.profileId || profileId;
|
|
14
|
+
const notifier = new ChangeNotifier();
|
|
15
|
+
const items = normalizeArray(subscriptions)
|
|
16
|
+
.map((item, index) => {
|
|
17
|
+
if (!item || typeof item !== 'object') return null;
|
|
18
|
+
const id = String(item.id || `sub_${index + 1}`);
|
|
19
|
+
const selector = String(item.selector || '').trim();
|
|
20
|
+
if (!selector) return null;
|
|
21
|
+
const events = normalizeArray(item.events).map((name) => String(name).trim()).filter(Boolean);
|
|
22
|
+
return { id, selector, events: events.length > 0 ? new Set(events) : null };
|
|
23
|
+
})
|
|
24
|
+
.filter(Boolean);
|
|
25
|
+
|
|
26
|
+
const state = new Map(items.map((item) => [item.id, { exists: false, stateSig: '', appearCount: 0 }]));
|
|
27
|
+
const intervalMs = Math.max(100, Number(throttle) || 500);
|
|
28
|
+
let stopped = false;
|
|
29
|
+
|
|
30
|
+
const emit = async (payload) => {
|
|
31
|
+
try {
|
|
32
|
+
await onEvent(payload);
|
|
33
|
+
} catch (err) {
|
|
34
|
+
onError(err);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const poll = async () => {
|
|
39
|
+
if (stopped) return;
|
|
40
|
+
try {
|
|
41
|
+
const snapshot = await getDomSnapshotByProfile(resolvedProfile);
|
|
42
|
+
const ts = new Date().toISOString();
|
|
43
|
+
for (const item of items) {
|
|
44
|
+
const prev = state.get(item.id) || { exists: false, stateSig: '', appearCount: 0 };
|
|
45
|
+
const elements = notifier.findElements(snapshot, { css: item.selector });
|
|
46
|
+
const exists = elements.length > 0;
|
|
47
|
+
const stateSig = elements.map((node) => node.path).sort().join(',');
|
|
48
|
+
const changed = stateSig !== prev.stateSig;
|
|
49
|
+
const next = {
|
|
50
|
+
exists,
|
|
51
|
+
stateSig,
|
|
52
|
+
appearCount: prev.appearCount + (exists && !prev.exists ? 1 : 0),
|
|
53
|
+
};
|
|
54
|
+
state.set(item.id, next);
|
|
55
|
+
|
|
56
|
+
const shouldEmit = (type) => !item.events || item.events.has(type);
|
|
57
|
+
if (exists && !prev.exists && shouldEmit('appear')) {
|
|
58
|
+
await emit({ type: 'appear', profileId: resolvedProfile, subscriptionId: item.id, selector: item.selector, count: elements.length, elements, timestamp: ts });
|
|
59
|
+
}
|
|
60
|
+
if (!exists && prev.exists && shouldEmit('disappear')) {
|
|
61
|
+
await emit({ type: 'disappear', profileId: resolvedProfile, subscriptionId: item.id, selector: item.selector, count: 0, elements: [], timestamp: ts });
|
|
62
|
+
}
|
|
63
|
+
if (exists && shouldEmit('exist')) {
|
|
64
|
+
await emit({ type: 'exist', profileId: resolvedProfile, subscriptionId: item.id, selector: item.selector, count: elements.length, elements, timestamp: ts });
|
|
65
|
+
}
|
|
66
|
+
if (changed && shouldEmit('change')) {
|
|
67
|
+
await emit({ type: 'change', profileId: resolvedProfile, subscriptionId: item.id, selector: item.selector, count: elements.length, elements, timestamp: ts });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
await emit({ type: 'tick', profileId: resolvedProfile, timestamp: ts });
|
|
71
|
+
} catch (err) {
|
|
72
|
+
onError(err);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const interval = setInterval(poll, intervalMs);
|
|
77
|
+
await poll();
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
stop: () => {
|
|
81
|
+
if (stopped) return;
|
|
82
|
+
stopped = true;
|
|
83
|
+
clearInterval(interval);
|
|
84
|
+
},
|
|
85
|
+
profileId: resolvedProfile,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { callAPI, getSessionByProfile } from '../../utils/browser-service.mjs';
|
|
2
|
+
import { getDefaultProfile } from '../../utils/config.mjs';
|
|
3
|
+
import { ChangeNotifier } from '../change-notifier.mjs';
|
|
4
|
+
import { getRegisteredTargets } from '../subscription-registry.mjs';
|
|
5
|
+
|
|
6
|
+
export function asErrorPayload(code, message, data = {}) {
|
|
7
|
+
return { ok: false, code, message, data };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function normalizeArray(value) {
|
|
11
|
+
return Array.isArray(value) ? value : [];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function extractPageList(payload) {
|
|
15
|
+
const pages = normalizeArray(payload?.pages || payload?.data?.pages)
|
|
16
|
+
.map((item, idx) => {
|
|
17
|
+
const rawIndex = Number(item?.index);
|
|
18
|
+
return {
|
|
19
|
+
...item,
|
|
20
|
+
index: Number.isFinite(rawIndex) ? rawIndex : idx,
|
|
21
|
+
};
|
|
22
|
+
})
|
|
23
|
+
.sort((a, b) => Number(a.index) - Number(b.index));
|
|
24
|
+
const activeIndex = Number(payload?.activeIndex ?? payload?.data?.activeIndex);
|
|
25
|
+
return {
|
|
26
|
+
pages,
|
|
27
|
+
activeIndex: Number.isFinite(activeIndex) ? activeIndex : null,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function getCurrentUrl(profileId) {
|
|
32
|
+
const res = await callAPI('evaluate', {
|
|
33
|
+
profileId,
|
|
34
|
+
script: 'window.location.href',
|
|
35
|
+
});
|
|
36
|
+
return String(res?.result || res?.data?.result || '');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function firstSelectorForContainer(profileId, containerId) {
|
|
40
|
+
const targets = getRegisteredTargets(profileId)?.profile;
|
|
41
|
+
const selectors = normalizeArray(targets?.selectors);
|
|
42
|
+
const direct = selectors.find((item) => item?.setId === containerId && typeof item?.css === 'string');
|
|
43
|
+
if (direct?.css) return direct.css;
|
|
44
|
+
|
|
45
|
+
const fallbackTarget = normalizeArray(targets?.targets).find(
|
|
46
|
+
(item) => item?.containerId === containerId && item?.markerType === 'url_dom' && item?.dom?.css,
|
|
47
|
+
);
|
|
48
|
+
if (fallbackTarget?.dom?.css) return fallbackTarget.dom.css;
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function maybeSelector({ profileId, containerId, selector }) {
|
|
53
|
+
if (selector && typeof selector === 'string') return selector;
|
|
54
|
+
if (containerId && typeof containerId === 'string') {
|
|
55
|
+
return firstSelectorForContainer(profileId, containerId);
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function buildSelectorCheck(snapshot, selector) {
|
|
61
|
+
if (!snapshot || !selector) return [];
|
|
62
|
+
const notifier = new ChangeNotifier();
|
|
63
|
+
if (typeof selector === 'string') {
|
|
64
|
+
return notifier.findElements(snapshot, { css: selector });
|
|
65
|
+
}
|
|
66
|
+
if (selector && typeof selector === 'object') {
|
|
67
|
+
return notifier.findElements(snapshot, selector);
|
|
68
|
+
}
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function isCheckpointRiskUrl(url) {
|
|
73
|
+
const lower = String(url || '').toLowerCase();
|
|
74
|
+
return (
|
|
75
|
+
lower.includes('/website-login/captcha')
|
|
76
|
+
|| lower.includes('verifyuuid=')
|
|
77
|
+
|| lower.includes('verifytype=')
|
|
78
|
+
|| lower.includes('verifybiz=')
|
|
79
|
+
|| lower.includes('/website-login/verify')
|
|
80
|
+
|| lower.includes('/website-login/security')
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function ensureActiveSession(profileId) {
|
|
85
|
+
const targetProfile = profileId || getDefaultProfile();
|
|
86
|
+
if (!targetProfile) {
|
|
87
|
+
throw new Error('profileId is required');
|
|
88
|
+
}
|
|
89
|
+
const session = await getSessionByProfile(targetProfile);
|
|
90
|
+
if (!session) {
|
|
91
|
+
throw new Error(`No active session for profile: ${targetProfile}`);
|
|
92
|
+
}
|
|
93
|
+
return session;
|
|
94
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { getDomSnapshotByProfile } from '../../utils/browser-service.mjs';
|
|
2
|
+
import { detectCheckpoint } from './checkpoint.mjs';
|
|
3
|
+
import {
|
|
4
|
+
asErrorPayload,
|
|
5
|
+
buildSelectorCheck,
|
|
6
|
+
ensureActiveSession,
|
|
7
|
+
getCurrentUrl,
|
|
8
|
+
maybeSelector,
|
|
9
|
+
normalizeArray,
|
|
10
|
+
} from './utils.mjs';
|
|
11
|
+
|
|
12
|
+
async function validatePage(profileId, spec = {}, platform = 'xiaohongshu') {
|
|
13
|
+
const url = await getCurrentUrl(profileId);
|
|
14
|
+
const includes = normalizeArray(spec.urlIncludes || []);
|
|
15
|
+
const excludes = normalizeArray(spec.urlExcludes || []);
|
|
16
|
+
const hostIncludes = normalizeArray(spec.hostIncludes || []);
|
|
17
|
+
const errors = [];
|
|
18
|
+
|
|
19
|
+
for (const token of includes) {
|
|
20
|
+
if (!url.includes(String(token))) errors.push(`url missing token: ${token}`);
|
|
21
|
+
}
|
|
22
|
+
for (const token of excludes) {
|
|
23
|
+
if (url.includes(String(token))) errors.push(`url contains forbidden token: ${token}`);
|
|
24
|
+
}
|
|
25
|
+
if (hostIncludes.length > 0 && !hostIncludes.some((token) => url.includes(String(token)))) {
|
|
26
|
+
errors.push(`url host mismatch, expected one of: ${hostIncludes.join(',')}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const checkpoints = normalizeArray(spec.checkpointIn || []);
|
|
30
|
+
let checkpoint = null;
|
|
31
|
+
if (checkpoints.length > 0) {
|
|
32
|
+
const detected = await detectCheckpoint({ profileId, platform });
|
|
33
|
+
checkpoint = detected?.data?.checkpoint || null;
|
|
34
|
+
if (!checkpoints.includes(checkpoint)) {
|
|
35
|
+
errors.push(`checkpoint mismatch: got ${checkpoint}, expect one of ${checkpoints.join(',')}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
ok: errors.length === 0,
|
|
41
|
+
url,
|
|
42
|
+
checkpoint,
|
|
43
|
+
errors,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function validateContainer(profileId, spec = {}) {
|
|
48
|
+
const snapshot = await getDomSnapshotByProfile(profileId);
|
|
49
|
+
const selector = maybeSelector({
|
|
50
|
+
profileId,
|
|
51
|
+
containerId: spec.containerId || null,
|
|
52
|
+
selector: spec.selector || null,
|
|
53
|
+
});
|
|
54
|
+
if (!selector) {
|
|
55
|
+
return { ok: false, selector: null, count: 0, errors: ['container selector not resolved'] };
|
|
56
|
+
}
|
|
57
|
+
const matched = buildSelectorCheck(snapshot, selector);
|
|
58
|
+
const count = matched.length;
|
|
59
|
+
const mustExist = spec.mustExist !== false;
|
|
60
|
+
const minCount = Number.isFinite(Number(spec.minCount)) ? Number(spec.minCount) : (mustExist ? 1 : 0);
|
|
61
|
+
const maxCount = Number.isFinite(Number(spec.maxCount)) ? Number(spec.maxCount) : null;
|
|
62
|
+
const errors = [];
|
|
63
|
+
|
|
64
|
+
if (count < minCount) errors.push(`container count too small: ${count} < ${minCount}`);
|
|
65
|
+
if (maxCount !== null && count > maxCount) errors.push(`container count too large: ${count} > ${maxCount}`);
|
|
66
|
+
if (!mustExist && minCount === 0) {
|
|
67
|
+
return { ok: true, selector, count, errors: [] };
|
|
68
|
+
}
|
|
69
|
+
return { ok: errors.length === 0, selector, count, errors };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function validateOperation({
|
|
73
|
+
profileId,
|
|
74
|
+
validationSpec = {},
|
|
75
|
+
phase = 'pre',
|
|
76
|
+
context = {},
|
|
77
|
+
platform = 'xiaohongshu',
|
|
78
|
+
}) {
|
|
79
|
+
try {
|
|
80
|
+
const mode = String(validationSpec.mode || 'none').toLowerCase();
|
|
81
|
+
if (mode === 'none') {
|
|
82
|
+
return { ok: true, code: 'VALIDATION_SKIPPED', message: 'Validation skipped', data: { phase, mode } };
|
|
83
|
+
}
|
|
84
|
+
if (phase === 'pre' && mode === 'post') {
|
|
85
|
+
return { ok: true, code: 'VALIDATION_SKIPPED', message: 'Pre validation skipped by mode=post', data: { phase, mode } };
|
|
86
|
+
}
|
|
87
|
+
if (phase === 'post' && mode === 'pre') {
|
|
88
|
+
return { ok: true, code: 'VALIDATION_SKIPPED', message: 'Post validation skipped by mode=pre', data: { phase, mode } };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const effective = validationSpec[phase] || {};
|
|
92
|
+
const resolvedProfile = (await ensureActiveSession(profileId)).profileId || profileId;
|
|
93
|
+
const pageResult = effective.page
|
|
94
|
+
? await validatePage(resolvedProfile, effective.page, platform)
|
|
95
|
+
: { ok: true, errors: [] };
|
|
96
|
+
const containerResult = effective.container
|
|
97
|
+
? await validateContainer(resolvedProfile, effective.container)
|
|
98
|
+
: { ok: true, errors: [] };
|
|
99
|
+
const allErrors = [...normalizeArray(pageResult.errors), ...normalizeArray(containerResult.errors)];
|
|
100
|
+
|
|
101
|
+
if (allErrors.length > 0) {
|
|
102
|
+
return asErrorPayload('VALIDATION_FAILED', `Validation failed at phase=${phase}`, {
|
|
103
|
+
phase,
|
|
104
|
+
mode,
|
|
105
|
+
errors: allErrors,
|
|
106
|
+
page: pageResult,
|
|
107
|
+
container: containerResult,
|
|
108
|
+
context,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
ok: true,
|
|
114
|
+
code: 'VALIDATION_PASSED',
|
|
115
|
+
message: `Validation passed at phase=${phase}`,
|
|
116
|
+
data: {
|
|
117
|
+
phase,
|
|
118
|
+
mode,
|
|
119
|
+
page: pageResult,
|
|
120
|
+
container: containerResult,
|
|
121
|
+
context,
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
} catch (err) {
|
|
125
|
+
return asErrorPayload('VALIDATION_FAILED', err?.message || String(err), { phase, context });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './runtime-core/index.mjs';
|