spider 2.48.13

A web crawler and scraper, building blocks for data curation workloads.
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
use aho_corasick::{AhoCorasick, AhoCorasickBuilder};

#[cfg(all(feature = "chrome", feature = "real_browser"))]
use chromiumoxide::{
    cdp::js_protocol::runtime::{CallFunctionOnParams, EvaluateParams},
    error::CdpError,
    layout::Point,
    Page,
};
use std::time::Duration;

#[cfg(all(feature = "chrome", feature = "real_browser"))]
use crate::utils::{page_wait, perform_smart_mouse_movement, RequestError, CF_WAIT_FOR};
#[cfg(all(feature = "chrome", feature = "real_browser"))]
use base64::prelude::*;

static VERIFY_PATTERNS: &[&[u8]] = &[
    b"verifying you are human",
    b"review the security of your connection",
    b"please verify you are a human",
    b"checking your browser before accessing",
    b"prove you are human",
    b"checking if the site connection is secure",
];

/// Imperva iframe patterns.
static IMPERVA_IFRAME_PATTERNS: &[&[u8]] = &[
    b"geo.captcha-delivery.com",
    b"captcha-delivery.com",
    b"Verification system",
    b"Verification Required",
    b"Verification successful",
    b"Verifying device",
];

/// Recaptcha iframe patterns.
static RECAPTCHA_PATTERNS: &[&[u8]] = &[
    b"https://www.google.com/recaptcha/",
    b"/recaptcha/",
    b"recaptcha/api2/anchor",
    b"recaptcha/enterprise/bframe",
];

/// Geetest patterns.
static GEETEST_PATTERNS: &[&[u8]] = &[
    b"id=\"embed-captcha\"",
    b"class=\"gee-test",
    b"class=\"gee-test__placeholder",
];

/// Geetest loading patterns.
static GEETEST_LOADING_PATTERNS: &[&[u8]] = &[b"Loading GeeTest", b"geetest_wait", b"geetest_init"];

/// Geetest visible patterns.
static GEETEST_VISIBLE_PATTERNS: &[&[u8]] = &[
    b"geetest_widget",
    b"geetest_slider_button",
    b"geetest_canvas",
    b"geetest_canvas_slice",
];

/// Imperva wait patterns.
static IMPERVA_WAIT_PATTERNS: &[&[u8]] = &[
    b"Verifying the device",
    b"Verifying the device...",
    b"The requested content will be available after verification",
    b"available after verification",
];

/// Imperva iframe phase patterns.
static IMPERVA_IFRAME_PHASE_PATTERNS: &[&[u8]] = &[
    b"geo.captcha-delivery.com",
    b"captcha-delivery.com",
    b"Verification system",
];

/// Hcaptcha wait patterns.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
static HCAPTCHA_IFRAME_PATTERNS: &[&[u8]] = &[
    b"newassets.hcaptcha.com",
    b"hcaptcha.com/captcha",
    b"Widget containing checkbox for hCaptcha",
    b"data-hcaptcha-widget-id",
];

/// RC enterprise guards.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
static RC_ENTERPRISE_GUARD_PATTERNS: &[&[u8]] = &[
    b"__recaptcha_api",
    b"/recaptcha/enterprise/",
    b"rc-imageselect",
    b"rc-imageselect-tile",
];

static LEMIN_PATTERNS: &[&[u8]] = &[b"id=\"lemin-cropped-captcha\""];

/// RC verify btn patterns.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
static RC_VERIFY_BUTTON_PATTERNS: &[&[u8]] = &[b"id=\"recaptcha-verify-button\"", b">Verify<"];

/// RC tile patterns.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
static RC_TILE_CLASS_PATTERNS: &[&[u8]] = &[b"rc-imageselect-tile"];

#[cfg(all(feature = "chrome", feature = "real_browser"))]
lazy_static! {
    /// hCaptcha‑iframe matcher (used inside Imperva flow)
    static ref HCAPTCHA_IFRAME_AC: AhoCorasick = AhoCorasickBuilder::new()
        .ascii_case_insensitive(true)
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .build(HCAPTCHA_IFRAME_PATTERNS)
        .expect("valid hCaptcha iframe patterns");

            static ref RC_ENTERPRISE_GUARD_AC: AhoCorasick = AhoCorasickBuilder::new()
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .ascii_case_insensitive(false)
        .build(RC_ENTERPRISE_GUARD_PATTERNS)
        .expect("valid enterprise‑recaptcha guard patterns");

    // Verify‑button detection (either the hidden button id or the visible “Verify” text).
    static ref RC_VERIFY_BUTTON_AC: AhoCorasick = AhoCorasickBuilder::new()
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .ascii_case_insensitive(false)
        .build(RC_VERIFY_BUTTON_PATTERNS)
        .expect("valid verify‑button patterns");

    // Tile‑class matcher – used to locate every tile in the HTML.
    static ref RC_TILE_CLASS_AC: AhoCorasick = AhoCorasickBuilder::new()
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .ascii_case_insensitive(false)
        .build(RC_TILE_CLASS_PATTERNS)
        .expect("valid tile‑class pattern");
}

#[cfg(any(not(feature = "wreq"), feature = "cache_request"))]
lazy_static! {
    /// Gemini client – plain reqwest client (no middleware).
    static ref GEMINI_CLIENT: reqwest::Client = {
        reqwest::ClientBuilder::new()
            .timeout(Duration::from_millis(20_000))
            .build()
            .expect("failed to build Gemini client")
    };
}
#[cfg(all(feature = "wreq", not(feature = "cache_request")))]
lazy_static! {
    /// Gemini client – plain wreq client (no middleware).
    static ref GEMINI_CLIENT: wreq::Client = {
        wreq::ClientBuilder::new()
            .timeout(Duration::from_millis(20_000))
            .build()
            .expect("failed to build Gemini client")
    };
}

lazy_static! {
    /// Imperva check
    static ref AC_IMPERVA_IFRAME: aho_corasick::AhoCorasick = aho_corasick::AhoCorasickBuilder::new()
        .ascii_case_insensitive(true)
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .build(IMPERVA_IFRAME_PATTERNS)
        .expect("valid imperva iframe patterns");
    /// Bot verify.
    static ref AC: AhoCorasick =  aho_corasick::AhoCorasickBuilder::new()
        .match_kind(aho_corasick::MatchKind::LeftmostLongest)
        .build(VERIFY_PATTERNS)
        .unwrap();
    /// Recaptcha patterns.
    static ref RECAPTCHA_AC: AhoCorasick = AhoCorasickBuilder::new()
        .ascii_case_insensitive(true)
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .build(RECAPTCHA_PATTERNS)
        .expect("valid recaptcha patterns");
    /// GeeTest patterns.
    static ref GEETEST_AC: AhoCorasick = AhoCorasickBuilder::new()
        .ascii_case_insensitive(true)
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .build(GEETEST_PATTERNS)
        .expect("valid geetest patterns");
    /// GeeTest loading AC.
    static ref GEETEST_LOADING_AC: AhoCorasick = AhoCorasickBuilder::new()
        .ascii_case_insensitive(true)
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .build(GEETEST_LOADING_PATTERNS)
        .expect("valid geetest loading patterns");
    /// GeeTest visible‑challenge matcher
    static ref GEETEST_VISIBLE_AC: AhoCorasick = AhoCorasickBuilder::new()
        .ascii_case_insensitive(true)
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .build(GEETEST_VISIBLE_PATTERNS)
        .expect("valid geetest visible patterns");
    /// Imperva wait AC.
    static ref IMPERVA_WAIT_AC: AhoCorasick = AhoCorasickBuilder::new()
            .ascii_case_insensitive(true)
            .match_kind(aho_corasick::MatchKind::LeftmostFirst)
            .build(IMPERVA_WAIT_PATTERNS)
            .expect("valid Imperva wait‑screen patterns");

    /// Imperva iframe matcher.
    static ref IMPERVA_IFRAME_PHASE_AC: AhoCorasick = AhoCorasickBuilder::new()
        .ascii_case_insensitive(true)
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .build(IMPERVA_IFRAME_PHASE_PATTERNS)
        .expect("valid Imperva iframe‑phase patterns");
    /// Lemin match.
    static ref LEMIN_AC: AhoCorasick = AhoCorasickBuilder::new()
        .ascii_case_insensitive(true)
        .match_kind(aho_corasick::MatchKind::LeftmostFirst)
        .build(LEMIN_PATTERNS)
        .expect("valid lemin patterns");
}

#[cfg(feature = "chrome")]
/// CF prefix scan bytes.
const CF_PREFIX_SCAN_BYTES: usize = 120;

#[cfg(feature = "chrome")]
#[inline(always)]
/// CF slice prefix.
fn cf_prefix_slice(b: &[u8]) -> &[u8] {
    if b.len() > CF_PREFIX_SCAN_BYTES {
        &b[..CF_PREFIX_SCAN_BYTES]
    } else {
        b
    }
}

#[cfg(feature = "chrome")]
lazy_static! {
    /// CF end match.
    static ref CF_END: &'static [u8; 62] =
        b"target=\"_blank\">Cloudflare</a></div></div></div></body></html>";
    /// CF end second template.
    static ref CF_END2: &'static [u8; 72] =
        b"Performance &amp; security by Cloudflare</div></div></div></body></html>";
    /// CF head.
    static ref CF_HEAD: &'static [u8; 34] = b"<html><head>\n    <style global=\"\">";
    /// CF mock frame.
    static ref CF_MOCK_FRAME: &'static [u8; 137] = b"<iframe height=\"1\" width=\"1\" style=\"position: absolute; top: 0px; left: 0px; border: none; visibility: hidden;\"></iframe>\n\n</body></html>";
    /// Cf just a moment.
    static ref CF_JUST_A_MOMENT: &'static [u8] =
        b"<!DOCTYPE html><html lang=\"en-US\" dir=\"ltr\"><head><title>Just a moment...</title>";

    // Fast prefix-only matcher (scan only the first ~120 bytes).
    static ref CF_JUST_A_MOMENT_AC: aho_corasick::AhoCorasick = aho_corasick::AhoCorasickBuilder::new()
        .ascii_case_insensitive(true)
        .build([
            b"<title>Just a moment...</title>".as_slice(),
            b"Just a moment...".as_slice(),
        ])
        .expect("valid CF just-a-moment patterns");
}

#[inline(always)]
/// Detect recaptcha.
pub fn detect_recaptcha(html: &[u8]) -> bool {
    RECAPTCHA_AC.is_match(html)
}

#[inline(always)]
/// Detect GeeTest.
pub fn detect_geetest(html: &[u8]) -> bool {
    GEETEST_AC.is_match(html)
}

#[inline(always)]
/// Detect lemin.
pub fn detect_lemin(html: &[u8]) -> bool {
    LEMIN_AC.is_match(html)
}

/// Looks like GeeTest.
#[inline(always)]
pub fn looks_like_geetest(html: &[u8]) -> bool {
    GEETEST_AC.is_match(html)
}

/// Looks like GeeTest loading.
#[inline(always)]
pub fn looks_like_geetest_loading(html: &[u8]) -> bool {
    GEETEST_LOADING_AC.is_match(html)
}

