@xkwdstore/agent 2.0.2 → 2.0.3

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,83 +1,83 @@
1
- 'use strict';
2
-
3
- /**
4
- * Follow action — follow a user on X.
5
- * Ported from backend/routes/browser.js doFollow().
6
- */
7
-
8
- const { sleep, humanDelay, randDelay, naturalNavigate, humanPreAction, humanClick, detectStage } = require('./helpers');
9
-
10
- // Follow button labels in multiple languages
11
- const FOLLOW_LABELS = [
12
- 'follow', 'seguir', 'suivre', 'folgen', 'تابع', 'フォロー',
13
- 'segui', 'volgen', 'śledź', 'seguir', '关注', '팔로우',
14
- ];
15
-
16
- const FOLLOWING_LABELS = [
17
- 'following', 'siguiendo', 'abonné', 'folgt', 'يتابعك', 'フォロー中',
18
- 'following', 'volgend', 'obserwujesz', 'seguindo', '已关注', '팔로잉',
19
- 'unfollow', 'dejar de seguir', 'ne plus suivre', 'entfolgen', 'إلغاء المتابعة',
20
- ];
21
-
22
- async function follow(page, payload) {
23
- const { username } = payload;
24
- if (!username) throw new Error('username required');
25
-
26
- const cleanUsername = String(username).replace(/^@/, '').replace(/^.*x\.com\//, '');
27
- await naturalNavigate(page, `https://x.com/${cleanUsername}`);
28
- await humanPreAction(page);
29
-
30
- // Check if page loaded properly
31
- if (page.url().includes('/login') || page.url().includes('/i/flow')) {
32
- throw new Error('Not logged in — open browser and login first');
33
- }
34
-
35
- // Check for suspended/not-found
36
- const title = await page.title().catch(() => '');
37
- const bodyText = await page.evaluate(() => document.body.innerText).catch(() => '');
38
- if (/account suspended|doesn't exist|page doesn't exist/i.test(title + bodyText)) {
39
- throw new Error(`Account @${cleanUsername} is suspended or not found`);
40
- }
41
-
42
- // Strategy 1: data-testid selectors
43
- const testIdSelectors = [
44
- `[data-testid="${cleanUsername}-follow"]`,
45
- '[data-testid="follow"]',
46
- ];
47
- for (const sel of testIdSelectors) {
48
- const btn = page.locator(sel).first();
49
- if (await btn.isVisible({ timeout: 2000 }).catch(() => false)) {
50
- await humanClick(page, btn);
51
- await sleep(3000);
52
- return { success: true, message: `Following @${cleanUsername}` };
53
- }
54
- }
55
-
56
- // Strategy 2: scan buttons for follow labels
57
- const buttons = page.locator('[role="button"], button');
58
- const count = await buttons.count();
59
- for (let i = 0; i < count; i++) {
60
- const btn = buttons.nth(i);
61
- const text = (await btn.innerText().catch(() => '')).toLowerCase().trim();
62
- if (FOLLOW_LABELS.some(label => text === label || text.includes(label))) {
63
- // Check if it's actually "following" (already following)
64
- if (FOLLOWING_LABELS.some(label => text.includes(label))) continue;
65
- await humanClick(page, btn);
66
- await sleep(3000);
67
- return { success: true, message: `Following @${cleanUsername}` };
68
- }
69
- }
70
-
71
- // Check if already following
72
- for (let i = 0; i < count; i++) {
73
- const btn = buttons.nth(i);
74
- const text = (await btn.innerText().catch(() => '')).toLowerCase().trim();
75
- if (FOLLOWING_LABELS.some(label => text.includes(label))) {
76
- return { success: true, message: `Already following @${cleanUsername}`, status: 'already_following' };
77
- }
78
- }
79
-
80
- throw new Error(`Could not find follow button for @${cleanUsername}`);
81
- }
82
-
1
+ 'use strict';
2
+
3
+ /**
4
+ * Follow action — follow a user on X.
5
+ * Ported from backend/routes/browser.js doFollow().
6
+ */
7
+
8
+ const { sleep, humanDelay, randDelay, naturalNavigate, humanPreAction, humanClick, detectStage } = require('./helpers');
9
+
10
+ // Follow button labels in multiple languages
11
+ const FOLLOW_LABELS = [
12
+ 'follow', 'seguir', 'suivre', 'folgen', 'تابع', 'フォロー',
13
+ 'segui', 'volgen', 'śledź', 'seguir', '关注', '팔로우',
14
+ ];
15
+
16
+ const FOLLOWING_LABELS = [
17
+ 'following', 'siguiendo', 'abonné', 'folgt', 'يتابعك', 'フォロー中',
18
+ 'following', 'volgend', 'obserwujesz', 'seguindo', '已关注', '팔로잉',
19
+ 'unfollow', 'dejar de seguir', 'ne plus suivre', 'entfolgen', 'إلغاء المتابعة',
20
+ ];
21
+
22
+ async function follow(page, payload) {
23
+ const { username } = payload;
24
+ if (!username) throw new Error('username required');
25
+
26
+ const cleanUsername = String(username).replace(/^@/, '').replace(/^.*x\.com\//, '');
27
+ await naturalNavigate(page, `https://x.com/${cleanUsername}`);
28
+ await humanPreAction(page);
29
+
30
+ // Check if page loaded properly
31
+ if (page.url().includes('/login') || page.url().includes('/i/flow')) {
32
+ throw new Error('Not logged in — open browser and login first');
33
+ }
34
+
35
+ // Check for suspended/not-found
36
+ const title = await page.title().catch(() => '');
37
+ const bodyText = await page.evaluate(() => document.body.innerText).catch(() => '');
38
+ if (/account suspended|doesn't exist|page doesn't exist/i.test(title + bodyText)) {
39
+ throw new Error(`Account @${cleanUsername} is suspended or not found`);
40
+ }
41
+
42
+ // Strategy 1: data-testid selectors
43
+ const testIdSelectors = [
44
+ `[data-testid="${cleanUsername}-follow"]`,
45
+ '[data-testid="follow"]',
46
+ ];
47
+ for (const sel of testIdSelectors) {
48
+ const btn = page.locator(sel).first();
49
+ if (await btn.isVisible({ timeout: 2000 }).catch(() => false)) {
50
+ await humanClick(page, btn);
51
+ await sleep(3000);
52
+ return { success: true, message: `Following @${cleanUsername}` };
53
+ }
54
+ }
55
+
56
+ // Strategy 2: scan buttons for follow labels
57
+ const buttons = page.locator('[role="button"], button');
58
+ const count = await buttons.count();
59
+ for (let i = 0; i < count; i++) {
60
+ const btn = buttons.nth(i);
61
+ const text = (await btn.innerText().catch(() => '')).toLowerCase().trim();
62
+ if (FOLLOW_LABELS.some(label => text === label || text.includes(label))) {
63
+ // Check if it's actually "following" (already following)
64
+ if (FOLLOWING_LABELS.some(label => text.includes(label))) continue;
65
+ await humanClick(page, btn);
66
+ await sleep(3000);
67
+ return { success: true, message: `Following @${cleanUsername}` };
68
+ }
69
+ }
70
+
71
+ // Check if already following
72
+ for (let i = 0; i < count; i++) {
73
+ const btn = buttons.nth(i);
74
+ const text = (await btn.innerText().catch(() => '')).toLowerCase().trim();
75
+ if (FOLLOWING_LABELS.some(label => text.includes(label))) {
76
+ return { success: true, message: `Already following @${cleanUsername}`, status: 'already_following' };
77
+ }
78
+ }
79
+
80
+ throw new Error(`Could not find follow button for @${cleanUsername}`);
81
+ }
82
+
83
83
  module.exports = { follow };