tail-fin-twitter 0.5.1

Twitter/X adapter for tail-fin: timeline, search, profile, bookmarks, likes, thread, post, like, follow, block, bookmark, reply, trending, lists, article, download, notifications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
use tail_fin_common::js::escape_js_string;
use tail_fin_common::page::navigate_and_eval;
use tail_fin_common::parse_action_result;
use tail_fin_common::BrowserSession;
use tail_fin_common::TailFinError;

use crate::types::{ActionResult, MediaItem, Trend};

pub(crate) async fn browser_post(
    session: &BrowserSession,
    text: &str,
) -> Result<ActionResult, TailFinError> {
    session.navigate("https://x.com/compose/tweet").await?;
    let _ = session.wait_for_network_idle(15000, 1000).await;
    tokio::time::sleep(std::time::Duration::from_secs(2)).await;

    let escaped = escape_js_string(text);
    let js = format!(
        r#"(async () => {{
            try {{
                const box = document.querySelector('[data-testid="tweetTextarea_0"]');
                if (!box) return JSON.stringify({{ status: "failed", message: "Could not find tweet composer." }});
                box.focus();
                const dt = new DataTransfer();
                dt.setData('text/plain', "{escaped}");
                box.dispatchEvent(new ClipboardEvent('paste', {{ clipboardData: dt, bubbles: true, cancelable: true }}));
                await new Promise(r => setTimeout(r, 1000));
                const btn = document.querySelector('[data-testid="tweetButton"]') || document.querySelector('[data-testid="tweetButtonInline"]');
                if (btn && !btn.disabled) {{ btn.click(); return JSON.stringify({{ status: "success", message: "Tweet posted." }}); }}
                return JSON.stringify({{ status: "failed", message: "Tweet button disabled or not found." }});
            }} catch (e) {{ return JSON.stringify({{ status: "failed", message: e.toString() }}); }}
        }})()"#
    );

    parse_action_result(session.eval(&js).await?)
}

pub(crate) async fn browser_like(
    session: &BrowserSession,
    tweet_url: &str,
) -> Result<ActionResult, TailFinError> {
    let js = r#"(async () => {
        try {
            for (let i = 0; i < 20; i++) {
                const unlike = document.querySelector('[data-testid="unlike"]');
                if (unlike) return JSON.stringify({ status: "success", message: "Tweet is already liked." });
                const btn = document.querySelector('[data-testid="like"]');
                if (btn) {
                    btn.click();
                    await new Promise(r => setTimeout(r, 1000));
                    const verify = document.querySelector('[data-testid="unlike"]');
                    if (verify) return JSON.stringify({ status: "success", message: "Tweet liked." });
                    return JSON.stringify({ status: "failed", message: "Like action did not register." });
                }
                await new Promise(r => setTimeout(r, 500));
            }
            return JSON.stringify({ status: "failed", message: "Like button not found." });
        } catch (e) { return JSON.stringify({ status: "failed", message: e.toString() }); }
    })()"#;

    parse_action_result(navigate_and_eval(session, tweet_url, 2, js).await?)
}

pub(crate) async fn browser_follow(
    session: &BrowserSession,
    username: &str,
    follow: bool,
) -> Result<ActionResult, TailFinError> {
    let username = username.trim_start_matches('@');
    session
        .navigate(&format!("https://x.com/{}", username))
        .await?;
    let _ = session.wait_for_network_idle(15000, 1000).await;
    tokio::time::sleep(std::time::Duration::from_secs(2)).await;

    let (target_testid, verify_testid, action_word) = if follow {
        ("-follow", "-unfollow", "followed")
    } else {
        ("-unfollow", "-follow", "unfollowed")
    };

    let js = format!(
        r#"(async () => {{
            try {{
                for (let i = 0; i < 20; i++) {{
                    const btn = document.querySelector('[data-testid$="{target_testid}"]');
                    if (btn) {{
                        btn.click();
                        await new Promise(r => setTimeout(r, 1500));
                        const confirm = document.querySelector('[data-testid="confirmationSheetConfirm"]');
                        if (confirm) {{ confirm.click(); await new Promise(r => setTimeout(r, 1000)); }}
                        return JSON.stringify({{ status: "success", message: "Successfully {action_word} @{username}." }});
                    }}
                    const verify = document.querySelector('[data-testid$="{verify_testid}"]');
                    if (verify) return JSON.stringify({{ status: "success", message: "Already {action_word} @{username}." }});
                    await new Promise(r => setTimeout(r, 500));
                }}
                return JSON.stringify({{ status: "failed", message: "Button not found." }});
            }} catch (e) {{ return JSON.stringify({{ status: "failed", message: e.toString() }}); }}
        }})()"#
    );

    parse_action_result(session.eval(&js).await?)
}