/// Geetest challenge visible.
#[inline(always)]
pub fn looks_like_geetest_challenge_visible(html: &[u8]) -> bool {
    GEETEST_VISIBLE_AC.is_match(html)
}

#[inline(always)]
/// Imperva challenge size
pub fn imperva_challenge_sized(len: usize) -> bool {
    len > 0 && len <= 220_000
}

#[inline(always)]
/// Looks like imperva wait screen.
pub fn looks_like_imperva_wait_screen(html: &[u8]) -> bool {
    imperva_challenge_sized(html.len()) && IMPERVA_WAIT_AC.is_match(html)
}

#[inline(always)]
/// Looks like imperva phase screen.
pub fn looks_like_imperva_iframe_phase(html: &[u8]) -> bool {
    imperva_challenge_sized(html.len()) && IMPERVA_IFRAME_PHASE_AC.is_match(html)
}

#[inline(always)]
#[cfg(all(feature = "chrome", feature = "real_browser"))]
/// Looks like hcaptcha iframe.
pub fn looks_like_hcaptcha_iframe(html: &[u8]) -> bool {
    imperva_challenge_sized(html.len()) && HCAPTCHA_IFRAME_AC.is_match(html)
}

#[inline(always)]
#[cfg(all(feature = "chrome", feature = "real_browser"))]
/// Looks like imperva.
pub fn looks_like_imperva_any(html: &[u8]) -> bool {
    looks_like_imperva_wait_screen(html)
        || looks_like_imperva_iframe_phase(html)
        || looks_like_hcaptcha_iframe(html)
}

#[cfg(feature = "chrome")]
#[inline]
/// Is turnstile page?
pub(crate) fn detect_cf_turnstyle(b: &[u8]) -> bool {
    if b.ends_with(CF_END.as_ref()) || b.ends_with(CF_END2.as_ref()) {
        return true;
    }

    if b.starts_with(CF_HEAD.as_ref()) && b.ends_with(CF_MOCK_FRAME.as_ref()) {
        return true;
    }

    let pfx = cf_prefix_slice(b);

    pfx.starts_with(CF_JUST_A_MOMENT.as_ref()) || CF_JUST_A_MOMENT_AC.is_match(pfx)
}

lazy_static! {
    /// Apache server forbidden.
    pub static ref APACHE_FORBIDDEN: &'static [u8; 317] = br#"<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
<p>Additionally, a 403 Forbidden
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>"#;

    /// Open Resty forbidden.
    pub static ref OPEN_RESTY_FORBIDDEN: &'static [u8; 125] = br#"<html><head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>openresty</center>"#;


  /// Empty html.
  pub static ref EMPTY_HTML_BASIC: &'static [u8; 13] = b"<html></html>";
  /// The vision endpoint gemini.
  pub static ref GEMINI_VISION_ENDPOINT: String = {
    std::env::var("GEMINI_VISION_ENDPOINT").unwrap_or("https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision".into())
  };
}

#[inline(always)]
/// Detect imperva verification iframe.
pub fn detect_imperva_verification_iframe(html: &[u8]) -> bool {
    AC_IMPERVA_IFRAME.is_match(html)
}

/// A combined “looks like Imperva verification page” check.
/// Use this before deciding that X-Cdn: Imperva implies Imperva.
#[inline(always)]
pub fn looks_like_imperva_verify(content_len: usize, html: &[u8]) -> bool {
    imperva_challenge_sized(content_len) && detect_imperva_verification_iframe(html)
}

/// Detect if openresty hard 403 is forbidden and should not retry.
#[inline(always)]
pub fn detect_open_resty_forbidden(b: &[u8]) -> bool {
    b.starts_with(*OPEN_RESTY_FORBIDDEN)
}

/// Detect if a page is forbidden and should not retry.
#[inline(always)]
pub fn detect_hard_forbidden_content(b: &[u8]) -> bool {
    b == *APACHE_FORBIDDEN || detect_open_resty_forbidden(b)
}

/// Needs bot verification.
#[inline(always)]
pub fn contains_verification(text: &Vec<u8>) -> bool {
    AC.is_match(text)
}

/// Handle protected pages via chrome. This does nothing without the real_browser feature enabled.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
#[inline(always)]
pub async fn cf_handle(
    b: &mut Vec<u8>,
    page: &chromiumoxide::Page,
    target_url: &str,
    viewport: &Option<crate::configuration::Viewport>,
) -> Result<bool, chromiumoxide::error::CdpError> {
    let mut validated = false;

    let page_result = tokio::time::timeout(tokio::time::Duration::from_secs(30), async {
        let page_navigate = async {
            // force upgrade https check.
            if let Some(page_url) = page.url().await? {
                if page_url == "about:blank" {
                    let target_url = if target_url.starts_with("http://") {
                        let mut s = String::with_capacity(target_url.len() + 1);
                        s.push_str("https://");
                        s.push_str(&target_url["http://".len()..]);
                        s
                    } else {
                        target_url.to_string()
                    };
                    let _ = page.goto(target_url).await?.wait_for_navigation().await?;
                }
                else if page_url.starts_with("http://") {
                    let _ = page.goto(page_url.replacen("http://", "https://", 1)).await?;
                } else {
                    tokio::time::sleep(Duration::from_millis(3_500)).await;
                }
            }

            Ok::<(), chromiumoxide::error::CdpError>(())
        };

        // get the csp settings before hand
        let _ = tokio::join!(page.disable_network_cache(true), page_navigate, perform_smart_mouse_movement(page, viewport));

        for _ in 0..10 {
            let mut wait_for = CF_WAIT_FOR.clone();

            let mut clicks = 0usize;
            let mut hidden = false;

            if let Ok(els) = page
                .find_elements_pierced(
                    r#"
                div[id*="turnstile"],
                iframe[src*="challenges.cloudflare.com"],
                iframe[src*="turnstile"],
                iframe[title*="widget"],
                input[type="checkbox"]"#,
                )
                .await
            {
                perform_smart_mouse_movement(page, viewport).await;
                for el in els {
                    let f = async {
                        match el.clickable_point().await {
                            Ok(pt) => page.click(pt).await.is_ok() || el.click().await.is_ok(),
                            Err(_) => el.click().await.is_ok(),
                        }
                    };

                    let (did_click, _) =
                        tokio::join!(f, perform_smart_mouse_movement(page, viewport));

                    if did_click {
                        clicks += 1;
                    }
                }
            } else {

                hidden = true;
                let wait = Some(wait_for.clone());
                let _ = tokio::join!(
                    page_wait(page, &wait),
                    perform_smart_mouse_movement(page, viewport)
                );
            }

            if !hidden && clicks == 0 {
                let f = page.evaluate(
                    r#"document.querySelectorAll("iframe,input")?.forEach(el => el.click());document.querySelector('.cf-turnstile')?.click();"#,
                );
                let _ = tokio::join!(f, perform_smart_mouse_movement(page, viewport));
            }

            wait_for.page_navigations = true;
            let wait = Some(wait_for.clone());

            let _ = tokio::join!(
                page_wait(page, &wait),
                perform_smart_mouse_movement(page, viewport),
            );

            if let Ok(mut next_content) = page.outer_html_bytes().await {
                if !detect_cf_turnstyle(&next_content) {
                    validated = true;
                    wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                        core::time::Duration::from_secs(4),
                    ))
                    .into();
                    page_wait(page, &Some(wait_for)).await;
                    if let Ok(nc) = page.outer_html_bytes().await {
                        next_content = nc;
                    }
                } else if contains_verification(&next_content) {
                    wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                        core::time::Duration::from_millis(3500),
                    ))
                    .into();
                    page_wait(page, &Some(wait_for.clone())).await;

                    if let Ok(nc) = page.outer_html_bytes().await {
                        next_content = nc;
                    }
                    if !detect_cf_turnstyle(&next_content) {
                        validated = true;
                        page_wait(page, &Some(wait_for)).await;
                        if let Ok(nc) = page.outer_html_bytes().await {
                            next_content = nc;
                        }
                    }
                };

                *b = next_content;

                if validated {
                    break;
                }
            }
        }

        Ok::<(), chromiumoxide::error::CdpError>(())
    })
    .await;

    match page_result {
        Ok(_) => Ok(validated),
        _ => Err(chromiumoxide::error::CdpError::Timeout),
    }
}

