tt-help-cli-ycl 1.3.60 → 1.3.61

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tt-help-cli-ycl",
3
- "version": "1.3.60",
3
+ "version": "1.3.61",
4
4
  "description": "TikTok user & video data scraper - extract ttSeller, verified, locationCreated from HTML source",
5
5
  "type": "module",
6
6
  "bin": {
@@ -66,45 +66,39 @@ export async function withBrowserRecovery(fn, browser, page, cdpOptions, port) {
66
66
  }
67
67
  }
68
68
 
69
+ const DOM_CHECK_TIMEOUT = 20000; // 单次 DOM 检测超时 20 秒
70
+ const DOM_CHECK_RETRIES = 3; // DOM 检测最大重试次数
71
+
69
72
  /**
70
73
  * 判断登录状态:Cookie 为主,DOM 验真为辅。
71
74
  * - 无 sessionid Cookie → 未登录
72
- * - sessionid Cookie 已过期 → 未登录
73
75
  * - 有 Cookie + DOM 确认已登录 → 已登录
74
76
  * - 有 Cookie + DOM 确认未登录(出现登录按钮)→ 未登录
75
- * - 有 Cookie + DOM 无法判断(超时/元素未找到)→ 信任 Cookie,判定已登录
77
+ * - 有 Cookie + DOM 无法判断(超时/元素未找到),重试后仍无法判断 信任 Cookie,判定已登录
76
78
  */
77
79
  export async function isLoggedIn(page) {
78
80
  const cookies = await page.context().cookies("https://www.tiktok.com");
79
- const sessionCookie = cookies.find((c) => c.name === "sessionid");
80
-
81
- if (!sessionCookie) {
82
- console.error("[登录检测] sessionid Cookie,判定未登录");
83
- return false;
84
- }
85
-
86
- // Cookie 过期检查:TikTok sessionid 可能是 session cookie(expires=-1)
87
- // 只有当 expires > 0 且已过期时才判定无效
88
- if (
89
- sessionCookie.expires > 0 &&
90
- sessionCookie.expires < Math.floor(Date.now() / 1000)
91
- ) {
92
- console.error("[登录检测] sessionid Cookie 已过期,判定未登录");
93
- return false;
94
- }
95
-
96
- const domResult = await isLoggedInByDom(page);
97
- // domResult: true=已登录, false=明确未登录, null=无法判断
98
- if (domResult === true) {
99
- console.error("[登录检测] DOM 验真确认已登录");
100
- return true;
101
- }
102
- if (domResult === false) {
103
- console.error("[登录检测] DOM 验真发现登录按钮,判定未登录(Cookie 可能已失效)");
104
- return false;
81
+ const hasSessionId = cookies.some((c) => c.name === "sessionid");
82
+ if (!hasSessionId) return false;
83
+
84
+ // 重试 DOM 检测,直到得到明确结果或耗尽重试次数
85
+ for (let attempt = 1; attempt <= DOM_CHECK_RETRIES; attempt++) {
86
+ const domResult = await isLoggedInByDom(page);
87
+ // domResult: true=已登录, false=明确未登录, null=无法判断
88
+ if (domResult === true) return true;
89
+ if (domResult === false) return false;
90
+ // null: DOM 无法判断,刷新页面后重试
91
+ if (attempt < DOM_CHECK_RETRIES) {
92
+ console.error(
93
+ ` [登录检测] DOM 无法判断,刷新页面后重试 (${attempt}/${DOM_CHECK_RETRIES})...`,
94
+ );
95
+ await page.reload({ waitUntil: "domcontentloaded" });
96
+ }
105
97
  }
106
- // null: DOM 无法判断,信任 Cookie
107
- console.error("[登录检测] DOM 验真超时,信任 Cookie 判定已登录");
98
+ // 重试后仍无法判断,信任 Cookie
99
+ console.error(
100
+ ` [登录检测] DOM 检测 ${DOM_CHECK_RETRIES} 次均未明确结果,信任 Cookie 判定为已登录`,
101
+ );
108
102
  return true;
109
103
  }
110
104
 
@@ -124,31 +118,13 @@ export async function isLoggedInByDom(page) {
124
118
  'button:has-text("Sign in")',
125
119
  ].join(", ");
126
120
 
127
- // 分阶段等待:先 5 秒,超时再重试 10 秒
128
- const timeouts = [5000, 10000];
129
- let selectorFound = false;
130
-
131
- for (let i = 0; i < timeouts.length; i++) {
132
- const found = await page
133
- .waitForSelector(loginOrLoggedInSelector, { timeout: timeouts[i] })
134
- .then(() => true)
135
- .catch(() => false);
136
-
137
- if (found) {
138
- selectorFound = true;
139
- break;
140
- }
141
- // 非最后一次,输出重试日志
142
- if (i < timeouts.length - 1) {
143
- console.error(
144
- `[登录检测] DOM 元素等待超时 (${timeouts[i]}ms),重试中...`,
145
- );
146
- }
147
- }
121
+ const selectorFound = await page
122
+ .waitForSelector(loginOrLoggedInSelector, { timeout: DOM_CHECK_TIMEOUT })
123
+ .then(() => true)
124
+ .catch(() => false);
148
125
 
149
126
  if (!selectorFound) {
150
- // 所有阶段都超时,DOM 无法判断
151
- console.error(`[登录检测] DOM 验真超时 (${timeouts[timeouts.length - 1]}ms),无法判断`);
127
+ // 等待超时,DOM 无法判断
152
128
  return null;
153
129
  }
154
130