pub(crate) async fn browser_delete(
    session: &BrowserSession,
    tweet_url: &str,
) -> Result<ActionResult, TailFinError> {
    let js = r#"(async () => {
        try {
            const visible = (el) => !!el && (el.offsetParent !== null || el.getClientRects().length > 0);
            const articles = Array.from(document.querySelectorAll('article'));
            if (!articles.length) return JSON.stringify({ status: "failed", message: "Tweet not found on page." });

            for (const article of articles) {
                const buttons = Array.from(article.querySelectorAll('button,[role="button"]'));
                const moreMenu = buttons.find(el => visible(el) && (el.getAttribute('aria-label') || '').trim() === 'More');
                if (!moreMenu) continue;

                moreMenu.click();
                await new Promise(r => setTimeout(r, 1000));

                const items = Array.from(document.querySelectorAll('[role="menuitem"]'));
                const deleteBtn = items.find(item => {
                    const text = (item.textContent || '').trim();
                    return text.includes('Delete') && !text.includes('List');
                });
                if (!deleteBtn) {
                    document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
                    await new Promise(r => setTimeout(r, 500));
                    continue;
                }

                deleteBtn.click();
                await new Promise(r => setTimeout(r, 1000));

                const confirmBtn = document.querySelector('[data-testid="confirmationSheetConfirm"]');
                if (confirmBtn) {
                    confirmBtn.click();
                    return JSON.stringify({ status: "success", message: "Tweet deleted." });
                }
                return JSON.stringify({ status: "failed", message: "Delete confirmation did not appear." });
            }
            return JSON.stringify({ status: "failed", message: "Delete option not found. This tweet may not belong to you." });
        } catch (e) { return JSON.stringify({ status: "failed", message: e.toString() }); }
    })()"#;

    parse_action_result(navigate_and_eval(session, tweet_url, 2, js).await?)
}

pub(crate) async fn browser_block(
    session: &BrowserSession,
    username: &str,
    block: bool,
) -> Result<ActionResult, TailFinError> {
    let username = username.trim_start_matches('@');
    session
        .navigate(&format!("https://x.com/{}", username))
        .await?;
    let _ = session.wait_for_network_idle(15000, 1000).await;
    tokio::time::sleep(std::time::Duration::from_secs(2)).await;

    let js = if block {
        format!(
            r#"(async () => {{
                try {{
                    const blocked = document.querySelector('[data-testid$="-unblock"]');
                    if (blocked) return JSON.stringify({{ status: "success", message: "Already blocking @{username}." }});
                    const moreBtn = document.querySelector('[data-testid="userActions"]');
                    if (!moreBtn) return JSON.stringify({{ status: "failed", message: "User actions menu not found." }});
                    moreBtn.click();
                    await new Promise(r => setTimeout(r, 1000));
                    const items = document.querySelectorAll('[role="menuitem"]');
                    let blockItem = null;
                    for (const item of items) {{ if (item.textContent && item.textContent.includes('Block')) {{ blockItem = item; break; }} }}
                    if (!blockItem) return JSON.stringify({{ status: "failed", message: "Block option not found." }});
                    blockItem.click();
                    await new Promise(r => setTimeout(r, 1000));
                    const confirm = document.querySelector('[data-testid="confirmationSheetConfirm"]');
                    if (confirm) {{ confirm.click(); await new Promise(r => setTimeout(r, 1000)); }}
                    return JSON.stringify({{ status: "success", message: "Blocked @{username}." }});
                }} catch (e) {{ return JSON.stringify({{ status: "failed", message: e.toString() }}); }}
            }})()"#
        )
    } else {
        format!(
            r#"(async () => {{
                try {{
                    for (let i = 0; i < 20; i++) {{
                        const followBtn = document.querySelector('[data-testid="{username}-follow"]');
                        if (followBtn) return JSON.stringify({{ status: "success", message: "Not blocking @{username} (already unblocked)." }});
                        const unblockBtn = document.querySelector('[data-testid="{username}-unblock"]') || document.querySelector('[data-testid$="-unblock"]');
                        if (unblockBtn) {{
                            unblockBtn.click();
                            await new Promise(r => setTimeout(r, 1000));
                            const confirm = document.querySelector('[data-testid="confirmationSheetConfirm"]');
                            if (confirm) {{ confirm.click(); await new Promise(r => setTimeout(r, 1000)); }}
                            return JSON.stringify({{ status: "success", message: "Unblocked @{username}." }});
                        }}
                        await new Promise(r => setTimeout(r, 500));
                    }}
                    return JSON.stringify({{ status: "failed", message: "Unblock button not found." }});
                }} catch (e) {{ return JSON.stringify({{ status: "failed", message: e.toString() }}); }}
            }})()"#
        )
    };

    parse_action_result(session.eval(&js).await?)
}