/// Handle imperva protected pages via chrome. This does nothing without the real_browser feature enabled.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
#[inline(always)]
pub async fn imperva_handle(
    b: &mut Vec<u8>,
    page: &chromiumoxide::Page,
    _target_url: &str,
    viewport: &Option<crate::configuration::Viewport>,
) -> Result<bool, chromiumoxide::error::CdpError> {
    // -----------------------------------------------------------------
    // Fast‑path – bail out early if the response does not look like an
    // Imperva challenge at all.
    // -----------------------------------------------------------------
    if !looks_like_imperva_any(b.as_slice()) {
        return Ok(false);
    }

    // -----------------------------------------------------------------
    // Drag‑helpers (unchanged)
    // -----------------------------------------------------------------
    #[inline(always)]
    fn pt(x: f64, y: f64) -> chromiumoxide::layout::Point {
        chromiumoxide::layout::Point { x, y }
    }

    #[inline(always)]
    fn clamp(v: f64, lo: f64, hi: f64) -> f64 {
        if v < lo {
            lo
        } else if v > hi {
            hi
        } else {
            v
        }
    }

    /// Build js drag handler.
    #[inline(always)]
    fn build_js_drag(fx: f64, fy: f64, tx: f64, ty: f64) -> String {
        use core::fmt::Write as _;
        let mut s = String::with_capacity(1024);
        let _ = write!(
            &mut s,
            r#"(function(){{const fx={:.3},fy={:.3},tx={:.3},ty={:.3};
const at=(x,y)=>document.elementFromPoint(x,y);
const fire=(el,type,x,y)=>{{if(!el)return;const o={{bubbles:true,cancelable:true,clientX:x,clientY:y,buttons:1}};el.dispatchEvent(new MouseEvent(type,o));try{{const p=type==='mousedown'?'pointerdown':type==='mousemove'?'pointermove':type==='mouseup'?'pointerup':type;el.dispatchEvent(new PointerEvent(p,{{bubbles:true,cancelable:true,clientX:x,clientY:y,buttons:1,pointerId:1,isPrimary:true}}));}}catch(e){{}}}};
const el0=at(fx,fy);fire(el0,'mousedown',fx,fy);
for(let i=1;i<=18;i++){{const t=i/18,x=fx+(tx-fx)*t,y=fy+(ty-fy)*t;fire(at(x,y)||el0,'mousemove',x,y);}}
fire(at(tx,ty)||el0,'mouseup',tx,ty);return true;}})()"#,
            fx, fy, tx, ty
        );
        s
    }

    // -----------------------------------------------------------------
    // Main solving loop (unchanged apart from the matcher calls)
    // -----------------------------------------------------------------
    let mut validated = false;

    let page_result = tokio::time::timeout(tokio::time::Duration::from_secs(30), async {
        // Disable cache + a little mouse‑movement jitter.
        let _ = tokio::join!(
            page.disable_network_cache(true),
            perform_smart_mouse_movement(page, viewport)
        );

        for _ in 0..10 {
            let mut wait_for = CF_WAIT_FOR.clone();

            // ---------------------------------------------------------
            // Pull HTML once per iteration.
            // ---------------------------------------------------------
            let cur_html = match page.outer_html_bytes().await {
                Ok(h) => h,
                Err(_) => {
                    let wait = Some(wait_for.clone());
                    let _ = tokio::join!(
                        page_wait(page, &wait),
                        perform_smart_mouse_movement(page, viewport),
                    );
                    continue;
                }
            };
            *b = cur_html;

            // ---------------------------------------------------------
            // If we have left the challenge, we are done.
            // ---------------------------------------------------------
            if !looks_like_imperva_any(b.as_slice()) {
                validated = true;
                break;
            }

            // ---------------------------------------------------------
            // 0️⃣  hCaptcha checkbox flow (used inside Imperva pages)
            // ---------------------------------------------------------
            let hcaptcha_iframe_present = page
                .find_elements_pierced(
                    r#"iframe[src*="hcaptcha.com"], iframe[src*="newassets.hcaptcha.com"]"#,
                )
                .await
                .map(|els| !els.is_empty())
                .unwrap_or(false);

            if hcaptcha_iframe_present || looks_like_hcaptcha_iframe(b.as_slice()) {
                if let Ok(boxes) = page.find_elements_pierced(r#"#checkbox"#).await {
                    if let Some(cb_el) = boxes.into_iter().next() {
                        // Click the checkbox – prefer the clickable point.
                        let clicked = match cb_el.clickable_point().await {
                            Ok(p) => page.click(p).await.is_ok() || cb_el.click().await.is_ok(),
                            Err(_) => cb_el.click().await.is_ok(),
                        };

                        if clicked {
                            // Give the page a moment to render/transition.
                            wait_for.delay = crate::features::chrome_common::WaitForDelay::new(
                                Some(core::time::Duration::from_millis(900)),
                            )
                            .into();
                            wait_for.idle_network =
                                crate::features::chrome_common::WaitForIdleNetwork::new(
                                    core::time::Duration::from_secs(6).into(),
                                )
                                .into();
                            wait_for.page_navigations = true;

                            let wait = Some(wait_for.clone());
                            let _ = tokio::join!(
                                page_wait(page, &wait),
                                perform_smart_mouse_movement(page, viewport),
                            );

                            if let Ok(nc) = page.outer_html_bytes().await {
                                *b = nc;
                                if !looks_like_imperva_any(b.as_slice()) {
                                    validated = true;
                                    break;
                                }
                            }
                        } else {
                            // Click failed – wait a bit and retry.
                            wait_for.delay = crate::features::chrome_common::WaitForDelay::new(
                                Some(core::time::Duration::from_millis(650)),
                            )
                            .into();
                            let wait = Some(wait_for.clone());
                            let _ = tokio::join!(
                                page_wait(page, &wait),
                                perform_smart_mouse_movement(page, viewport),
                            );
                        }

                        // Continue the outer loop – we may now be in the slider phase.
                        continue;
                    }
                }

                // No checkbox yet – behave like Cloudflare: just wait for the iframe to load.
                wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                    core::time::Duration::from_millis(900),
                ))
                .into();
                wait_for.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                    core::time::Duration::from_secs(6).into(),
                )
                .into();
                wait_for.page_navigations = true;

                let wait = Some(wait_for.clone());
                let _ = tokio::join!(
                    page_wait(page, &wait),
                    perform_smart_mouse_movement(page, viewport),
                );
                if let Ok(nc) = page.outer_html_bytes().await {
                    *b = nc;
                    if !looks_like_imperva_any(b.as_slice()) {
                        validated = true;
                        break;
                    }
                }
                continue;
            }

            // ---------------------------------------------------------
            // 1️⃣  WAIT SCREEN – just a static “please wait” page.
            // ---------------------------------------------------------
            if looks_like_imperva_wait_screen(b.as_slice()) {
                wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                    core::time::Duration::from_millis(1_100),
                ))
                .into();
                wait_for.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                    core::time::Duration::from_secs(7).into(),
                )
                .into();
                wait_for.page_navigations = true;

                let wait = Some(wait_for.clone());
                let _ = tokio::join!(
                    page_wait(page, &wait),
                    perform_smart_mouse_movement(page, viewport),
                );

                if let Ok(nc) = page.outer_html_bytes().await {
                    *b = nc;
                }
                continue;
            }

            // ---------------------------------------------------------
            // 2️⃣  Imperva iframe / slider phase.
            // ---------------------------------------------------------
            let verify_iframe_present = page
                .find_elements_pierced(
                    r#"
                    iframe[src*="geo.captcha-delivery.com"],
                    iframe[src*="captcha-delivery.com"],
                    iframe[title*="Verification system"],
                    iframe[title*="verification system"]
                    "#,
                )
                .await
                .map(|els| !els.is_empty())
                .unwrap_or(false);

            if verify_iframe_present || looks_like_imperva_iframe_phase(b.as_slice()) {
                let mut did_drag = false;

                // -----------------------------------------------------------------
                // Try to locate a native slider handle first.
                // -----------------------------------------------------------------
                if let Ok(handles) = page
                    .find_elements_pierced(
                        r#"
                        .slider,
                        [class*="sliderHandle"],
                        [class*="slider-handle"],
                        [class*="slider"]
                        "#,
                    )
                    .await
                {
                    if let Some(handle) = handles.into_iter().next() {
                        if let (Ok(hb), Ok(conts)) = (
                            handle.bounding_box().await,
                            page.find_elements_pierced(
                                r#"
                                .sliderContainer,
                                [class*="sliderContainer"],
                                .slider-container,
                                [class*="slider-container"]
                                "#,
                            )
                            .await,
                        ) {
                            if let Some(container) = conts.into_iter().next() {
                                if let Ok(cb) = container.bounding_box().await {
                                    let from = pt(hb.x + hb.width * 0.5, hb.y + hb.height * 0.5);
                                    let to_x = cb.x + cb.width - 8.0;
                                    let to_y = cb.y + cb.height * 0.5;
                                    let to = pt(
                                        clamp(to_x, cb.x + 2.0, cb.x + cb.width - 2.0),
                                        clamp(to_y, cb.y + 2.0, cb.y + cb.height - 2.0),
                                    );

                                    // Small mouse‑move jitter before the drag.
                                    let _ = tokio::join!(
                                        perform_smart_mouse_movement(page, viewport),
                                        async {
                                            let _ = page.move_mouse(from).await;
                                        }
                                    );

                                    if page.click_and_drag(from, to).await.is_ok() {
                                        did_drag = true;
                                    }
                                }
                            }
                        }
                    }
                }

                // -----------------------------------------------------------------
                // Fallback – build a JS drag that uses the container bbox.
                // -----------------------------------------------------------------
                if !did_drag {
                    if let Ok(conts) = page
                        .find_elements_pierced(
                            r#"
                            .sliderContainer,
                            [class*="sliderContainer"],
                            .slider-container,
                            [class*="slider-container"]
                            "#,
                        )
                        .await
                    {
                        if let Some(container) = conts.into_iter().next() {
                            if let Ok(cb) = container.bounding_box().await {
                                let from_x = clamp(cb.x + 10.0, cb.x + 2.0, cb.x + cb.width - 2.0);
                                let from_y = clamp(
                                    cb.y + cb.height * 0.5,
                                    cb.y + 2.0,
                                    cb.y + cb.height - 2.0,
                                );
                                let to_x = clamp(
                                    cb.x + cb.width - 10.0,
                                    cb.x + 2.0,
                                    cb.x + cb.width - 2.0,
                                );
                                let to_y = from_y;

                                let js = build_js_drag(from_x, from_y, to_x, to_y);
                                let _ = page.evaluate(js).await;
                                did_drag = true;
                            }
                        }
                    }
                }

                // -----------------------------------------------------------------
                // Wait a little after the drag (or after the JS fallback).
                // -----------------------------------------------------------------
                if did_drag {
                    wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                        core::time::Duration::from_millis(900),
                    ))
                    .into();
                    wait_for.idle_network =
                        crate::features::chrome_common::WaitForIdleNetwork::new(
                            core::time::Duration::from_secs(6).into(),
                        )
                        .into();
                    wait_for.page_navigations = true;
                } else {
                    wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                        core::time::Duration::from_millis(650),
                    ))
                    .into();
                    wait_for.page_navigations = true;
                }

                let wait = Some(wait_for.clone());
                let _ = tokio::join!(
                    page_wait(page, &wait),
                    perform_smart_mouse_movement(page, viewport),
                );

                if let Ok(nc) = page.outer_html_bytes().await {
                    *b = nc;
                    if !looks_like_imperva_any(b.as_slice()) {
                        validated = true;
                        break;
                    }
                }

                continue;
            }

            // ---------------------------------------------------------
            // 3️⃣  Unknown interstitial – do a generic CF‑style wait and retry.
            // ---------------------------------------------------------
            wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                core::time::Duration::from_millis(900),
            ))
            .into();
            wait_for.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                core::time::Duration::from_secs(6).into(),
            )
            .into();
            wait_for.page_navigations = true;

            let wait = Some(wait_for.clone());
            let _ = tokio::join!(
                page_wait(page, &wait),
                perform_smart_mouse_movement(page, viewport),
            );

            if let Ok(nc) = page.outer_html_bytes().await {
                *b = nc;
                if !looks_like_imperva_any(b.as_slice()) {
                    validated = true;
                    break;
                }
            }
        }

        Ok::<(), chromiumoxide::error::CdpError>(())
    })
    .await;

    match page_result {
        Ok(_) => Ok(validated),
        _ => Err(chromiumoxide::error::CdpError::Timeout),
    }
}

#[cfg(all(feature = "chrome", feature = "real_browser"))]
/// Returns the `data:image/...;base64,…` string for the `<img>` whose
/// `src` attribute equals `src`.  The image is already loaded in the
/// page, so this does **not** trigger a network request – it draws the
/// image onto a temporary canvas and reads the data‑URL from that canvas.
async fn extract_image_dataurl(page: &chromiumoxide::Page, src: &str) -> Result<String, CdpError> {
    // JavaScript that receives the exact `src` value, finds the <img>,
    // draws it onto a canvas, and returns `canvas.toDataURL()`.
    let js = format!(
        r#"(function(){{
            const img = document.querySelector('img[src="{src}"]');
            if (!img) return null;
            const canvas = document.createElement('canvas');
            canvas.width = img.naturalWidth || img.width;
            canvas.height = img.naturalHeight || img.height;
            const ctx = canvas.getContext('2d');
            ctx.drawImage(img, 0, 0);
            return canvas.toDataURL();
        }})()"#
    );

    // `page.evaluate` returns an `EvaluationResult`.
    let eval = page.evaluate(js).await?;
    let dataurl = eval
        .value()
        .and_then(|v| v.as_str().map(|s| s.to_owned()))
        .ok_or_else(|| CdpError::msg("failed to extract tile data‑url"))?;

    Ok(dataurl)
}

