@jackwener/opencli 0.6.3 → 0.7.0

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.
Files changed (63) hide show
  1. package/README.md +1 -1
  2. package/README.zh-CN.md +1 -1
  3. package/SKILL.md +7 -2
  4. package/dist/build-manifest.js +2 -0
  5. package/dist/cli-manifest.json +604 -24
  6. package/dist/clis/reddit/comment.d.ts +1 -0
  7. package/dist/clis/reddit/comment.js +57 -0
  8. package/dist/clis/reddit/popular.yaml +40 -0
  9. package/dist/clis/reddit/read.yaml +76 -0
  10. package/dist/clis/reddit/save.d.ts +1 -0
  11. package/dist/clis/reddit/save.js +51 -0
  12. package/dist/clis/reddit/saved.d.ts +1 -0
  13. package/dist/clis/reddit/saved.js +46 -0
  14. package/dist/clis/reddit/search.yaml +37 -11
  15. package/dist/clis/reddit/subreddit.yaml +14 -4
  16. package/dist/clis/reddit/subscribe.d.ts +1 -0
  17. package/dist/clis/reddit/subscribe.js +50 -0
  18. package/dist/clis/reddit/upvote.d.ts +1 -0
  19. package/dist/clis/reddit/upvote.js +64 -0
  20. package/dist/clis/reddit/upvoted.d.ts +1 -0
  21. package/dist/clis/reddit/upvoted.js +46 -0
  22. package/dist/clis/reddit/user-comments.yaml +45 -0
  23. package/dist/clis/reddit/user-posts.yaml +43 -0
  24. package/dist/clis/reddit/user.yaml +39 -0
  25. package/dist/clis/twitter/article.d.ts +1 -0
  26. package/dist/clis/twitter/article.js +157 -0
  27. package/dist/clis/twitter/bookmark.d.ts +1 -0
  28. package/dist/clis/twitter/bookmark.js +63 -0
  29. package/dist/clis/twitter/follow.d.ts +1 -0
  30. package/dist/clis/twitter/follow.js +65 -0
  31. package/dist/clis/twitter/profile.js +110 -42
  32. package/dist/clis/twitter/thread.d.ts +1 -0
  33. package/dist/clis/twitter/thread.js +150 -0
  34. package/dist/clis/twitter/unbookmark.d.ts +1 -0
  35. package/dist/clis/twitter/unbookmark.js +62 -0
  36. package/dist/clis/twitter/unfollow.d.ts +1 -0
  37. package/dist/clis/twitter/unfollow.js +71 -0
  38. package/dist/main.js +31 -8
  39. package/dist/registry.d.ts +1 -0
  40. package/package.json +1 -1
  41. package/src/build-manifest.ts +3 -0
  42. package/src/clis/reddit/comment.ts +60 -0
  43. package/src/clis/reddit/popular.yaml +40 -0
  44. package/src/clis/reddit/read.yaml +76 -0
  45. package/src/clis/reddit/save.ts +54 -0
  46. package/src/clis/reddit/saved.ts +48 -0
  47. package/src/clis/reddit/search.yaml +37 -11
  48. package/src/clis/reddit/subreddit.yaml +14 -4
  49. package/src/clis/reddit/subscribe.ts +53 -0
  50. package/src/clis/reddit/upvote.ts +67 -0
  51. package/src/clis/reddit/upvoted.ts +48 -0
  52. package/src/clis/reddit/user-comments.yaml +45 -0
  53. package/src/clis/reddit/user-posts.yaml +43 -0
  54. package/src/clis/reddit/user.yaml +39 -0
  55. package/src/clis/twitter/article.ts +161 -0
  56. package/src/clis/twitter/bookmark.ts +67 -0
  57. package/src/clis/twitter/follow.ts +69 -0
  58. package/src/clis/twitter/profile.ts +113 -45
  59. package/src/clis/twitter/thread.ts +181 -0
  60. package/src/clis/twitter/unbookmark.ts +66 -0
  61. package/src/clis/twitter/unfollow.ts +75 -0
  62. package/src/main.ts +24 -5
  63. package/src/registry.ts +1 -0