pub(crate) async fn browser_reply(
    session: &BrowserSession,
    tweet_url: &str,
    text: &str,
) -> Result<ActionResult, TailFinError> {
    let tweet_id = crate::util::extract_tweet_id(tweet_url);
    let compose_url = format!("https://x.com/compose/post?in_reply_to={}", tweet_id);
    session.navigate(&compose_url).await?;
    let _ = session.wait_for_network_idle(15000, 1000).await;
    tokio::time::sleep(std::time::Duration::from_secs(3)).await;

    let escaped = escape_js_string(text);
    let js = format!(
        r#"(async () => {{
            try {{
                const boxes = Array.from(document.querySelectorAll('[data-testid="tweetTextarea_0"]'));
                const box = boxes.find(el => el.offsetParent !== null) || boxes[0];
                if (!box) return JSON.stringify({{ status: "failed", message: "Reply text area not found." }});
                box.focus();
                const dt = new DataTransfer();
                dt.setData('text/plain', "{escaped}");
                box.dispatchEvent(new ClipboardEvent('paste', {{ clipboardData: dt, bubbles: true, cancelable: true }}));
                await new Promise(r => setTimeout(r, 1000));
                const buttons = Array.from(document.querySelectorAll('[data-testid="tweetButton"], [data-testid="tweetButtonInline"]'));
                const btn = buttons.find(el => el.offsetParent !== null && !el.disabled);
                if (!btn) return JSON.stringify({{ status: "failed", message: "Reply button disabled or not found." }});
                btn.click();
                return JSON.stringify({{ status: "success", message: "Reply posted." }});
            }} catch (e) {{ return JSON.stringify({{ status: "failed", message: e.toString() }}); }}
        }})()"#
    );

    parse_action_result(session.eval(&js).await?)
}

pub(crate) async fn browser_trending(
    session: &BrowserSession,
    count: usize,
) -> Result<Vec<Trend>, TailFinError> {
    let js = r#"(() => {
        const items = [];
        const cells = document.querySelectorAll('[data-testid="trend"]');
        cells.forEach((cell) => {
            const text = cell.textContent || '';
            if (text.includes('Promoted')) return;
            const container = cell.querySelector(':scope > div');
            if (!container) return;
            const divs = container.children;
            if (divs.length < 2) return;
            const topic = divs[1].textContent.trim();
            if (!topic) return;
            const catText = divs[0].textContent.trim();
            const category = catText.replace(/^\d+\s*/, '').replace(/^\xB7\s*/, '').trim();
            let tweets = 'N/A';
            for (let j = 2; j < divs.length; j++) {
                if (divs[j].matches('[data-testid="caret"]') || divs[j].querySelector('[data-testid="caret"]')) continue;
                const t = divs[j].textContent.trim();
                if (t && /\d/.test(t)) { tweets = t; break; }
            }
            items.push(JSON.stringify({ rank: items.length + 1, topic, tweets, category }));
        });
        return '[' + items.join(',') + ']';
    })()"#;

    let result = navigate_and_eval(session, "https://x.com/explore/tabs/trending", 3, js).await?;
    let s = result.as_str().unwrap_or("[]");
    let trends: Vec<Trend> = serde_json::from_str(s).unwrap_or_default();
    Ok(trends.into_iter().take(count).collect())
}