/// High‑level wrapper – first tries the in‑page Gemini helper,
/// falls back to the external Gemini HTTP call when the helper is missing.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
pub async fn solve_enterprise_with_browser_gemini(
    page: &chromiumoxide::Page,
    challenge: &RcEnterpriseChallenge<'_>,
    timeout_ms: u64,
) -> Result<Vec<u8>, CdpError> {
    let mut tiles_json = Vec::with_capacity(challenge.tiles.len());

    for tile in &challenge.tiles {
        let dataurl = extract_image_dataurl(page, tile.img_src).await?;
        tiles_json.push(serde_json::json!({ "id": tile.id, "dataurl": dataurl }));
    }

    let target = challenge.target.unwrap_or("target object").to_string();

    match solve_with_inpage_helper(page, &tiles_json, &target, timeout_ms).await {
        Ok(ids) => return Ok(ids),
        Err(e) if !is_missing_helper_error(&e) => return Err(e),
        Err(_) => {} // helper missing → fall back
    }

    solve_with_external_gemini(challenge, timeout_ms)
        .await
        .map_err(|e| CdpError::msg(format!("external‑gemini failed: {e}")))
}

/// In‑page Gemini helper – receives tiles that already contain a
/// `dataurl` field (the image as a `data:image/...;base64,…` string).
#[cfg(all(feature = "chrome", feature = "real_browser"))]
async fn solve_with_inpage_helper(
    page: &chromiumoxide::Page,
    tiles_json: &[serde_json::Value],
    target: &str,
    timeout_ms: u64,
) -> Result<Vec<u8>, CdpError> {
    let script = format!(
        r#"
        async function solveRecaptchaEnterpriseWithGemini(tiles, target) {{
            const session = await LanguageModel.create({{
                expectedInputs: [
                    {{ type: "text", languages: ["en"] }},
                    {{ type: "image" }},
                ],
                expectedOutputs: [{{ type: "text", languages: ["en"] }}],
            }});
            const yesIds = [];
            for (const tile of tiles) {{
                const resp = await fetch(tile.dataurl);
                if (!resp.ok) continue;
                const blob = await resp.blob();
                const prompt = [{{
                    role: "user",
                    content: [
                        {{ type: "text", value: `Does this image contain a ${{
                            target
                        }}? Answer only with "yes" or "no".` }},
                        {{ type: "image", value: blob }},
                    ],
                }}];
                const answer = await session.prompt(prompt);
                const txt = (answer || "").toString().trim().toLowerCase();
                if (txt.includes("yes")) yesIds.push(tile.id);
            }}
            return yesIds;
        }}

        (async () => {{
            const result = await solveRecaptchaEnterpriseWithGemini(
                {tiles},
                {target}
            );
            return result;
        }})()
        "#,
        tiles = serde_json::to_string(tiles_json).unwrap_or_default(),
        target = serde_json::to_string(target).unwrap_or_default(),
    );

    // -----------------------------------------------------------------
    // Ask Chrome to evaluate the script (same timeout logic as before).
    // -----------------------------------------------------------------
    let params = EvaluateParams::builder()
        .expression(&script)
        .await_promise(true)
        .build()
        .map_err(|e| CdpError::msg(format!("evaluate params: {e}")))?;

    let eval_fut = page.evaluate(params);
    let eval_res = tokio::time::timeout(Duration::from_millis(timeout_ms + 5_000), eval_fut)
        .await
        .map_err(|_| CdpError::Timeout)?;

    match eval_res {
        Ok(eval) => match eval.value() {
            Some(serde_json::Value::Array(arr)) => {
                let ids = arr
                    .iter()
                    .filter_map(|v| v.as_u64().map(|n| n as u8))
                    .collect();
                Ok(ids)
            }
            _ => Ok(vec![]),
        },
        Err(e) => Err(e),
    }
}

#[cfg(all(feature = "chrome", feature = "real_browser"))]
/// Is the language model missing.
fn is_missing_helper_error(err: &CdpError) -> bool {
    let txt = format!("{err}");
    txt.contains("LanguageModel is not defined")
        || txt.contains("ReferenceError")
        || txt.contains("Uncaught ReferenceError")
        || txt.contains("cannot read property 'create' of undefined")
}

#[cfg(all(feature = "chrome", feature = "real_browser"))]
/// Extract gemini fallback.
async fn solve_with_external_gemini(
    challenge: &RcEnterpriseChallenge<'_>,
    timeout_ms: u64,
) -> Result<Vec<u8>, RequestError> {
    if let Ok(api_key) = std::env::var("GEMINI_API_KEY") {
        if let Ok(_sem) = crate::utils::GEMINI_SEM
            .acquire_many(challenge.tiles.len().try_into().unwrap_or(1))
            .await
        {
            let endpoint = format!("{}?key={}", *GEMINI_VISION_ENDPOINT, api_key);

            let target = challenge.target.unwrap_or("target object").to_string();

            let mut yes_ids = Vec::new();

            for tile in &challenge.tiles {
                // -------------------------------------------------------------
                // a) Download the image bytes.
                // -------------------------------------------------------------
                let img_bytes = match GEMINI_CLIENT.get(tile.img_src).send().await {
                    Ok(resp) if resp.status().is_success() => resp.bytes().await?,
                    _ => continue, // if we cannot fetch the image we just skip it
                };

                // -------------------------------------------------------------
                // b) Build the Gemini request body.
                // -------------------------------------------------------------
                // Gemini expects a JSON object with a `contents` array.
                // Each element contains a `parts` array.  We send one text part and
                // one image part (base64‑encoded).
                let request_body = serde_json::json!({
                    "contents": [{
                        "role": "user",
                        "parts": [
                            {
                                "text": format!("Does this image contain a {}? Answer only with \"yes\" or \"no\".", target)
                            },
                            {
                                "inlineData": {
                                    "mimeType": "image/jpeg",   // recaptcha images are JPEGs
                                    "data": BASE64_STANDARD.encode(&img_bytes)
                                }
                            }
                        ]
                    }],
                    // The model may be asked to stop after it emits the answer.
                    "generationConfig": {
                        "maxOutputTokens": 5,
                        "temperature": 0.0
                    }
                });

                // -------------------------------------------------------------
                // c) Send the request (with a per‑tile timeout that is a fraction of
                //    the total timeout we were given).
                // -------------------------------------------------------------
                let per_tile_timeout =
                    Duration::from_millis(timeout_ms / (challenge.tiles.len() as u64 + 1));
                let resp = tokio::time::timeout(
                    per_tile_timeout,
                    GEMINI_CLIENT.post(&endpoint).json(&request_body).send(),
                )
                .await;

                let resp = match resp {
                    Ok(Ok(r)) => r,
                    // Either the HTTP request timed out or returned an error – skip.
                    _ => continue,
                };

                // -------------------------------------------------------------
                // d) Parse the Gemini answer.
                // -------------------------------------------------------------
                let resp_json: serde_json::Value = resp.json().await?;
                // The answer text lives in `candidates[0].content.parts[0].text`.
                let answer_text = resp_json
                    .get("candidates")
                    .and_then(|c| c.get(0))
                    .and_then(|c| c.get("content"))
                    .and_then(|c| c.get("parts"))
                    .and_then(|p| p.get(0))
                    .and_then(|p| p.get("text"))
                    .and_then(|t| t.as_str())
                    .unwrap_or("")
                    .trim()
                    .to_ascii_lowercase();

                if answer_text.contains("yes") {
                    yes_ids.push(tile.id);
                }
            }

            Ok(yes_ids)
        } else {
            Ok(Vec::new())
        }
    } else {
        Ok(Vec::new())
    }
}