@@ -0,0 +1,67 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+
3
+ cli({
4
+ site: 'reddit',
5
+ name: 'upvote',
6
+ description: 'Upvote or downvote a Reddit post',
7
+ domain: 'reddit.com',
8
+ strategy: Strategy.COOKIE,
9
+ browser: true,
10
+ args: [
11
+ { name: 'post_id', type: 'string', required: true, help: 'Post ID (e.g. 1abc123) or fullname (t3_xxx)' },
12
+ { name: 'direction', type: 'string', default: 'up', help: 'Vote direction: up, down, none' },
13
+ ],
14
+ columns: ['status', 'message'],
15
+ func: async (page, kwargs) => {
16
+ if (!page) throw new Error('Requires browser');
17
+
18
+ await page.goto('https://www.reddit.com');
19
+ await page.wait(3);
20
+
21
+ const result = await page.evaluate(`(async () => {
22
+ try {
23
+ let postId = ${JSON.stringify(kwargs.post_id)};
24
+ // Extract ID from URL if needed
25
+ const urlMatch = postId.match(/comments\\/([a-z0-9]+)/);
26
+ if (urlMatch) postId = urlMatch[1];
27
+ // Build fullname
28
+ const fullname = postId.startsWith('t3_') || postId.startsWith('t1_')
29
+ ? postId : 't3_' + postId;
30
+
31
+ const dir = ${JSON.stringify(kwargs.direction)};
32
+ const direction = dir === 'down' ? -1 : dir === 'none' ? 0 : 1;
33
+
34
+ // Get modhash from Reddit config
35
+ const configEl = document.getElementById('config');
36
+ let modhash = '';
37
+ if (configEl) {
38
+ modhash = configEl.querySelector('[name="uh"]')?.getAttribute('content') || '';
39
+ }
40
+ if (!modhash) {
41
+ // Try fetching from /api/me.json
42
+ const meRes = await fetch('/api/me.json', { credentials: 'include' });
43
+ const me = await meRes.json();
44
+ modhash = me?.data?.modhash || '';
45
+ }
46
+
47
+ const res = await fetch('/api/vote', {
48
+ method: 'POST',
49
+ credentials: 'include',
50
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
51
+ body: 'id=' + encodeURIComponent(fullname)
52
+ + '&dir=' + direction
53
+ + (modhash ? '&uh=' + encodeURIComponent(modhash) : ''),
54
+ });
55
+
56
+ if (!res.ok) return { ok: false, message: 'HTTP ' + res.status };
57
+
58
+ const labels = { '1': 'Upvoted', '-1': 'Downvoted', '0': 'Vote removed' };
59
+ return { ok: true, message: (labels[String(direction)] || 'Voted') + ' ' + fullname };
60
+ } catch (e) {
61
+ return { ok: false, message: e.toString() };
62
+ }
63
+ })()`);
64
+
65
+ return [{ status: result.ok ? 'success' : 'failed', message: result.message }];
66
+ }
67
+ });
@@ -0,0 +1,48 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+
3
+ cli({
4
+ site: 'reddit',
5
+ name: 'upvoted',
6
+ description: 'Browse your upvoted Reddit posts',
7
+ domain: 'reddit.com',
8
+ strategy: Strategy.COOKIE,
9
+ browser: true,
10
+ args: [
11
+ { name: 'limit', type: 'int', default: 15 },
12
+ ],
13
+ columns: ['title', 'subreddit', 'score', 'comments', 'url'],
14
+ func: async (page, kwargs) => {
15
+ if (!page) throw new Error('Requires browser');
16
+
17
+ await page.goto('https://www.reddit.com');
18
+ await page.wait(3);
19
+
20
+ const result = await page.evaluate(`(async () => {
21
+ try {
22
+ // Get current username
23
+ const meRes = await fetch('/api/me.json?raw_json=1', { credentials: 'include' });
24
+ const me = await meRes.json();
25
+ const username = me?.name || me?.data?.name;
26
+ if (!username) return { error: 'Not logged in — cannot determine username' };
27
+
28
+ const limit = ${kwargs.limit};
29
+ const res = await fetch('/user/' + username + '/upvoted.json?limit=' + limit + '&raw_json=1', {
30
+ credentials: 'include'
31
+ });
32
+ const d = await res.json();
33
+ return (d?.data?.children || []).map(c => ({
34
+ title: c.data.title || '-',
35
+ subreddit: c.data.subreddit_name_prefixed || 'r/' + (c.data.subreddit || '?'),
36
+ score: c.data.score || 0,
37
+ comments: c.data.num_comments || 0,
38
+ url: 'https://www.reddit.com' + (c.data.permalink || ''),
39
+ }));
40
+ } catch (e) {
41
+ return { error: e.toString() };
42
+ }
43
+ })()`);
44
+
45
+ if (result?.error) throw new Error(result.error);
46
+ return (result || []).slice(0, kwargs.limit);
47
+ }
48
+ });
@@ -0,0 +1,45 @@
1
+ site: reddit
2
+ name: user-comments
3
+ description: View a Reddit user's comment history
4
+ domain: reddit.com
5
+ strategy: cookie
6
+ browser: true
7
+
8
+ args:
9
+ username:
10
+ type: string
11
+ required: true
12
+ limit:
13
+ type: int
14
+ default: 15
15
+
16
+ columns: [subreddit, score, body, url]
17
+
18
+ pipeline:
19
+ - navigate: https://www.reddit.com
20
+ - evaluate: |
21
+ (async () => {
22
+ const username = ${{ args.username | json }};
23
+ const name = username.startsWith('u/') ? username.slice(2) : username;
24
+ const limit = ${{ args.limit }};
25
+ const res = await fetch('/user/' + name + '/comments.json?limit=' + limit + '&raw_json=1', {
26
+ credentials: 'include'
27
+ });
28
+ const d = await res.json();
29
+ return (d?.data?.children || []).map(c => {
30
+ let body = c.data.body || '';
31
+ if (body.length > 300) body = body.slice(0, 300) + '...';
32
+ return {
33
+ subreddit: c.data.subreddit_name_prefixed,
34
+ score: c.data.score,
35
+ body: body,
36
+ url: 'https://www.reddit.com' + c.data.permalink,
37
+ };
38
+ });
39
+ })()
40
+ - map:
41
+ subreddit: ${{ item.subreddit }}
42
+ score: ${{ item.score }}
43
+ body: ${{ item.body }}
44
+ url: ${{ item.url }}
45
+ - limit: ${{ args.limit }}
@@ -0,0 +1,43 @@
1
+ site: reddit
2
+ name: user-posts
3
+ description: View a Reddit user's submitted posts
4
+ domain: reddit.com
5
+ strategy: cookie
6
+ browser: true
7
+
8
+ args:
9
+ username:
10
+ type: string
11
+ required: true
12
+ limit:
13
+ type: int
14
+ default: 15
15
+
16
+ columns: [title, subreddit, score, comments, url]
17
+
18
+ pipeline:
19
+ - navigate: https://www.reddit.com
20
+ - evaluate: |
21
+ (async () => {
22
+ const username = ${{ args.username | json }};
23
+ const name = username.startsWith('u/') ? username.slice(2) : username;
24
+ const limit = ${{ args.limit }};
25
+ const res = await fetch('/user/' + name + '/submitted.json?limit=' + limit + '&raw_json=1', {
26
+ credentials: 'include'
27
+ });
28
+ const d = await res.json();
29
+ return (d?.data?.children || []).map(c => ({
30
+ title: c.data.title,
31
+ subreddit: c.data.subreddit_name_prefixed,
32
+ score: c.data.score,
33
+ comments: c.data.num_comments,
34
+ url: 'https://www.reddit.com' + c.data.permalink,
35
+ }));
36
+ })()
37
+ - map:
38
+ title: ${{ item.title }}
39
+ subreddit: ${{ item.subreddit }}
40
+ score: ${{ item.score }}
41
+ comments: ${{ item.comments }}
42
+ url: ${{ item.url }}
43
+ - limit: ${{ args.limit }}
@@ -0,0 +1,39 @@
1
+ site: reddit
2
+ name: user
3
+ description: View a Reddit user profile
4
+ domain: reddit.com
5
+ strategy: cookie
6
+ browser: true
7
+
8
+ args:
9
+ username:
10
+ type: string
11
+ required: true
12
+
13
+ columns: [field, value]
14
+
15
+ pipeline:
16
+ - navigate: https://www.reddit.com
17
+ - evaluate: |
18
+ (async () => {
19
+ const username = ${{ args.username | json }};
20
+ const name = username.startsWith('u/') ? username.slice(2) : username;
21
+ const res = await fetch('/user/' + name + '/about.json?raw_json=1', {
22
+ credentials: 'include'
23
+ });
24
+ const d = await res.json();
25
+ const u = d?.data || d || {};
26
+ const created = u.created_utc ? new Date(u.created_utc * 1000).toISOString().split('T')[0] : '-';
27
+ return [
28
+ { field: 'Username', value: 'u/' + (u.name || name) },
29
+ { field: 'Post Karma', value: String(u.link_karma || 0) },
30
+ { field: 'Comment Karma', value: String(u.comment_karma || 0) },
31
+ { field: 'Total Karma', value: String(u.total_karma || (u.link_karma||0) + (u.comment_karma||0)) },
32
+ { field: 'Account Created', value: created },
33
+ { field: 'Gold', value: u.is_gold ? '⭐ Yes' : 'No' },
34
+ { field: 'Verified', value: u.verified ? '✅ Yes' : 'No' },
35
+ ];
36
+ })()
37
+ - map:
38
+ field: ${{ item.field }}
39
+ value: ${{ item.value }}
@@ -0,0 +1,161 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+
3
+ cli({
4
+ site: 'twitter',
5
+ name: 'article',
6
+ description: 'Fetch a Twitter Article (long-form content) and export as Markdown',
7
+ domain: 'x.com',
8
+ strategy: Strategy.COOKIE,
9
+ browser: true,
10
+ args: [
11
+ { name: 'tweet_id', type: 'string', positional: true, required: true, help: 'Tweet ID or URL containing the article' },
12
+ ],
13
+ columns: ['title', 'author', 'content', 'url'],
14
+ func: async (page, kwargs) => {
15
+ // Extract tweet ID from URL if needed
16
+ let tweetId = kwargs.tweet_id;
17
+ const urlMatch = tweetId.match(/\/(?:status|article)\/(\d+)/);
18
+ if (urlMatch) tweetId = urlMatch[1];
19
+
20
+ // Navigate to the tweet page for cookie context
21
+ await page.goto(`https://x.com/i/status/${tweetId}`);
22
+ await page.wait(3);
23
+
24
+ const result = await page.evaluate(`
25
+ async () => {
26
+ const tweetId = "${tweetId}";
27
+ const ct0 = document.cookie.split(';').map(c=>c.trim()).find(c=>c.startsWith('ct0='))?.split('=')[1];
28
+ if (!ct0) return {error: 'No ct0 cookie — not logged into x.com'};
29
+
30
+ const bearer = 'AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA';
31
+ const headers = {
32
+ 'Authorization': 'Bearer ' + decodeURIComponent(bearer),
33
+ 'X-Csrf-Token': ct0,
34
+ 'X-Twitter-Auth-Type': 'OAuth2Session',
35
+ 'X-Twitter-Active-User': 'yes'
36
+ };
37
+
38
+ const variables = JSON.stringify({
39
+ tweetId: tweetId,
40
+ withCommunity: false,
41
+ includePromotedContent: false,
42
+ withVoice: false,
43
+ });
44
+ const features = JSON.stringify({
45
+ longform_notetweets_consumption_enabled: true,
46
+ responsive_web_twitter_article_tweet_consumption_enabled: true,
47
+ longform_notetweets_rich_text_read_enabled: true,
48
+ longform_notetweets_inline_media_enabled: true,
49
+ articles_preview_enabled: true,
50
+ responsive_web_graphql_exclude_directive_enabled: true,
51
+ verified_phone_label_enabled: false,
52
+ });
53
+ const fieldToggles = JSON.stringify({
54
+ withArticleRichContentState: true,
55
+ withArticlePlainText: true,
56
+ });
57
+
58
+ // Dynamically resolve queryId: GitHub community source → JS bundle scan → hardcoded fallback
59
+ async function resolveQueryId(operationName, fallbackId) {
60
+ try {
61
+ const ghResp = await fetch('https://raw.githubusercontent.com/fa0311/twitter-openapi/refs/heads/main/src/config/placeholder.json');
62
+ if (ghResp.ok) {
63
+ const data = await ghResp.json();
64
+ const entry = data[operationName];
65
+ if (entry && entry.queryId) return entry.queryId;
66
+ }
67
+ } catch {}
68
+ try {
69
+ const scripts = performance.getEntriesByType('resource')
70
+ .filter(r => r.name.includes('client-web') && r.name.endsWith('.js'))
71
+ .map(r => r.name);
72
+ for (const scriptUrl of scripts.slice(0, 15)) {
73
+ try {
74
+ const text = await (await fetch(scriptUrl)).text();
75
+ const re = new RegExp('queryId:"([A-Za-z0-9_-]+)"[^}]{0,200}operationName:"' + operationName + '"');
76
+ const m = text.match(re);
77
+ if (m) return m[1];
78
+ } catch {}
79
+ }
80
+ } catch {}
81
+ return fallbackId;
82
+ }
83
+
84
+ const queryId = await resolveQueryId('TweetResultByRestId', '7xflPyRiUxGVbJd4uWmbfg');
85
+ const url = '/i/api/graphql/' + queryId + '/TweetResultByRestId?variables='
86
+ + encodeURIComponent(variables)
87
+ + '&features=' + encodeURIComponent(features)
88
+ + '&fieldToggles=' + encodeURIComponent(fieldToggles);
89
+
90
+ const resp = await fetch(url, {headers, credentials: 'include'});
91
+ if (!resp.ok) return {error: 'HTTP ' + resp.status, hint: 'Tweet may not exist or queryId expired'};
92
+ const d = await resp.json();
93
+
94
+ const result = d.data?.tweetResult?.result;
95
+ if (!result) return {error: 'Article not found'};
96
+
97
+ // Unwrap TweetWithVisibilityResults
98
+ const tw = result.tweet || result;
99
+ const legacy = tw.legacy || {};
100
+ const user = tw.core?.user_results?.result;
101
+ const screenName = user?.legacy?.screen_name || user?.core?.screen_name || 'unknown';
102
+
103
+ // Extract article content
104
+ const articleResults = tw.article?.article_results?.result;
105
+ if (!articleResults) {
106
+ // Fallback: return note_tweet text if present
107
+ const noteText = tw.note_tweet?.note_tweet_results?.result?.text;
108
+ if (noteText) {
109
+ return [{
110
+ title: '(Note Tweet)',
111
+ author: screenName,
112
+ content: noteText,
113
+ url: 'https://x.com/' + screenName + '/status/' + tweetId,
114
+ }];
115
+ }
116
+ return {error: 'Tweet ' + tweetId + ' has no article content'};
117
+ }
118
+
119
+ const title = articleResults.title || '(Untitled)';
120
+ const contentState = articleResults.content_state || {};
121
+ const blocks = contentState.blocks || [];
122
+
123
+ // Convert draft.js blocks to Markdown
124
+ const parts = [];
125
+ let orderedCounter = 0;
126
+ for (const block of blocks) {
127
+ const blockType = block.type || 'unstyled';
128
+ if (blockType === 'atomic') continue;
129
+ const text = block.text || '';
130
+ if (!text) continue;
131
+ if (blockType !== 'ordered-list-item') orderedCounter = 0;
132
+
133
+ if (blockType === 'header-one') parts.push('# ' + text);
134
+ else if (blockType === 'header-two') parts.push('## ' + text);
135
+ else if (blockType === 'header-three') parts.push('### ' + text);
136
+ else if (blockType === 'blockquote') parts.push('> ' + text);
137
+ else if (blockType === 'unordered-list-item') parts.push('- ' + text);
138
+ else if (blockType === 'ordered-list-item') {
139
+ orderedCounter++;
140
+ parts.push(orderedCounter + '. ' + text);
141
+ }
142
+ else if (blockType === 'code-block') parts.push('\`\`\`\\n' + text + '\\n\`\`\`');
143
+ else parts.push(text);
144
+ }
145
+
146
+ return [{
147
+ title,
148
+ author: screenName,
149
+ content: parts.join('\\n\\n') || legacy.full_text || '',
150
+ url: 'https://x.com/' + screenName + '/status/' + tweetId,
151
+ }];
152
+ }
153
+ `);
154
+
155
+ if (result?.error) {
156
+ throw new Error(result.error + (result.hint ? ` (${result.hint})` : ''));
157
+ }
158
+
159
+ return result || [];
160
+ }
161
+ });
@@ -0,0 +1,67 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+ import type { IPage } from '../../types.js';
3
+
4
+ cli({
5
+ site: 'twitter',
6
+ name: 'bookmark',
7
+ description: 'Bookmark a tweet',
8
+ domain: 'x.com',
9
+ strategy: Strategy.UI,
10
+ browser: true,
11
+ args: [
12
+ { name: 'url', type: 'string', positional: true, required: true, help: 'Tweet URL to bookmark' },
13
+ ],
14
+ columns: ['status', 'message'],
15
+ func: async (page: IPage | null, kwargs: any) => {
16
+ if (!page) throw new Error('Requires browser');
17
+
18
+ await page.goto(kwargs.url);
19
+ await page.wait(5);
20
+
21
+ const result = await page.evaluate(`(async () => {
22
+ try {
23
+ let attempts = 0;
24
+ let bookmarkBtn = null;
25
+ let removeBtn = null;
26
+
27
+ while (attempts < 20) {
28
+ // Check if already bookmarked
29
+ removeBtn = document.querySelector('[data-testid="removeBookmark"]');
30
+ if (removeBtn) {
31
+ return { ok: true, message: 'Tweet is already bookmarked.' };
32
+ }
33
+
34
+ bookmarkBtn = document.querySelector('[data-testid="bookmark"]');
35
+ if (bookmarkBtn) break;
36
+
37
+ await new Promise(r => setTimeout(r, 500));
38
+ attempts++;
39
+ }
40
+
41
+ if (!bookmarkBtn) {
42
+ return { ok: false, message: 'Could not find Bookmark button. Are you logged in?' };
43
+ }
44
+
45
+ bookmarkBtn.click();
46
+ await new Promise(r => setTimeout(r, 1000));
47
+
48
+ // Verify
49
+ const verify = document.querySelector('[data-testid="removeBookmark"]');
50
+ if (verify) {
51
+ return { ok: true, message: 'Tweet successfully bookmarked.' };
52
+ } else {
53
+ return { ok: false, message: 'Bookmark action initiated but UI did not update.' };
54
+ }
55
+ } catch (e) {
56
+ return { ok: false, message: e.toString() };
57
+ }
58
+ })()`);
59
+
60
+ if (result.ok) await page.wait(2);
61
+
62
+ return [{
63
+ status: result.ok ? 'success' : 'failed',
64
+ message: result.message
65
+ }];
66
+ }
67
+ });
@@ -0,0 +1,69 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+ import type { IPage } from '../../types.js';
3
+
4
+ cli({
5
+ site: 'twitter',
6
+ name: 'follow',
7
+ description: 'Follow a Twitter user',
8
+ domain: 'x.com',
9
+ strategy: Strategy.UI,
10
+ browser: true,
11
+ args: [
12
+ { name: 'username', type: 'string', positional: true, required: true, help: 'Twitter screen name (without @)' },
13
+ ],
14
+ columns: ['status', 'message'],
15
+ func: async (page: IPage | null, kwargs: any) => {
16
+ if (!page) throw new Error('Requires browser');
17
+ const username = kwargs.username.replace(/^@/, '');
18
+
19
+ await page.goto(`https://x.com/${username}`);
20
+ await page.wait(5);
21
+
22
+ const result = await page.evaluate(`(async () => {
23
+ try {
24
+ let attempts = 0;
25
+ let followBtn = null;
26
+ let unfollowTestId = null;
27
+
28
+ while (attempts < 20) {
29
+ // Check if already following (button shows screen_name-unfollow)
30
+ unfollowTestId = document.querySelector('[data-testid$="-unfollow"]');
31
+ if (unfollowTestId) {
32
+ return { ok: true, message: 'Already following @${username}.' };
33
+ }
34
+
35
+ // Look for the Follow button
36
+ followBtn = document.querySelector('[data-testid$="-follow"]');
37
+ if (followBtn) break;
38
+
39
+ await new Promise(r => setTimeout(r, 500));
40
+ attempts++;
41
+ }
42
+
43
+ if (!followBtn) {
44
+ return { ok: false, message: 'Could not find Follow button. Are you logged in?' };
45
+ }
46
+
47
+ followBtn.click();
48
+ await new Promise(r => setTimeout(r, 1500));
49
+
50
+ // Verify
51
+ const verify = document.querySelector('[data-testid$="-unfollow"]');
52
+ if (verify) {
53
+ return { ok: true, message: 'Successfully followed @${username}.' };
54
+ } else {
55
+ return { ok: false, message: 'Follow action initiated but UI did not update.' };
56
+ }
57
+ } catch (e) {
58
+ return { ok: false, message: e.toString() };
59
+ }
60
+ })()`);
61
+
62
+ if (result.ok) await page.wait(2);
63
+
64
+ return [{
65
+ status: result.ok ? 'success' : 'failed',
66
+ message: result.message
67
+ }];
68
+ }
69
+ });