pub(crate) async fn browser_download(
    session: &BrowserSession,
    username: &str,
    count: usize,
) -> Result<Vec<MediaItem>, TailFinError> {
    let username = username.trim_start_matches('@');
    session
        .navigate(&format!("https://x.com/{}/media", username))
        .await?;
    let _ = session.wait_for_network_idle(15000, 1000).await;
    tokio::time::sleep(std::time::Duration::from_secs(3)).await;

    tail_fin_common::page::scroll_to_load(session, count / 5 + 1, 2000).await?;

    let js = r#"(() => {
        const media = [];
        const seen = new Set();
        document.querySelectorAll('img[src*="pbs.twimg.com/media"]').forEach(img => {
            let src = img.src || '';
            src = src.replace(/&name=\w+$/, '&name=large');
            if (!src.includes('&name=')) src = src + '&name=large';
            if (!seen.has(src)) { seen.add(src); media.push(JSON.stringify({ type: 'image', url: src })); }
        });
        document.querySelectorAll('video').forEach(video => {
            const src = video.src || '';
            if (src && !seen.has(src)) { seen.add(src); media.push(JSON.stringify({ type: 'video', url: src })); }
        });
        return '[' + media.join(',') + ']';
    })()"#;

    let result = session.eval(js).await?;
    let s = result.as_str().unwrap_or("[]");
    let media: Vec<MediaItem> = serde_json::from_str(s).unwrap_or_default();
    Ok(media.into_iter().take(count).collect())
}

pub(crate) async fn browser_bookmark(
    session: &BrowserSession,
    tweet_url: &str,
    add: bool,
) -> Result<ActionResult, TailFinError> {
    let (target, already_state, success_msg, already_msg) = if add {
        (
            "bookmark",
            "removeBookmark",
            "Tweet bookmarked.",
            "Tweet is already bookmarked.",
        )
    } else {
        (
            "removeBookmark",
            "bookmark",
            "Bookmark removed.",
            "Tweet is not bookmarked.",
        )
    };

    let js = format!(
        r#"(async () => {{
            try {{
                for (let i = 0; i < 20; i++) {{
                    const already = document.querySelector('[data-testid="{already_state}"]');
                    if (already) return JSON.stringify({{ status: "success", message: "{already_msg}" }});
                    const btn = document.querySelector('[data-testid="{target}"]');
                    if (btn) {{
                        btn.click();
                        await new Promise(r => setTimeout(r, 1000));
                        const verify = document.querySelector('[data-testid="{already_state}"]');
                        if (verify) return JSON.stringify({{ status: "success", message: "{success_msg}" }});
                        return JSON.stringify({{ status: "failed", message: "Action did not register." }});
                    }}
                    await new Promise(r => setTimeout(r, 500));
                }}
                return JSON.stringify({{ status: "failed", message: "Bookmark button not found." }});
            }} catch (e) {{ return JSON.stringify({{ status: "failed", message: e.toString() }}); }}
        }})()"#
    );

    parse_action_result(navigate_and_eval(session, tweet_url, 2, &js).await?)
}