/// Warm‑up the in‑page Gemini `LanguageModel` for the given Chrome page.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
pub async fn warm_gemini_model(page: &Page) -> Result<(), CdpError> {
    let eval_params = EvaluateParams::builder()
        .expression(r#"(async()=>{try{const s=await LanguageModel.create({expectedInputs:[{type:"text",languages:["en"]}],expectedOutputs:[{type:"text",languages:["en"]}]});await s.prompt([{role:"user",content:[{type:"text",value:"ping"}]}])}catch(_){}})()"#)
        .await_promise(true)
        .build()
        .map_err(|e| CdpError::msg(format!("evaluate params: {e}")))?;

    tokio::time::timeout(Duration::from_secs(60), page.evaluate(eval_params))
        .await
        .map_err(|_| CdpError::Timeout)??;

    Ok(())
}

/// Handle reCAPTCHA checkbox (anchor iframe) via chrome.
/// This does nothing without the real_browser feature enabled.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
#[inline(always)]
pub async fn recaptcha_handle(
    b: &mut Vec<u8>,
    page: &chromiumoxide::Page,
    viewport: &Option<crate::configuration::Viewport>,
) -> Result<bool, CdpError> {
    if !detect_recaptcha(b.as_slice()) {
        return Ok(false);
    }

    let mut validated = false;

    let overall = tokio::time::timeout(Duration::from_secs(30), async {
        // Keep the mouse moving a little – helps not being flagged as a bot.
        let _ = tokio::join!(
            page.disable_network_cache(true),
            perform_smart_mouse_movement(page, viewport)
        );

        for _ in 0..10 {
            // ---------------------------------------------------------
            // a) Refresh HTML into the caller’s buffer.
            // ---------------------------------------------------------
            if let Ok(cur) = page.outer_html_bytes().await {
                *b = cur;
            }

            // ---------------------------------------------------------
            // b) If Recaptcha vanished → success.
            // ---------------------------------------------------------
            if !detect_recaptcha(b.as_slice()) {
                validated = true;
                break;
            }

            // ---------------------------------------------------------
            // c) **Enterprise** handling – now solved with the built‑in Gemini.
            // ---------------------------------------------------------
            if extract_rc_enterprise_challenge(b.as_slice()).is_some() {
                // 1️⃣  Ensure the anchor iframe exists (first click).
                let anchor_present = page
                    .find_elements_pierced(r#"iframe[src*="/recaptcha/api2/anchor"]"#)
                    .await
                    .map(|els| !els.is_empty())
                    .unwrap_or(false);

                if !anchor_present {
                    // Wait for it to appear – same CF‑style wait.
                    let mut wait_for = CF_WAIT_FOR.clone();
                    wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                        Duration::from_millis(900),
                    ))
                    .into();
                    wait_for.idle_network =
                        crate::features::chrome_common::WaitForIdleNetwork::new(
                            Duration::from_secs(6).into(),
                        )
                        .into();
                    wait_for.page_navigations = true;
                    let wait = Some(wait_for.clone());
                    let _ = tokio::join!(
                        page_wait(page, &wait),
                        perform_smart_mouse_movement(page, viewport),
                    );
                    continue; // retry outer loop
                }

                // 2️⃣  Click the classic checkbox (same logic as before).
                async fn click_anchor(page: &chromiumoxide::Page) -> bool {
                    if let Ok(els) = page.find_elements_pierced(r#"#recaptcha-anchor"#).await {
                        if let Some(el) = els.into_iter().next() {
                            return match el.clickable_point().await {
                                Ok(p) => page.click(p).await.is_ok() || el.click().await.is_ok(),
                                Err(_) => el.click().await.is_ok(),
                            };
                        }
                    }
                    if let Ok(els) = page
                        .find_elements_pierced(r#".recaptcha-checkbox-checkmark"#)
                        .await
                    {
                        if let Some(el) = els.into_iter().next() {
                            return match el.clickable_point().await {
                                Ok(p) => page.click(p).await.is_ok() || el.click().await.is_ok(),
                                Err(_) => el.click().await.is_ok(),
                            };
                        }
                    }
                    false
                }

                let clicked = click_anchor(page).await;

                // 3️⃣  Wait a bit for the grid iframe to load.
                let mut wait_for = CF_WAIT_FOR.clone();
                wait_for.delay =
                    crate::features::chrome_common::WaitForDelay::new(Some(if clicked {
                        Duration::from_millis(1_100)
                    } else {
                        Duration::from_millis(700)
                    }))
                    .into();
                wait_for.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                    Duration::from_secs(7).into(),
                )
                .into();
                wait_for.page_navigations = true;
                let wait = Some(wait_for.clone());
                let _ = tokio::join!(
                    page_wait(page, &wait),
                    perform_smart_mouse_movement(page, viewport),
                );

                // ---------------------------------------------------------
                // d) Grab the grid HTML again – we need the *latest* tile URLs.
                // ---------------------------------------------------------
                let grid_html = match page.outer_html_bytes().await {
                    Ok(h) => h,
                    Err(_) => continue,
                };
                *b = grid_html.clone();

                // If the grid disappeared after the click, we’re done.
                if !detect_recaptcha(b.as_slice()) {
                    validated = true;
                    break;
                }

                // Extract the challenge *again* (now we are sure the grid is present).
                let challenge = match extract_rc_enterprise_challenge(&grid_html) {
                    Some(c) => c,
                    None => continue,
                };

                // ---------------------------------------------------------
                // e) **Solve with the built‑in Gemini** (the function above).
                // ---------------------------------------------------------
                let yes_ids = solve_enterprise_with_browser_gemini(page, &challenge, 20_000)
                    .await
                    .map_err(|e| {
                        CdpError::ChromeMessage(format!("gemini in‑page failed: {}", e))
                    })?;

                // ---------------------------------------------------------
                // f) Click every tile that received a “yes”.
                // ---------------------------------------------------------
                for id in yes_ids {
                    if let Some(tile) = challenge.tiles.iter().find(|t| t.id == id) {
                        // Build a selector that matches the exact `<img src="…">`.
                        let selector = format!(r#"img[src="{}"]"#, tile.img_src);
                        if let Ok(els) = page.find_elements_pierced(&selector).await {
                            if let Some(el) = els.into_iter().next() {
                                let _ = el.click().await; // ignore possible errors
                            }
                        }
                    }
                }

                // ---------------------------------------------------------
                // g) Click the Verify button if it exists.
                // ---------------------------------------------------------
                if challenge.has_verify_button {
                    if let Ok(btns) = page
                        .find_elements_pierced(
                            r#"button[id*="recaptcha-verify-button"], button:contains("Verify")"#,
                        )
                        .await
                    {
                        if let Some(btn) = btns.into_iter().next() {
                            let _ = btn.click().await;
                        }
                    }
                }

                // ---------------------------------------------------------
                // h) Final wait for navigation / network idle.
                // ---------------------------------------------------------
                let mut wait_for = CF_WAIT_FOR.clone();
                wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                    Duration::from_millis(1_500),
                ))
                .into();
                wait_for.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                    Duration::from_secs(8).into(),
                )
                .into();
                wait_for.page_navigations = true;
                let wait = Some(wait_for.clone());
                let _ = tokio::join!(
                    page_wait(page, &wait),
                    perform_smart_mouse_movement(page, viewport),
                );

                // ---------------------------------------------------------
                // i) Refresh HTML one last time – if the whole Recaptcha is gone we’re finished.
                // ---------------------------------------------------------
                if let Ok(new_html) = page.outer_html_bytes().await {
                    *b = new_html;
                    if !detect_recaptcha(b.as_slice()) {
                        validated = true;
                        break;
                    }
                }

                // If we are still here the grid is still present – loop again (maybe a slider appears).
                continue;
            }

            let anchor_iframe_present = page
                .find_elements_pierced(r#"iframe[src*="/recaptcha/api2/anchor"]"#)
                .await
                .map(|els| !els.is_empty())
                .unwrap_or(false);

            if !anchor_iframe_present {
                let mut wait_for = CF_WAIT_FOR.clone();
                wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                    Duration::from_millis(900),
                ))
                .into();
                wait_for.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                    Duration::from_secs(6).into(),
                )
                .into();
                wait_for.page_navigations = true;
                let wait = Some(wait_for.clone());
                let _ = tokio::join!(
                    page_wait(page, &wait),
                    perform_smart_mouse_movement(page, viewport),
                );
                continue;
            }

            // Click the classic checkbox (same logic you already had)
            let mut clicked = false;
            if let Ok(els) = page.find_elements_pierced(r#"#recaptcha-anchor"#).await {
                if let Some(el) = els.into_iter().next() {
                    clicked = match el.clickable_point().await {
                        Ok(p) => page.click(p).await.is_ok() || el.click().await.is_ok(),
                        Err(_) => el.click().await.is_ok(),
                    };
                }
            }
            if !clicked {
                if let Ok(els) = page
                    .find_elements_pierced(r#".recaptcha-checkbox-checkmark"#)
                    .await
                {
                    if let Some(el) = els.into_iter().next() {
                        clicked = match el.clickable_point().await {
                            Ok(p) => page.click(p).await.is_ok() || el.click().await.is_ok(),
                            Err(_) => el.click().await.is_ok(),
                        };
                    }
                }
            }

            let mut wait_for = CF_WAIT_FOR.clone();
            wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(if clicked {
                Duration::from_millis(1_100)
            } else {
                Duration::from_millis(700)
            }))
            .into();
            wait_for.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                Duration::from_secs(7).into(),
            )
            .into();
            wait_for.page_navigations = true;
            let wait = Some(wait_for.clone());
            let _ = tokio::join!(
                page_wait(page, &wait),
                perform_smart_mouse_movement(page, viewport),
            );

            if let Ok(new_html) = page.outer_html_bytes().await {
                *b = new_html;
                if !detect_recaptcha(b.as_slice()) {
                    validated = true;
                    break;
                }
            }
        }

        Ok::<(), CdpError>(())
    })
    .await;

    match overall {
        Ok(_) => Ok(validated),
        Err(_) => Err(CdpError::Timeout),
    }
}

#[cfg(all(feature = "chrome", feature = "real_browser"))]
/// Remove solve lemin external.
pub async fn solve_lemin_with_external_gemini(image_dataurl: &str, timeout_ms: u64) -> (f64, f64) {
    let api_key = match std::env::var("GEMINI_API_KEY") {
        Ok(k) => k,
        Err(_) => return (0.0, 0.0),
    };

    /* ----------------------------------------------------------------- *
     * 2️⃣  Acquire the semaphore that throttles concurrent Gemini calls.
     * ----------------------------------------------------------------- */
    if crate::utils::GEMINI_SEM.acquire().await.is_err() {
        return (0.0, 0.0);
    }

    /* ----------------------------------------------------------------- *
     * 3️⃣  Decode the `data:` URL into raw PNG bytes.
     * ----------------------------------------------------------------- */
    let b64_part = match image_dataurl.split_once(',').map(|x| x.1) {
        Some(p) => p.trim(),
        None => return (0.0, 0.0),
    };

    let img_bytes = match BASE64_STANDARD.decode(b64_part) {
        Ok(b) => b,
        Err(_) => return (0.0, 0.0),
    };

    /* ----------------------------------------------------------------- *
     * 4️⃣  Build the Gemini request payload – we ask for a JSON array `[x,y]`.
     * ----------------------------------------------------------------- */
    let request_body = serde_json::json!({
        "contents": [{
            "role": "user",
            "parts": [
                {
                    "text": "Give me the centre (x and y coordinates) of the missing puzzle piece in this image. Return a JSON array like [x, y] with numbers only."
                },
                {
                    "inlineData": {
                        "mimeType": "image/png",
                        "data": BASE64_STANDARD.encode(&img_bytes)
                    }
                }
            ]
        }],
        "generationConfig": {
            "maxOutputTokens": 16,
            "temperature": 0.0
        }
    });

    /* ----------------------------------------------------------------- *
     * 5️⃣  Perform the HTTP request, respecting a per‑tile timeout.
     * ----------------------------------------------------------------- */
    let per_tile = Duration::from_millis(timeout_ms / 2);
    let resp = match tokio::time::timeout(
        per_tile,
        GEMINI_CLIENT
            .post(&*GEMINI_VISION_ENDPOINT)
            .header("x-goog-api-key", api_key)
            .json(&request_body)
            .send(),
    )
    .await
    {
        Ok(Ok(r)) => r,
        _ => return (0.0, 0.0), // timeout or transport error
    };

    /* ----------------------------------------------------------------- *
     * 6️⃣  Verify we received a 200 OK.
     * ----------------------------------------------------------------- */
    if !resp.status().is_success() {
        return (0.0, 0.0);
    }

    /* ----------------------------------------------------------------- *
     * 7️⃣  Pull the textual answer out of Gemini’s JSON envelope.
     * ----------------------------------------------------------------- */
    let json: serde_json::Value = match resp.json().await {
        Ok(j) => j,
        Err(_) => return (0.0, 0.0),
    };

    let txt = match json
        .get("candidates")
        .and_then(|c| c.get(0))
        .and_then(|c| c.get("content"))
        .and_then(|c| c.get("parts"))
        .and_then(|p| p.get(0))
        .and_then(|p| p.get("text"))
        .and_then(|t| t.as_str())
    {
        Some(t) => t.trim(),
        None => return (0.0, 0.0),
    };

    /* ----------------------------------------------------------------- *
     * 8️⃣  Parse the `[x, y]` JSON array we asked Gemini for.
     * ----------------------------------------------------------------- */
    let coords: Vec<f64> = match serde_json::from_str(txt) {
        Ok(v) => v,
        Err(_) => return (0.0, 0.0),
    };

    if coords.len() == 2 {
        (coords[0], coords[1])
    } else {
        (0.0, 0.0)
    }
}

