@xiaohhhh1/canvas-agent 0.4.40 → 0.4.41

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.
@@ -138,6 +138,7 @@ export declare class FastMossIntegration {
138
138
  private loadRows;
139
139
  private useLatestPage;
140
140
  private openProductRanking;
141
+ private waitForManualVerification;
141
142
  private openVideoLearningPage;
142
143
  private readVideoLearningItems;
143
144
  /**
@@ -154,6 +155,11 @@ export declare class FastMossIntegration {
154
155
  private clickVideoLearningNextPage;
155
156
  private waitForVideoLearningNavigation;
156
157
  }
158
+ export declare function waitForFastMossVerificationClear(inspect: () => Promise<Pick<FastMossStatus, "verificationRequired" | "membershipExpired" | "authenticated">>, options?: {
159
+ timeoutMs?: number;
160
+ pollMs?: number;
161
+ sleep?: (milliseconds: number) => Promise<unknown>;
162
+ }): Promise<void>;
157
163
  export declare function videoEvidenceTimestamps(durationMs: number): number[];
158
164
  export declare function fastMossSalesRankUrl(market?: string): string;
159
165
  export declare function fastMossRankingUrl(source: FastMossRankingSource, market?: string): string;
@@ -6,6 +6,7 @@ import { chromium } from "playwright-core";
6
6
  import { CONFIG_DIR } from "../config.js";
7
7
  const FASTMOSS_HOME = "https://www.fastmoss.com/";
8
8
  const MEMBERSHIP_RECHECK_MS = 5 * 60_000;
9
+ const MANUAL_VERIFICATION_TIMEOUT_MS = 10 * 60_000;
9
10
  const PRODUCT_TABLE_TIMEOUT_MS = 45_000;
10
11
  const PRODUCT_RANK_PAGE_SIZE = 10;
11
12
  const PRODUCT_RANK_MAX_PAGES = 50;
@@ -175,7 +176,7 @@ export class FastMossIntegration {
175
176
  await this.start();
176
177
  await this.inspect();
177
178
  if (this.verificationRequired)
178
- throw new Error("请先在 FastMoss 浏览器中手动完成验证");
179
+ await this.waitForManualVerification();
179
180
  if (this.membershipExpired)
180
181
  throw new Error("FastMoss 会员已过期或当前账号没有榜单权限,请先更换账号");
181
182
  if (Date.now() - this.lastMembershipCheckAt > MEMBERSHIP_RECHECK_MS) {
@@ -192,7 +193,16 @@ export class FastMossIntegration {
192
193
  let previousSignature = "";
193
194
  for (let pageNumber = 1; pageNumber <= PRODUCT_RANK_MAX_PAGES; pageNumber += 1) {
194
195
  const snapshot = await readVisibleTableSnapshot(this.page);
195
- if (!snapshot.tables[0]?.rows.length || snapshot.signature === previousSignature)
196
+ if (!snapshot.tables[0]?.rows.length) {
197
+ await this.inspect();
198
+ if (this.verificationRequired) {
199
+ await this.waitForManualVerification();
200
+ pageNumber -= 1;
201
+ continue;
202
+ }
203
+ break;
204
+ }
205
+ if (snapshot.signature === previousSignature)
196
206
  break;
197
207
  previousSignature = snapshot.signature;
198
208
  pages.push(snapshot.url);
@@ -204,10 +214,12 @@ export class FastMossIntegration {
204
214
  break;
205
215
  await this.page.waitForTimeout(800);
206
216
  await this.inspect();
207
- if (this.verificationRequired || this.membershipExpired)
217
+ if (this.verificationRequired)
218
+ await this.waitForManualVerification();
219
+ if (this.membershipExpired)
208
220
  break;
209
221
  }
210
- if (this.verificationRequired || this.membershipExpired)
222
+ if (this.membershipExpired)
211
223
  break;
212
224
  }
213
225
  const records = [...collected.values()];
@@ -227,7 +239,7 @@ export class FastMossIntegration {
227
239
  : `采集在${stage === "ranking" ? "榜单" : "商品详情互证"}阶段发现会员权限异常;已保留上次 ${previousProducts} 个商品结果`;
228
240
  return { ...this.status(), records: marketRecords, allRows: previousRows, trendProducts: previousProducts, detailFailures, interrupted: true };
229
241
  };
230
- if (this.verificationRequired || this.membershipExpired)
242
+ if (this.membershipExpired)
231
243
  return retainPreviousOnInterruption("ranking");
232
244
  const detailCandidates = selectDetailCandidates(marketRecords, DETAIL_CANDIDATE_LIMIT);
233
245
  const capturePage = this.page;
@@ -242,11 +254,13 @@ export class FastMossIntegration {
242
254
  if ((offset / DETAIL_FETCH_CONCURRENCY) % 10 === 0) {
243
255
  this.useLatestPage();
244
256
  await this.inspect();
245
- if (this.verificationRequired || this.membershipExpired)
257
+ if (this.verificationRequired)
258
+ await this.waitForManualVerification();
259
+ if (this.membershipExpired)
246
260
  break;
247
261
  }
248
262
  }
249
- if (this.verificationRequired || this.membershipExpired)
263
+ if (this.membershipExpired)
250
264
  return retainPreviousOnInterruption("detail", trendRows, detailFailures);
251
265
  if (!trendRows.length && !this.verificationRequired)
252
266
  throw new Error("榜单读取成功,但未能读取商品详情的最近 7 天销量;请检查会员权限后重试");
@@ -257,9 +271,7 @@ export class FastMossIntegration {
257
271
  await writeFile(path.join(this.dataDir, "observations.json"), JSON.stringify(allRows, null, 2), "utf8");
258
272
  await writeFile(path.join(this.dataDir, `capture-${localDate()}.json`), JSON.stringify({ capturedAt: this.lastCapture, pages, records: marketRecords, detailCandidates: detailCandidates.map((record) => record.product_id), trendRows, detailFailures }, null, 2), "utf8");
259
273
  const trendProducts = new Set(trendRows.map((row) => String(row.product_id))).size;
260
- this.message = this.verificationRequired
261
- ? `已保存验证前的 ${trendProducts} 个商品趋势;请手动完成滑块后再次抓取`
262
- : `四榜单去重后 ${marketRecords.length} 个商品,优先互证 ${detailCandidates.length} 个,其中 ${trendProducts} 个取得最近 ${DETAIL_TREND_DAYS} 天趋势`;
274
+ this.message = `四榜单去重后 ${marketRecords.length} 个商品,优先互证 ${detailCandidates.length} 个,其中 ${trendProducts} 个取得最近 ${DETAIL_TREND_DAYS} 天趋势`;
263
275
  return { ...this.status(), records: marketRecords, allRows, trendProducts, detailFailures };
264
276
  }
265
277
  /**
@@ -527,7 +539,7 @@ export class FastMossIntegration {
527
539
  const market = normalizeMarket(input.market);
528
540
  this.message = `正在自动进入 ${market} ${ranking.label}`;
529
541
  await this.page.goto(fastMossRankingUrl(ranking.source, market), { waitUntil: "domcontentloaded", timeout: 60_000 });
530
- const deadline = Date.now() + PRODUCT_TABLE_TIMEOUT_MS;
542
+ let deadline = Date.now() + PRODUCT_TABLE_TIMEOUT_MS;
531
543
  while (Date.now() < deadline) {
532
544
  const snapshot = await readVisibleTableSnapshot(this.page);
533
545
  if (hasReadableProductRows(snapshot)) {
@@ -536,8 +548,11 @@ export class FastMossIntegration {
536
548
  return;
537
549
  }
538
550
  await this.inspect();
539
- if (this.verificationRequired)
540
- throw new Error("检测到 FastMoss 滑块验证,请在专用窗口中手动完成后再次抓取");
551
+ if (this.verificationRequired) {
552
+ await this.waitForManualVerification();
553
+ deadline = Date.now() + PRODUCT_TABLE_TIMEOUT_MS;
554
+ continue;
555
+ }
541
556
  if (this.membershipExpired)
542
557
  throw new Error("FastMoss 会员已过期或当前账号没有商品榜单权限,请先更换账号");
543
558
  if (!this.authenticated)
@@ -546,6 +561,28 @@ export class FastMossIntegration {
546
561
  }
547
562
  throw new Error("FastMoss 商品榜单自动加载超时;请检查网络或在窗口中完成人工验证后重试");
548
563
  }
564
+ async waitForManualVerification() {
565
+ if (!this.verificationRequired)
566
+ return;
567
+ this.phase = "verification_required";
568
+ this.message = "采集已暂停等待人工滑块;完成后会自动继续,无需刷新或重新点击";
569
+ await this.page?.bringToFront().catch(() => undefined);
570
+ await waitForFastMossVerificationClear(async () => {
571
+ const status = await this.inspect();
572
+ if (status.verificationRequired)
573
+ this.message = "采集已暂停等待人工滑块;完成后会自动继续,无需刷新或重新点击";
574
+ return status;
575
+ }, {
576
+ timeoutMs: MANUAL_VERIFICATION_TIMEOUT_MS,
577
+ sleep: (milliseconds) => this.page && !this.page.isClosed()
578
+ ? this.page.waitForTimeout(milliseconds)
579
+ : new Promise((resolve) => setTimeout(resolve, milliseconds)),
580
+ });
581
+ this.phase = "ready";
582
+ this.message = "人工验证已完成,正在自动继续采集";
583
+ await this.page?.waitForLoadState("domcontentloaded", { timeout: 10_000 }).catch(() => undefined);
584
+ await this.page?.waitForTimeout(1_000).catch(() => undefined);
585
+ }
549
586
  async openVideoLearningPage(url, label) {
550
587
  if (!this.page || this.page.isClosed())
551
588
  throw new Error("FastMoss 专用浏览器未打开");
@@ -722,6 +759,24 @@ export class FastMossIntegration {
722
759
  });
723
760
  }
724
761
  }
762
+ export async function waitForFastMossVerificationClear(inspect, options = {}) {
763
+ const timeoutMs = Math.max(1, Number(options.timeoutMs || MANUAL_VERIFICATION_TIMEOUT_MS));
764
+ const pollMs = Math.max(1, Number(options.pollMs || 1_000));
765
+ const sleep = options.sleep || ((milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds)));
766
+ const deadline = Date.now() + timeoutMs;
767
+ while (Date.now() < deadline) {
768
+ const status = await inspect();
769
+ if (status.membershipExpired)
770
+ throw new Error("FastMoss 会员已过期或当前账号没有商品榜单权限,请先更换账号");
771
+ if (!status.verificationRequired) {
772
+ if (!status.authenticated)
773
+ throw new Error("FastMoss 登录已失效,请在专用窗口中重新登录后再次抓取");
774
+ return;
775
+ }
776
+ await sleep(pollMs);
777
+ }
778
+ throw new Error("等待人工滑块超过 10 分钟,本次采集已停止;完成验证后可重新开始,已保存结果不会丢失");
779
+ }
725
780
  export function videoEvidenceTimestamps(durationMs) {
726
781
  const maximumFrames = 24;
727
782
  const safeDuration = Math.max(1_000, Math.min(30 * 60_000, Math.round(Number(durationMs) || 0)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiaohhhh1/canvas-agent",
3
- "version": "0.4.40",
3
+ "version": "0.4.41",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",