pub(crate) async fn browser_hide_reply(
    session: &BrowserSession,
    tweet_url: &str,
) -> Result<ActionResult, TailFinError> {
    let js = r#"(async () => {
        try {
            const visible = (el) => !!el && (el.offsetParent !== null || el.getClientRects().length > 0);
            const articles = Array.from(document.querySelectorAll('article'));
            if (!articles.length) return JSON.stringify({ status: "failed", message: "Tweet not found on page." });

            for (const article of articles) {
                const buttons = Array.from(article.querySelectorAll('button,[role="button"]'));
                const moreMenu = buttons.find(el => visible(el) && (el.getAttribute('aria-label') || '').trim() === 'More');
                if (!moreMenu) continue;

                moreMenu.click();
                await new Promise(r => setTimeout(r, 1000));

                const items = Array.from(document.querySelectorAll('[role="menuitem"]'));
                const hideBtn = items.find(item => {
                    const text = (item.textContent || '').trim();
                    return text.includes('Hide reply');
                });
                if (!hideBtn) {
                    document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
                    await new Promise(r => setTimeout(r, 500));
                    continue;
                }

                hideBtn.click();
                await new Promise(r => setTimeout(r, 1000));
                return JSON.stringify({ status: "success", message: "Reply hidden." });
            }
            return JSON.stringify({ status: "failed", message: "Hide reply option not found. You may not own the parent tweet." });
        } catch (e) { return JSON.stringify({ status: "failed", message: e.toString() }); }
    })()"#;

    parse_action_result(navigate_and_eval(session, tweet_url, 2, js).await?)
}

pub(crate) async fn browser_lists(
    session: &BrowserSession,
    count: usize,
) -> Result<Vec<crate::types::TwitterList>, TailFinError> {
    let js = r#"(() => {
        const lists = [];
        const links = document.querySelectorAll('a[href*="/i/lists/"]');
        const seen = new Set();
        links.forEach(link => {
            const href = link.getAttribute('href') || '';
            const match = href.match(/\/i\/lists\/(\d+)/);
            if (!match || seen.has(match[1])) return;
            seen.add(match[1]);

            const container = link.closest('[data-testid="cellInnerDiv"]') || link;
            const text = container.textContent || '';

            const spans = Array.from(link.querySelectorAll('span'));
            const nameSpan = spans.find(s => s.textContent.trim().length > 0 && !s.textContent.includes('members') && !s.textContent.includes('follower'));
            const name = nameSpan ? nameSpan.textContent.trim() : '';
            if (!name) return;

            let memberCount = 0;
            let followerCount = 0;
            const nums = text.match(/(\d+)\s*members?/i);
            if (nums) memberCount = parseInt(nums[1], 10);
            const fols = text.match(/(\d+)\s*followers?/i);
            if (fols) followerCount = parseInt(fols[1], 10);

            const isPrivate = !!container.querySelector('[data-testid="icon-lock"]') ||
                              text.toLowerCase().includes('private');

            lists.push(JSON.stringify({
                id: match[1],
                name,
                member_count: memberCount,
                follower_count: followerCount,
                is_private: isPrivate
            }));
        });
        return '[' + lists.join(',') + ']';
    })()"#;

    let result = navigate_and_eval(session, "https://x.com/i/lists", 3, js).await?;
    let s = result.as_str().unwrap_or("[]");
    let lists: Vec<crate::types::TwitterList> = serde_json::from_str(s).unwrap_or_default();
    Ok(lists.into_iter().take(count).collect())
}