/// Lemin in page helper.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
async fn solve_lemin_with_inpage_helper(
    page: &Page,
    image_dataurl: &str,
    timeout_ms: u64,
) -> Result<(f64, f64), CdpError> {
    let script = format!(
        r#"(async () => {{
            try {{
                const session = await LanguageModel.create({{
                    expectedInputs: [
                        {{ type: "text", languages: ["en"] }},
                        {{ type: "image" }},
                    ],
                    expectedOutputs: [{{ type: "text", languages: ["en"] }}],
                }});
                const imgResp = await fetch("{image_dataurl}");
                if (!imgResp.ok) return null;
                const blob = await imgResp.blob();
                const prompt = [{{
                    role: "user",
                    content: [
                        {{ type: "text", value: "Give me the centre (x and y coordinates) of the missing puzzle piece in this image. Return a JSON array like [x, y] with numbers only." }},
                        {{ type: "image", value: blob }},
                    ],
                }}];
                const answer = await session.prompt(prompt);
                const txt = (answer || "").toString().trim();
                try {{ return JSON.parse(txt); }}
                catch {{ return null; }}
            }} catch (e) {{ throw e; }}
        }})()"#
    );

    let params = EvaluateParams::builder()
        .expression(&script)
        .await_promise(true)
        .build()
        .map_err(|e| CdpError::msg(format!("evaluate params: {e}")))?;

    let eval_fut = page.evaluate(params);

    let eval_res = tokio::time::timeout(Duration::from_millis(timeout_ms + 5_000), eval_fut)
        .await
        .map_err(|_| CdpError::Timeout)?; // outer timeout → CdpError::Timeout

    match eval_res {
        Ok(eval) => match eval.value() {
            Some(serde_json::Value::Array(arr)) if arr.len() == 2 => {
                let x = arr[0]
                    .as_f64()
                    .ok_or_else(|| CdpError::msg("Gemini did not return a numeric x"))?;
                let y = arr[1]
                    .as_f64()
                    .ok_or_else(|| CdpError::msg("Gemini did not return a numeric y"))?;
                Ok((x, y))
            }
            _ => Err(CdpError::msg("Gemini did not return a valid [x, y] array")),
        },
        Err(e) => Err(e), // propagate Chrome errors (including missing helper)
    }
}

#[cfg(all(feature = "chrome", feature = "real_browser"))]
/// Lemin solve handler.
pub async fn lemin_handle(
    b: &mut Vec<u8>,
    page: &Page,
    viewport: &Option<crate::configuration::Viewport>,
) -> Result<bool, CdpError> {
    // -----------------------------------------------------------------
    // Fast‑gate – bail out early if the page does not contain a Lemin widget.
    // -----------------------------------------------------------------
    if !detect_lemin(b.as_slice()) {
        return Ok(false);
    }

    let mut progressed = false;

    // -----------------------------------------------------------------
    // Whole routine lives inside a 30 s timeout (same pattern as the rest).
    // -----------------------------------------------------------------
    let page_result = tokio::time::timeout(Duration::from_secs(30), async {
        // Disable cache + a little “human” mouse movement.
        let _ = tokio::join!(
            page.disable_network_cache(true),
            perform_smart_mouse_movement(page, viewport)
        );

        for _ in 0..10 {
            // ---------------------------------------------------------
            // a) Refresh the HTML source.
            // ---------------------------------------------------------
            if let Ok(cur) = page.outer_html_bytes().await {
                *b = cur;
            }

            // ---------------------------------------------------------
            // b) If Lemin vanished → success.
            // ---------------------------------------------------------
            if !detect_lemin(b.as_slice()) {
                progressed = true;
                break;
            }

            // ---------------------------------------------------------
            // c) Click the hidden checkbox that activates the puzzle.
            // ---------------------------------------------------------
            if let Ok(checkboxes) = page
                .find_elements_pierced(
                    r#"div[id*="lemin-cropped-captcha"] input[type="checkbox"]"#,
                )
                .await
            {
                if let Some(cb) = checkboxes.into_iter().next() {
                    let clicked = match cb.clickable_point().await {
                        Ok(p) => page.click(p).await.is_ok() || cb.click().await.is_ok(),
                        Err(_) => cb.click().await.is_ok(),
                    };
                    if clicked {
                        let mut wait_for = CF_WAIT_FOR.clone();
                        wait_for.delay =
                            crate::features::chrome_common::WaitForDelay::new(Some(
                                Duration::from_millis(900),
                            ))
                            .into();
                        wait_for.idle_network =
                            crate::features::chrome_common::WaitForIdleNetwork::new(
                                Duration::from_secs(6).into(),
                            )
                            .into();
                        wait_for.page_navigations = true;
                        let wait = Some(wait_for.clone());
                        let _ = tokio::join!(
                            page_wait(page, &wait),
                            perform_smart_mouse_movement(page, viewport),
                        );
                    }
                }
            }

            // ---------------------------------------------------------
            // d) Locate the **full background image** and turn it into a data‑URL.
            // ---------------------------------------------------------
            let img_el = match page
                .find_elements_pierced(
                    r#"div[id*="lemin-captcha-popup"] img[src][width][height]"#,
                )
                .await
            {
                Ok(mut els) => els.pop(),
                Err(_) => None,
            };

            let dataurl = if let Some(img) = &img_el {
                // Use a temporary canvas to read the image as a data‑URL.
                let call = CallFunctionOnParams::builder()
                    .object_id(img.remote_object_id.clone())
                    .function_declaration(
                        "(function(){ const canvas = document.createElement('canvas'); \
                           canvas.width = this.naturalWidth || this.width; \
                           canvas.height = this.naturalHeight || this.height; \
                           const ctx = canvas.getContext('2d'); \
                           ctx.drawImage(this,0,0); \
                           return canvas.toDataURL(); })",
                    )
                    .await_promise(true)
                    .build()
                    .map_err(|e| CdpError::msg(format!("call function params: {e}")))?;

                let eval = page.evaluate_function(call).await?;
                eval.value()
                    .and_then(|v| v.as_str().map(|s| s.to_owned()))
                    .ok_or_else(|| CdpError::msg("failed to get data‑url from Lemin image"))?
            } else {
                return Err(CdpError::msg(
                    "Lemin puzzle image not found – cannot continue",
                ));
            };

            // ---------------------------------------------------------
            // e) Ask Gemini for the missing piece centre (x, y) – first try the
            //    in‑page helper, then fall back to the remote call.
            // ---------------------------------------------------------
            let (target_x, target_y) = match solve_lemin_with_inpage_helper(page, &dataurl, 20_000).await {
                Ok(p) => p,
                Err(e) if is_missing_helper_error(&e) => {
                    // -----------------------------------------------------------------
                    // Remote fallback – this is the same behaviour you already have
                    // for Recaptcha‑Enterprise.
                    // -----------------------------------------------------------------
                    solve_lemin_with_external_gemini(&dataurl, 20_000)
                        .await
                }
                Err(e) => return Err(e), // any other Chrome error bubbles up
            };

            // ---------------------------------------------------------
            // f) Locate the **draggable piece**.
            // ---------------------------------------------------------
            let piece_el = match page
                .find_elements_pierced(
                    r#"div[style*="touch-action: none"][style*="cursor: move"][style*="position: absolute"]"#,
                )
                .await
            {
                Ok(mut els) => els.pop(),
                Err(_) => None,
            };

            let piece_bb = if let Some(el) = piece_el {
                el.bounding_box().await?
            } else {
                return Err(CdpError::msg(
                    "Lemin draggable piece not found – cannot solve",
                ));
            };

            // ---------------------------------------------------------
            // g) Transform the coordinates returned by Gemini (relative to the
            //    full image) into absolute page coordinates.
            // ---------------------------------------------------------
            let img_bb = if let Some(img) = &img_el {
                img.bounding_box().await?
            } else {
                return Err(CdpError::msg(
                    "Lemin full image missing when calculating drag target",
                ));
            };

            // The image might be scaled, so compute a scale factor.
            let scale_x = img_bb.width / img_bb.width.max(1.0);
            let scale_y = img_bb.height / img_bb.height.max(1.0);
            let page_target_x = img_bb.x + target_x * scale_x;
            let page_target_y = img_bb.y + target_y * scale_y;

            // ---------------------------------------------------------
            // h) Drag the piece to the target.
            // ---------------------------------------------------------
            let from = Point {
                x: piece_bb.x + piece_bb.width * 0.5,
                y: piece_bb.y + piece_bb.height * 0.5,
            };
            let to = Point {
                x: page_target_x,
                y: page_target_y,
            };
            let _ = page.click_and_drag(from, to).await;

            // ---------------------------------------------------------
            // i) Click the **Verify** button (if present).
            // ---------------------------------------------------------
            if let Ok(btns) = page
                .find_elements_pierced(r#"button.verify-button, button[id*="verify-button"]"#)
                .await
            {
                if let Some(btn) = btns.into_iter().next() {
                    let _ = btn.click().await;
                }
            }

            // ---------------------------------------------------------
            // j) Wait a little, then check whether the widget disappeared.
            // ---------------------------------------------------------
            let mut wf = CF_WAIT_FOR.clone();
            wf.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                Duration::from_millis(1_100),
            ))
            .into();
            wf.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                Duration::from_secs(7).into(),
            )
            .into();
            wf.page_navigations = true;
            let wait = Some(wf.clone());
            let _ = tokio::join!(
                page_wait(page, &wait),
                perform_smart_mouse_movement(page, viewport),
            );

            // ---------------------------------------------------------
            // k) Final check – if the Lemin widget vanished we are done.
            // ---------------------------------------------------------
            if let Ok(nc2) = page.outer_html_bytes().await {
                *b = nc2;
                if !detect_lemin(b.as_slice()) {
                    progressed = true;
                    break;
                }
            }

            // If we get here the puzzle was not solved – the outer loop will retry.
        }

        Ok::<(), CdpError>(())
    })
    .await;

    match page_result {
        Ok(_) => Ok(progressed),
        Err(_) => Err(CdpError::Timeout),
    }
}

#[cfg(all(feature = "chrome", feature = "real_browser"))]
#[derive(Debug, Clone)]
/// The RC tile reference.
pub struct RcTileRef<'a> {
    /// The id.
    pub id: u8,
    /// The img src.
    pub img_src: &'a str,
}

#[cfg(all(feature = "chrome", feature = "real_browser"))]
/// Enterprise challenge.
#[derive(Debug, Default, Clone)]
pub struct RcEnterpriseChallenge<'a> {
    /// e.g. "bridges" (from `<strong>bridges</strong>`)
    pub target: Option<&'a str>,
    /// full instruction line if you want it
    pub instruction_text: Option<&'a str>,
    /// The tile space.
    pub tiles: Vec<RcTileRef<'a>>,
    /// Has the verification button.
    pub has_verify_button: bool,
}

/// Byte‑wise equality (fast, zero‑allocation).  
/// Returns `true` iff `a` and `b` have the same length **and** identical bytes.
#[inline(always)]
#[cfg(all(feature = "chrome", feature = "real_browser"))]
fn memeq(a: &[u8], b: &[u8]) -> bool {
    a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x == y)
}

/// Search for `needle` in `haystack` starting at `start`.  
/// Returns the absolute index of the first match or `None` if not found.
#[inline(always)]
#[cfg(all(feature = "chrome", feature = "real_browser"))]
fn find(h: &[u8], needle: &[u8], start: usize) -> Option<usize> {
    let nl = needle.len();
    if nl == 0 || start >= h.len() || nl > h.len() - start {
        return None;
    }
    h[start..]
        .windows(nl)
        .position(|w| memeq(w, needle))
        .map(|p| start + p)
}

/// Find the next double‑quote (`"`) after `start`.  
/// Returns its absolute index or `None` if missing.
#[inline(always)]
#[cfg(all(feature = "chrome", feature = "real_browser"))]
fn find_quote_end(h: &[u8], start: usize) -> Option<usize> {
    h.get(start..)?
        .iter()
        .position(|&c| c == b'"')
        .map(|p| start + p)
}

/// Is `b` an ASCII digit (`0`‑`9`)?
#[inline(always)]
#[cfg(all(feature = "chrome", feature = "real_browser"))]
fn is_digit(b: u8) -> bool {
    b.is_ascii_digit()
}

/// Convert a single ASCII digit to `u8`. Returns `None` for non‑digits.
#[inline(always)]
#[cfg(all(feature = "chrome", feature = "real_browser"))]
fn parse_u8_1digit(b: u8) -> Option<u8> {
    if is_digit(b) {
        Some(b - b'0')
    } else {
        None
    }
}

/// Extracts recaptcha enterprise image-grid metadata from the iframe inner HTML.
#[cfg(all(feature = "chrome", feature = "real_browser"))]
#[inline(always)]
pub fn extract_rc_enterprise_challenge<'a>(html: &'a [u8]) -> Option<RcEnterpriseChallenge<'a>> {
    // -----------------------------------------------------------------
    // Quick gate – all four guard patterns must be present.
    // -----------------------------------------------------------------
    // `RC_ENTERPRISE_GUARD_AC` contains the four patterns in the order
    // they appear in `RC_ENTERPRISE_GUARD_PATTERNS`.  We check each one
    // individually because we need **all** of them.
    let mut guard_hits = [false; 4];
    for m in RC_ENTERPRISE_GUARD_AC.find_iter(html) {
        guard_hits[m.pattern()] = true;
    }
    if !guard_hits.iter().all(|&b| b) {
        return None;
    }

    // -----------------------------------------------------------------
    // Does the page have a “Verify” button?
    // -----------------------------------------------------------------
    let has_verify_button = RC_VERIFY_BUTTON_AC.is_match(html);

    let mut out = RcEnterpriseChallenge {
        target: None,
        instruction_text: None,
        tiles: Vec::with_capacity(12),
        has_verify_button,
    };

    // -----------------------------------------------------------------
    // 1️⃣  Extract the *target* word (the word that appears inside the
    //      <strong …> … </strong> that is near the description).
    // -----------------------------------------------------------------
    const DESC_PAT: &[u8] = b"rc-imageselect-desc";
    const STRONG_OPEN: &[u8] = b"<strong";
    const GT: &[u8] = b">";
    const STRONG_CLOSE: &[u8] = b"</strong>";

    if let Some(desc_pos) = find(html, DESC_PAT, 0) {
        // Look forward a bounded window for the <strong> element.
        let win_end = (desc_pos + 900).min(html.len());

        if let Some(strong_pos) = find(html, STRONG_OPEN, desc_pos) {
            if strong_pos < win_end {
                if let Some(gt_pos) = find(html, GT, strong_pos) {
                    let txt_start = gt_pos + 1;
                    if let Some(close_pos) = find(html, STRONG_CLOSE, txt_start) {
                        if close_pos <= win_end {
                            if let Ok(word) = core::str::from_utf8(&html[txt_start..close_pos]) {
                                let word = word.trim();
                                if !word.is_empty() {
                                    out.target = Some(word);
                                }
                            }
                        }
                    }
                }
            }
        }

        // Optional – full description text (everything between the first ‘>’
        // after the descriptor and the next ‘<’).
        if let Some(tag_end) = find(html, b">", desc_pos) {
            let t0 = tag_end + 1;
            if let Some(t1) = find(html, b"<", t0) {
                if let Ok(txt) = core::str::from_utf8(&html[t0..t1]) {
                    let txt = txt.trim();
                    if !txt.is_empty() {
                        out.instruction_text = Some(txt);
                    }
                }
            }
        }
    }

    // -----------------------------------------------------------------
    // 2️⃣  Extract every tile (id + image URL).
    // -----------------------------------------------------------------
    const ID_PAT: &[u8] = b"id=\"";
    const SRC_PAT: &[u8] = b"src=\"";
    const PAYLOAD_PREFIX: &[u8] = b"https://www.google.com/recaptcha/enterprise/payload";

    // `RC_TILE_CLASS_AC` yields the start offset of every occurrence of
    // `rc-imageselect-tile`.  We iterate over those offsets instead of the
    // previous while‑loop that scanned the whole buffer.
    for m in RC_TILE_CLASS_AC.find_iter(html) {
        let tile_pos = m.start();

        // Back‑scan (max 240 bytes) for the id attribute that belongs to this tile.
        let back = tile_pos.saturating_sub(240);
        let id_pos = match find(html, ID_PAT, back) {
            Some(p) if p < tile_pos => p,
            _ => continue,
        };
        // The id is a single digit (0‑9) in the official widget.
        let id = match html
            .get(id_pos + ID_PAT.len())
            .copied()
            .and_then(parse_u8_1digit)
        {
            Some(v) => v,
            None => continue,
        };

        // Find the image src *after* the tile marker.
        let src_pos = match find(html, SRC_PAT, tile_pos) {
            Some(p) => p,
            None => continue,
        };
        let url_start = src_pos + SRC_PAT.len();

        // Ensure the URL really points to the Enterprise payload endpoint.
        if html.get(url_start..url_start + PAYLOAD_PREFIX.len()) != Some(PAYLOAD_PREFIX) {
            continue;
        }

        // The URL ends at the next double‑quote.
        let url_end = match find_quote_end(html, url_start) {
            Some(e) => e,
            None => continue,
        };
        let url = match core::str::from_utf8(&html[url_start..url_end]) {
            Ok(s) => s,
            Err(_) => continue,
        };

        // De‑duplicate tiles that may re‑appear after a re‑render.
        if !out.tiles.iter().any(|t| t.id == id) {
            out.tiles.push(RcTileRef { id, img_src: url });
        }
    }

    if out.tiles.is_empty() {
        None
    } else {
        Some(out)
    }
}
#[cfg(feature = "gemini")]
mod gemini {
    use super::*;
    // ----  no `anyhow` import any more  ----
    use serde::{Deserialize, Serialize};

    #[derive(Serialize)]
    struct Payload<'a> {
        /// Base‑64 data URL of the canvas (`data:image/png;base64,…`).
        image: &'a str,
        /// Prompt that makes Gemini return the **horizontal pixel offset** of the
        /// missing piece.
        prompt: &'static str,
    }

    #[derive(Deserialize)]
    struct GeminiResponse {
        /// X‑offset of the gap (relative to the left edge of the image).
        x: f64,
    }

    /// Calls Gemini‑Pro‑Vision and returns the x‑coordinate of the gap.
    ///
    /// The function now returns a plain `Result<f64, Box<dyn std::error::Error>>`,
    /// which works with the `?` operator for every error type that `reqwest`
    /// (and `serde_json`) may produce.
    pub async fn solve_with_gemini(
        api_key: &str,
        image_dataurl: &str,
    ) -> Result<f64, Box<dyn std::error::Error>> {
        // Prompt that works best for GeeTest sliders.
        const PROMPT: &str = r#"
You are shown a screenshot of a GeeTest sliding‑puzzle captcha.
The image contains a background with a single missing puzzle piece cut‑out.
Return **only** the horizontal pixel offset (integer or float) of the left edge of the missing piece
measured from the left border of the image.
Do NOT return any extra text, JSON keys, or explanations.
"#;

        let payload = Payload {
            image: image_dataurl,
            prompt: PROMPT,
        };

        let url = format!(
            "{}:generateContent?key={}",
            *GEMINI_VISION_ENDPOINT, api_key
        );

        // All intermediate errors (`reqwest::Error`, `serde_json::Error`, …)
        // are automatically converted into `Box<dyn Error>` via the `From`
        // implementations that the standard library provides.
        let resp = GEMINI_CLIENT
            .post(&url)
            .json(&payload)
            .send()
            .await?
            .error_for_status()?
            .json::<GeminiResponse>()
            .await?;

        Ok(resp.x)
    }
}

#[cfg(all(feature = "chrome", feature = "real_browser"))]
/// In page geetest helper.
pub async fn solve_geetest_with_inpage_helper(
    page: &Page,
    canvas_dataurl: &str,
    timeout_ms: u64,
) -> Result<f64, CdpError> {
    // -----------------------------------------------------------------
    // 1️⃣  Encode the data‑url as a JSON string so that it can be safely
    //     interpolated into the JS source.
    // -----------------------------------------------------------------
    let js_literal = serde_json::to_string(canvas_dataurl)
        .map_err(|e| CdpError::msg(format!("JSON encode error: {e}")))?;

    // -----------------------------------------------------------------
    // 2️⃣  The in‑page helper script.
    // -----------------------------------------------------------------
    //    • Creates a `LanguageModel` (the same model Chrome exposes to
    //      extensions).
    //    • Downloads the image from the data‑url, sends it together with a
    //      short prompt that asks for *only* the horizontal offset.
    //    • Returns that offset as a plain number (or `null` on any error).
    // -----------------------------------------------------------------
    let script = format!(
        r#"(async () => {{
            try {{
                const session = await LanguageModel.create({{
                    expectedInputs: [
                        {{ type: "image" }},
                        {{ type: "text", languages: ["en"] }},
                    ],
                    expectedOutputs: [{{ type: "text", languages: ["en"] }}],
                }});
                const imgResp = await fetch({js_literal});
                if (!imgResp.ok) return null;
                const blob = await imgResp.blob();

                const prompt = [{{
                    role: "user",
                    content: [
                        {{ type: "image", value: blob }},
                        {{ type: "text", value: "Return only the horizontal pixel offset (as a number) of the missing puzzle piece gap in this image." }},
                    ],
                }}];

                const answer = await session.prompt(prompt);
                const txt = (answer ?? "").toString().trim();
                const num = parseFloat(txt);
                return isNaN(num) ? null : num;
            }} catch (e) {{
                throw e;
            }}
        }})()"#
    );

    let params = EvaluateParams::builder()
        .expression(&script)
        .await_promise(true)
        .build()
        .map_err(|e| CdpError::msg(format!("evaluate params: {e}")))?;

    let eval_fut = page.evaluate(params);

    let eval_outcome = tokio::time::timeout(Duration::from_millis(timeout_ms + 5_000), eval_fut)
        .await
        .map_err(|_| CdpError::Timeout)?; // outer timeout → CdpError::Timeout

    // -----------------------------------------------------------------
    // 4️⃣  Distinguish three cases:
    //     a) The script succeeded (`Ok(EvaluationResult)`).
    //     b) The script threw → we get `Err(CdpError)`.  If the error
    //        signals a missing helper we fall back, otherwise we bubble it.
    //     c) The script succeeded but returned no numeric value.
    // -----------------------------------------------------------------
    let eval_res = match eval_outcome {
        Ok(res) => res,
        Err(err) => {
            if is_missing_helper_error(&err) {
                #[cfg(feature = "gemini")]
                {
                    let api_key = std::env::var("GEMINI_API_KEY")
                        .map_err(|_| CdpError::msg("GEMINI_API_KEY not set"))?;
                    return gemini::solve_with_gemini(&api_key, canvas_dataurl)
                        .await
                        .map_err(|e| CdpError::msg(format!("Gemini external error: {e}")));
                }

                #[cfg(not(feature = "gemini"))]
                {
                    // No Gemini compiled – return centre of track.
                    return Ok(0.0);
                }
            } else {
                // Some other Chrome‑side error – propagate it.
                return Err(err);
            }
        }
    };

    let maybe_offset = match eval_res.value() {
        Some(v) => match v {
            serde_json::Value::Number(n) => n.as_f64(),
            serde_json::Value::String(s) => s.parse::<f64>().ok(),
            _ => None,
        },
        None => None,
    };

    if let Some(off) = maybe_offset {
        return Ok(off);
    }

    Err(CdpError::msg(
        "In‑page Gemini helper returned no numeric result",
    ))
}