pub(crate) async fn browser_reply_dm(
    session: &BrowserSession,
    text: &str,
    count: usize,
) -> Result<Vec<crate::types::DmResult>, TailFinError> {
    session.navigate("https://x.com/messages").await?;
    let _ = session.wait_for_network_idle(15000, 1000).await;
    tokio::time::sleep(std::time::Duration::from_secs(3)).await;

    // Scroll to load conversations
    for _ in 0..(count / 5 + 1) {
        session
            .eval(
                "document.querySelector('[data-testid=\"DmScrollerContainer\"]')?.scrollBy(0, 500)",
            )
            .await?;
        tokio::time::sleep(std::time::Duration::from_millis(1500)).await;
    }

    let escaped = escape_js_string(text);
    let js = format!(
        r#"(async () => {{
            const results = [];
            const convos = Array.from(document.querySelectorAll('[data-testid="conversation"]')).slice(0, {count});
            for (let i = 0; i < convos.length; i++) {{
                const convo = convos[i];
                const name = convo.textContent.trim().split('\n')[0] || ('Conversation ' + (i + 1));
                try {{
                    convo.click();
                    await new Promise(r => setTimeout(r, 2000));
                    const box = document.querySelector('[data-testid="dmComposerTextInput"]');
                    if (!box) {{
                        results.push(JSON.stringify({{ conversation: name, status: "skipped", message: "No input box found" }}));
                        continue;
                    }}
                    box.focus();
                    const dt = new DataTransfer();
                    dt.setData('text/plain', "{escaped}");
                    box.dispatchEvent(new ClipboardEvent('paste', {{ clipboardData: dt, bubbles: true, cancelable: true }}));
                    await new Promise(r => setTimeout(r, 500));
                    const sendBtn = document.querySelector('[data-testid="dmComposerSendButton"]');
                    if (sendBtn && !sendBtn.disabled) {{
                        sendBtn.click();
                        await new Promise(r => setTimeout(r, 1500));
                        results.push(JSON.stringify({{ conversation: name, status: "sent", message: "Message sent" }}));
                    }} else {{
                        results.push(JSON.stringify({{ conversation: name, status: "failed", message: "Send button not found or disabled" }}));
                    }}
                }} catch (e) {{
                    results.push(JSON.stringify({{ conversation: name, status: "error", message: e.toString() }}));
                }}
            }}
            return '[' + results.join(',') + ']';
        }})()"#
    );

    let result = session.eval(&js).await?;
    let s = result.as_str().unwrap_or("[]");
    let dm_results: Vec<crate::types::DmResult> = serde_json::from_str(s).unwrap_or_default();
    Ok(dm_results)
}

pub(crate) async fn browser_accept(
    session: &BrowserSession,
    filter: Option<&str>,
    count: usize,
) -> Result<Vec<crate::types::AcceptResult>, TailFinError> {
    session.navigate("https://x.com/follower_requests").await?;
    let _ = session.wait_for_network_idle(15000, 1000).await;
    tokio::time::sleep(std::time::Duration::from_secs(3)).await;

    // Scroll to load more requests
    for _ in 0..(count / 5 + 1) {
        session
            .eval("window.scrollBy(0, window.innerHeight)")
            .await?;
        tokio::time::sleep(std::time::Duration::from_millis(1500)).await;
    }

    let filter_js = match filter {
        Some(f) => {
            let keywords: Vec<&str> = f.split(',').map(|s| s.trim()).collect();
            let json_arr = serde_json::to_string(&keywords).unwrap_or_else(|_| "[]".into());
            format!("const filterKeywords = {};", json_arr)
        }
        None => "const filterKeywords = null;".into(),
    };

    let js = format!(
        r#"(async () => {{
            {filter_js}
            const results = [];
            const cells = Array.from(document.querySelectorAll('[data-testid="cellInnerDiv"]')).slice(0, {count});
            for (const cell of cells) {{
                const text = cell.textContent || '';
                const nameMatch = text.match(/^@?(\w+)/);
                const username = nameMatch ? nameMatch[1] : 'unknown';

                if (filterKeywords && !filterKeywords.some(kw => text.toLowerCase().includes(kw.toLowerCase()))) {{
                    results.push(JSON.stringify({{ username, status: "skipped", message: "Did not match filter" }}));
                    continue;
                }}

                const acceptBtn = Array.from(cell.querySelectorAll('button,[role="button"]'))
                    .find(btn => (btn.textContent || '').trim().toLowerCase().includes('confirm') ||
                                 (btn.textContent || '').trim().toLowerCase().includes('accept'));
                if (acceptBtn) {{
                    acceptBtn.click();
                    await new Promise(r => setTimeout(r, 1000));
                    const confirm = document.querySelector('[data-testid="confirmationSheetConfirm"]');
                    if (confirm) {{ confirm.click(); await new Promise(r => setTimeout(r, 500)); }}
                    results.push(JSON.stringify({{ username, status: "accepted", message: "Follow request accepted" }}));
                }} else {{
                    results.push(JSON.stringify({{ username, status: "skipped", message: "Accept button not found" }}));
                }}
            }}
            return '[' + results.join(',') + ']';
        }})()"#
    );

    let result = session.eval(&js).await?;
    let s = result.as_str().unwrap_or("[]");
    let accept_results: Vec<crate::types::AcceptResult> =
        serde_json::from_str(s).unwrap_or_default();
    Ok(accept_results)
}