/// Geetest solving
#[cfg(all(feature = "chrome", feature = "real_browser"))]
#[inline(always)]
pub async fn geetest_handle(
    b: &mut Vec<u8>,
    page: &Page,
    viewport: &Option<crate::configuration::Viewport>,
) -> Result<bool, CdpError> {
    // -----------------------------------------------------------------
    // Fast gate – bail out early if the page does not look like GeeTest.
    // -----------------------------------------------------------------
    if !looks_like_geetest(b.as_slice()) {
        return Ok(false);
    }

    let mut progressed = false;

    // -----------------------------------------------------------------
    // Whole routine lives inside a 30 s timeout (same pattern as the rest
    // of the code‑base).
    // -----------------------------------------------------------------
    let page_result = tokio::time::timeout(Duration::from_secs(30), async {
        // Disable the network cache + a little “human” mouse movement.
        let _ = tokio::join!(
            page.disable_network_cache(true),
            perform_smart_mouse_movement(page, viewport)
        );

        for _ in 0..10 {
            // -------------------------------------------------------------
            // a) Refresh the HTML source.
            // -------------------------------------------------------------
            if let Ok(cur) = page.outer_html_bytes().await {
                *b = cur;
            }

            // -------------------------------------------------------------
            // b) If GeeTest vanished → success.
            // -------------------------------------------------------------
            if !looks_like_geetest(b.as_slice()) {
                progressed = true;
                break;
            }

            // -------------------------------------------------------------
            // c) Still loading?  Wait like Cloudflare.
            // -------------------------------------------------------------
            if looks_like_geetest_loading(b.as_slice()) {
                let mut wait_for = CF_WAIT_FOR.clone();
                wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                    Duration::from_millis(1_000),
                ))
                .into();
                wait_for.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                    Duration::from_secs(7).into(),
                )
                .into();
                wait_for.page_navigations = true;
                let wait = Some(wait_for.clone());
                let _ = tokio::join!(
                    page_wait(page, &wait),
                    perform_smart_mouse_movement(page, viewport),
                );
                continue;
            }

            // -------------------------------------------------------------
            // d) Click the “Click to verify” radar.
            // -------------------------------------------------------------
            let mut clicked = false;
            if let Ok(els) = page.find_elements_pierced(r#".geetest_radar"#).await {
                if let Some(el) = els.into_iter().next() {
                    clicked = match el.clickable_point().await {
                        Ok(p) => page.click(p).await.is_ok() || el.click().await.is_ok(),
                        Err(_) => el.click().await.is_ok(),
                    };
                }
            }
            // Fallback element.
            if !clicked {
                if let Ok(els) = page
                    .find_elements_pierced(r#".geetest_radar_tip_content"#)
                    .await
                {
                    if let Some(el) = els.into_iter().next() {
                        clicked = match el.clickable_point().await {
                            Ok(p) => page.click(p).await.is_ok() || el.click().await.is_ok(),
                            Err(_) => el.click().await.is_ok(),
                        };
                    }
                }
            }

            // -------------------------------------------------------------
            // e) Short wait after the click so the widget can render.
            // -------------------------------------------------------------
            let mut wait_for = CF_WAIT_FOR.clone();
            wait_for.delay = crate::features::chrome_common::WaitForDelay::new(Some(if clicked {
                Duration::from_millis(900)
            } else {
                Duration::from_millis(700)
            }))
            .into();
            wait_for.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                Duration::from_secs(6).into(),
            )
            .into();
            wait_for.page_navigations = true;
            let wait = Some(wait_for.clone());
            let _ = tokio::join!(
                page_wait(page, &wait),
                perform_smart_mouse_movement(page, viewport),
            );

            // -------------------------------------------------------------
            // f) Refresh HTML again – now the slider should be visible.
            // -------------------------------------------------------------
            if let Ok(nc) = page.outer_html_bytes().await {
                *b = nc;

                if looks_like_geetest_challenge_visible(b.as_slice()) {
                    // -------------------------------------------------
                    //   🎯  ***  SOLVE THE SLIDER  ***  🎯
                    // -------------------------------------------------
                    // 1️⃣  Grab the *track* (the gray bar the button slides on)
                    //     and the slider button.
                    //     Try the v3 selectors first; fall back to the v4 ones.
                    // -------------------------------------------------
                    async fn first_of(
                        page: &Page,
                        sel_a: &str,
                        sel_b: &str,
                    ) -> Result<chromiumoxide::Element, CdpError> {
                        // Try selector A.
                        if let Ok(els) = page.find_elements_pierced(sel_a).await {
                            if let Some(el) = els.into_iter().next() {
                                return Ok(el);
                            }
                        }
                        // Fallback to selector B.
                        let els = page.find_elements_pierced(sel_b).await?;
                        let el = els.into_iter().next().ok_or_else(|| {
                            CdpError::msg(format!("neither {sel_a} nor {sel_b} found"))
                        })?;
                        Ok(el)
                    }

                    // Track – v3: .geetest_slicebg  |  v4: .geetest_wrap
                    let track_el = first_of(page, ".geetest_slicebg", ".geetest_wrap").await?;
                    let track_bb = track_el.bounding_box().await?;

                    // Button – v3: .geetest_slider_button  |  v4: .geetest_btn
                    let btn_el = first_of(page, ".geetest_slider_button", ".geetest_btn").await?;
                    let btn_bb = btn_el.bounding_box().await?;

                    // -------------------------------------------------
                    // 2️⃣  Locate the *canvas* that holds the puzzle image.
                    // -------------------------------------------------
                    let canvas_el = page
                        .find_elements_pierced(r#".geetest_canvas_slice.geetest_absolute"#)
                        .await?
                        .into_iter()
                        .next()
                        .ok_or_else(|| CdpError::msg("canvas element not found"))?;

                    // -------------------------------------------------
                    // 3️⃣  Pull the canvas data‑URL using the element we just
                    //     fetched (no unused‑variable warning).
                    // -------------------------------------------------
                    let dataurl: String = {
                        let call = CallFunctionOnParams::builder()
                            .object_id(canvas_el.remote_object_id.clone())
                            .function_declaration("(function(){ return this.toDataURL(); })")
                            .await_promise(true)
                            .build()
                            .map_err(|e| CdpError::msg(format!("call function params: {e}")))?;

                        // `page.evaluate_function` returns an `EvaluationResult`.
                        let eval_res = page.evaluate_function(call).await?;
                        eval_res
                            .value()
                            .and_then(|v| v.as_str().map(|s| s.to_owned()))
                            .ok_or_else(|| {
                                CdpError::msg("Failed to extract data‑url from canvas")
                            })?
                    };

                    // -------------------------------------------------
                    // 4️⃣  Try the in‑page Gemini helper first.  If it does not
                    //     exist we fall back to the external Gemini API (or the
                    //     centre‑of‑track when the gemini feature is disabled).
                    // -------------------------------------------------
                    let gap_x = match solve_geetest_with_inpage_helper(page, &dataurl, 20_000).await
                    {
                        Ok(x) => x,
                        Err(e) if is_missing_helper_error(&e) => {
                            #[cfg(feature = "gemini")]
                            {
                                let api_key = std::env::var("GEMINI_API_KEY")
                                    .map_err(|_| CdpError::msg("GEMINI_API_KEY not set"))?;
                                gemini::solve_with_gemini(&api_key, &dataurl)
                                    .await
                                    .map_err(|e| {
                                        CdpError::msg(format!("Gemini external error: {e}"))
                                    })?
                            }

                            #[cfg(not(feature = "gemini"))]
                            {
                                // centre of the track – old hard‑coded fallback.
                                (track_bb.width * 0.5) as f64
                            }
                        }
                        Err(e) => return Err(e), // real Chrome error – bubble up
                    };

                    // -------------------------------------------------
                    // 5️⃣  Convert the canvas‑relative offset into a *page*
                    //     coordinate.
                    // -------------------------------------------------
                    let canvas_width: f64 = page
                        .evaluate(format!(
                            "document.querySelector('{}').width",
                            ".geetest_canvas_slice.geetest_absolute"
                        ))
                        .await?
                        .into_value()?;

                    let proportion = (gap_x / canvas_width).clamp(0.0, 1.0);
                    let target_x = track_bb.x + proportion * track_bb.width;

                    // -------------------------------------------------
                    // 6️⃣  Build the drag points.
                    // -------------------------------------------------
                    let from = Point {
                        x: btn_bb.x + btn_bb.width * 0.5,
                        y: btn_bb.y + btn_bb.height * 0.5,
                    };
                    let to = Point {
                        x: target_x,
                        y: track_bb.y + track_bb.height * 0.5,
                    };

                    // -------------------------------------------------
                    // 7️⃣  Perform the drag.
                    // -------------------------------------------------
                    let _ = page.click_and_drag(from, to).await;

                    // -------------------------------------------------
                    // 8️⃣  Wait a little, then verify whether the widget vanished.
                    // -------------------------------------------------
                    let mut wf = CF_WAIT_FOR.clone();
                    wf.delay = crate::features::chrome_common::WaitForDelay::new(Some(
                        Duration::from_millis(1_100),
                    ))
                    .into();
                    wf.idle_network = crate::features::chrome_common::WaitForIdleNetwork::new(
                        Duration::from_secs(7).into(),
                    )
                    .into();
                    wf.page_navigations = true;
                    let wait = Some(wf.clone());
                    let _ = tokio::join!(
                        page_wait(page, &wait),
                        perform_smart_mouse_movement(page, viewport),
                    );

                    // Refresh the HTML one final time.
                    if let Ok(nc2) = page.outer_html_bytes().await {
                        *b = nc2;
                        if !looks_like_geetest(b.as_slice()) {
                            progressed = true;
                            break;
                        }
                    }

                    // If we are still here the slider failed – loop again (max 10).
                    continue;
                }

                // If the widget disappeared after any step, we are done.
                if !looks_like_geetest(b.as_slice()) {
                    progressed = true;
                    break;
                }
            }
        }

        Ok::<(), CdpError>(())
    })
    .await;

    match page_result {
        Ok(_) => Ok(progressed),
        Err(_) => Err(CdpError::Timeout),
    }
}