forked from Emmanuel-Rods/SnapBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapbot.js
More file actions
1148 lines (1019 loc) · 37.6 KB
/
Copy pathsnapbot.js
File metadata and controls
1148 lines (1019 loc) · 37.6 KB
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
import puppeteer from "puppeteer-extra";
import Stealth from "puppeteer-extra-plugin-stealth";
puppeteer.use(Stealth());
import fs from "fs";
import fsPromise from "fs/promises";
import path from "path";
function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
const lastTestedVersion = "v13.38.0";
export default class SnapBot {
constructor() {
this.page = null;
this.browser = null;
}
async launchSnapchat(obj, cookiefile) {
try {
const options = {
...obj,
// executablePath: "/usr/bin/google-chrome", // for docker
};
this.browser = await puppeteer.launch(options);
const cookiesPath = cookiefile
? (cookiefile.includes("/") || cookiefile.includes("\\")
? cookiefile
: path.join(process.env.COOKIES_DIR || path.resolve(process.cwd(), "data", "cookies"), `${cookiefile}-cookies.json`))
: null;
const context = this.browser.defaultBrowserContext();
await context.overridePermissions("https://web.snapchat.com", [
"camera",
"microphone",
]);
this.page = await context.newPage();
await this.page.setViewport({
width: 1920,
height: 1080,
deviceScaleFactor: 1,
});
await this.page.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
);
// Load cookies if available (before navigation)
if (cookiesPath && fs.existsSync(cookiesPath)) {
try {
const cookiesString = fs.readFileSync(cookiesPath, "utf-8");
const cookies = JSON.parse(cookiesString);
const normalized = cookies.map((c) => (c.url || c.domain ? c : { ...c, url: "https://web.snapchat.com" }));
await this.page.setCookie(...normalized);
console.log("Cookies set from:", cookiesPath);
} catch (error) {
console.error("Error applying cookies from file", cookiesPath, error);
}
}
//gets the version
this.page.on("console", (msg) => {
if (msg.type() === "log") {
const text = msg.text();
if (text.includes("Snapchat")) {
console.log("Snapchat for Web Build info:", text);
const version = text.match(/v\d+\.\d+\.\d+/);
const currentVersion = version[0];
console.log("Version", currentVersion);
//check version
if (currentVersion != lastTestedVersion) {
console.warn(
`⚠️ Warning: Some methods were last tested on version ${lastTestedVersion} \n\n` +
`Detected current version is ${currentVersion}\n\n` +
`Some features might not work properly.\n` +
`If you encounter issues, please try updating the project using 'git pull'.\n` +
`If the problem persists, consider raising an issue or contacting the developer.`
);
}
}
}
});
// Go directly to Snapchat Web app
await this.page.goto("https://web.snapchat.com/");
} catch (error) {
console.error(`Error while Starting Snapchat : ${error}`);
}
}
async login(credentials) {
const { username, password } = credentials;
if (username == "" || password == "") {
throw new Error("Credentials cannot be empty");
}
try {
// Ensure we're on the login screen
const loginFieldSelector = 'input[name="accountIdentifier"], #ai_input';
// short wait for network to settle
try {
await this.page.waitForNetworkIdle({ idleTime: 500, timeout: 5000 });
} catch (_) { }
let loginField = await this.page.$(loginFieldSelector);
if (!loginField) {
// Try clicking a visible "Log in" button/link if present
const loginCandidates = await this.page.$x(
"//a[normalize-space()='Log in' or normalize-space()='Log In'] | //button[normalize-space()='Log in' or normalize-space()='Log In']"
);
if (loginCandidates && loginCandidates.length > 0) {
console.log("Found 'Log in' button/link. Clicking...");
await loginCandidates[0].click();
try {
await this.page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: 30000 });
} catch (_) { }
} else {
// Fallback: go straight to accounts login
console.log("Directing to accounts login page...");
await this.page.goto("https://accounts.snapchat.com/accounts/v2/login", { waitUntil: 'domcontentloaded' });
}
await this.page.waitForSelector(loginFieldSelector, { visible: true, timeout: 30000 });
loginField = await this.page.$(loginFieldSelector);
}
// Enter username into the appropriate field
const loginBtn = await this.page.$('input[name="accountIdentifier"]');
const defaultLoginBtn = await this.page.$("#ai_input");
console.log("Entering username...");
if (loginBtn) {
await this.page.type('input[name="accountIdentifier"]', username, { delay: 100 });
} else if (defaultLoginBtn) {
await this.page.type('#ai_input', username, { delay: 100 });
}
// Submit username (some flows have a single submit button)
const submitBtn = await this.page.$("button[type='submit']");
if (submitBtn) await submitBtn.click();
} catch (e) {
console.log("Username field error:", e);
}
try {
//Enter Password
console.log("Waiting for password field...");
await this.page.waitForSelector("#password", {
visible: true,
timeout: 60000,
});
await this.page.type("#password", password, { delay: 100 });
console.log("Password field filled.");
} catch (e) {
console.log("Password field loading error:", e);
}
await this.page.click("button[type='submit']");
await delay(10000);
//click not now
try {
const notNowBtn = "button.NRgbw.eKaL7.Bnaur"; //NRgbw eKaL7 Bnaur
console.log("Checking for 'Not now' button...");
await this.page.waitForSelector(notNowBtn, {
visible: true,
timeout: 5000,
});
await this.page.click(notNowBtn);
console.log("Clicked 'Not now' button.");
} catch (e) {
console.log("Popup handling error or popup not found:", e);
}
await delay(1000);
}
async isLogged(timeout = 20000) {
// Robust detection: wait for either login form or app UI elements
const appSelector =
"#downshift-1-toggle-button, div.ReactVirtualized__Grid__innerScrollContainer, button[title=\"View friend requests\"]";
const loginSelector =
'input[name="accountIdentifier"], #ai_input, #password';
const start = Date.now();
console.log("Detecting authentication state...");
while (Date.now() - start < timeout) {
try {
const appEl = await this.page.$(appSelector);
if (appEl) {
console.log("Detected Snapchat Web UI – logged in");
return true;
}
const loginEl = await this.page.$(loginSelector);
if (loginEl) {
console.log("Detected login form – login required");
return false;
}
} catch (e) {
// ignore transient errors while DOM updates
}
await delay(500);
}
// Fallback: infer from URL if selectors did not appear in time
const url = this.page.url();
console.warn(
`Auth state uncertain after timeout. Current URL: ${url}. Assuming not logged in if login selectors appear later.`
);
const appElFinal = await this.page.$(appSelector);
return Boolean(appElFinal);
}
async ensureLoggedIn(credentials, options = { handlePopup: true, retry: 0 }) {
const { retry = 0, handlePopup = true } = options || {};
let logged = await this.isLogged();
if (!logged) {
await this.login(credentials);
await delay(1000);
if (handlePopup) {
try {
await this.handlePopup();
} catch (_) { }
}
logged = await this.isLogged();
if (!logged && retry > 0) {
console.log("Auth still not confirmed, retrying login...");
return this.ensureLoggedIn(credentials, { handlePopup, retry: retry - 1 });
}
} else {
console.log("Bot is already Logged in");
if (handlePopup) {
try {
await this.handlePopup();
} catch (_) { }
}
}
return logged;
}
async handlePopup() {
try {
const notNowBtn = "button.NRgbw.eKaL7.Bnaur"; //NRgbw eKaL7 Bnaur
const notNowBtnHandle = await this.page.waitForSelector(notNowBtn, {
visible: true,
timeout: 5000,
});
console.log("Checking for 'Not now' button...");
if (notNowBtnHandle) {
await this.page.waitForSelector(notNowBtn, {
visible: true,
timeout: 5000,
});
await this.page.click(notNowBtn);
console.log("Clicked 'Not now' button.");
} else {
console.log("not found");
}
} catch (error) {
console.log(`could not find Popup`);
}
}
async captureSnap(obj) {
try {
//updated version here v2.0
let captureBtnFound = false;
const captureButtonSelector = "button.FBYjn.gK0xL.A7Cr_.m3ODJ";
const captureButton = await this.page.$(captureButtonSelector);
if (captureButton) {
const isVisible = (await captureButton.boundingBox()) !== null;
if (isVisible) {
await captureButton.click();
captureBtnFound = true;
}
}
if (!captureBtnFound) {
const svgButtonSelector = "button.qJKfS";
await delay(1000);
let isSVGbuttonFound = null;
let retries = 0;
const maxRetries = 3;
while (!isSVGbuttonFound && retries < maxRetries) {
try {
isSVGbuttonFound = await this.page.waitForSelector(
svgButtonSelector,
{
visible: true,
timeout: 15000,
}
);
} catch (error) {
console.log("Couldn't find the SVG selector, retrying...");
}
if (!isSVGbuttonFound) {
retries++;
console.log(`Retries left: ${maxRetries - retries}`);
await delay(1000);
}
}
if (isSVGbuttonFound) {
await this.page.click(svgButtonSelector);
console.log("clicked svg button");
} else {
console.log("SVG button not found after maximum retries.");
}
// Capture button
if (isSVGbuttonFound) {
await delay(1000);
const captureButtonSelector = "button.FBYjn.gK0xL.A7Cr_.m3ODJ"; //FBYjn gK0xL A7Cr_ m3ODJ
const captureButton = await this.page.waitForSelector(
captureButtonSelector,
{ visible: true }
);
await captureButton.click();
console.log("✅ Clicked the capture button");
}
}
await delay(3000);
// 📸 Add custom image if `obj.path` exists
if (obj.path) {
try {
const imageToBase64 = await fsPromise.readFile(obj.path, "base64");
const imageData = `data:image/png;base64,${imageToBase64}`;
// Wait for container
const containerSelector = "#snap-preview-container";
await this.page.waitForSelector(containerSelector, { visible: true });
await this.page.evaluate(
(containerSelector, imageData) => {
const container = document.querySelector(containerSelector);
if (container) {
const img = container.querySelector("img");
if (img) img.src = imageData;
}
},
containerSelector,
imageData
);
// await this.page.evaluate((imageData) => {
// const img = document.querySelector("#snap-preview-container img");
// if (img) img.src = imageData; // if imageData is already available in the page
// }, imageData);
console.log("✅ Image added successfully");
} catch (error) {
console.warn("⚠️ Error adding image:", error);
}
}
await delay(1000);
// 📝 Add caption if provided
if (obj.caption) {
await delay(2000);
const captionButtonSelector = 'button.eUb32[title="Add a caption"]';
await this.page.waitForSelector(captionButtonSelector, {
visible: true,
});
await this.page.click(captionButtonSelector);
await delay(1000);
const textareaSelector = 'textarea.B9QiX[aria-label="Caption Input"]';
await this.page.waitForSelector(textareaSelector, { visible: true });
await this.page.type(textareaSelector, obj.caption, { delay: 100 });
console.log("✅ Caption added successfully");
await delay(1000);
//caption pos
if (obj.position) {
const elementHandle = await this.page.$(textareaSelector);
if (elementHandle) {
const box = await elementHandle.boundingBox();
if (box) {
const startX = box.x + box.width / 2;
const startY = box.y + box.height / 2;
const endY = startY + obj.position;
await this.page.mouse.move(startX, startY); // Move to starting position
await this.page.mouse.down(); // Click and hold (start drag)
await this.page.mouse.move(startX, endY, { steps: 10 }); // Drag smoothly
await this.page.mouse.up(); // Release (drop)
}
}
}
}
} catch (error) {
console.error("❌ Error in capturing the snap:", error);
}
}
async recordVideo({ caption, durationMs = 5000 } = {}) {
try {
// Ensure capture UI is visible
const captureButtonSelector = "button.FBYjn.gK0xL.A7Cr_.m3ODJ";
let captureButton = await this.page.$(captureButtonSelector);
if (!captureButton) {
const svgButtonSelector = "button.qJKfS";
await delay(1000);
try {
const isSVGbuttonFound = await this.page.waitForSelector(svgButtonSelector, {
visible: true,
timeout: 15000,
});
if (isSVGbuttonFound) {
await this.page.click(svgButtonSelector);
await delay(1000);
}
} catch (_) { }
captureButton = await this.page.waitForSelector(captureButtonSelector, {
visible: true,
timeout: 15000,
});
}
// Hold mouse on capture button to record
const box = await captureButton.boundingBox();
if (!box) throw new Error("Capture button not visible for recording");
const x = box.x + box.width / 2;
const y = box.y + box.height / 2;
await this.page.mouse.move(x, y);
await this.page.mouse.down();
await delay(durationMs);
await this.page.mouse.up();
console.log(`🎥 Recorded video for ~${durationMs}ms`);
await delay(1500);
// Add caption if provided (same UI flow as captureSnap)
if (caption) {
const captionButtonSelector = 'button.eUb32[title="Add a caption"]';
await this.page.waitForSelector(captionButtonSelector, { visible: true });
await this.page.click(captionButtonSelector);
await delay(500);
const textareaSelector = 'textarea.B9QiX[aria-label="Caption Input"]';
await this.page.waitForSelector(textareaSelector, { visible: true });
await this.page.type(textareaSelector, caption, { delay: 100 });
console.log("✅ Caption added to video");
}
} catch (error) {
console.error("❌ Error in recording video:", error);
}
}
async send(person) {
try {
const button = await this.page.$("button.YatIx.fGS78.eKaL7.Bnaur"); //updated this
if (button) {
console.log("Button found!");
await button.click();
} else {
console.log("Button not found.");
}
await delay(1000);
let selected = "";
person = person.toLowerCase();
if (person == "bestfriends") {
selected = "ul.UxcmY li div.Ewflr.cDeBk.A8BRr ";
} else if (person == "groups") {
selected = "li div.RbA83";
} else if (person == "friends") {
selected = "li div.Ewflr";
} else if (person == "all") {
console.log("not implemented yet");
} else {
throw new Error("Option not found");
}
const accounts = await this.page.$$(selected);
for (const account of accounts) {
const isFriendVisible = await account.evaluate(
(el) => el.offsetWidth > 0 && el.offsetHeight > 0
); // Check if the div is visible
if (isFriendVisible) {
await account.click(); // Click on the div element
} else {
console.log("account not found.");
}
}
const sendButton = await this.page.$("button[type='submit']"); //YatIx q5eEJ eKaL7 Bnaur
await sendButton.click();
delay(5000);
} catch (error) {
console.error("Error while sending snap", error);
}
}
// Upload a video file from the computer
async uploadVideo({ videoPath, caption }) {
try {
console.log('📹 Uploading video from file...');
// Dismiss any popups
await this.page.evaluate(() => {
const buttons = document.querySelectorAll('button');
for (const btn of buttons) {
if (btn.textContent?.trim() === 'Got it!') {
btn.click();
break;
}
}
});
await delay(500);
// Find file input element
let fileInput = await this.page.$('input[type="file"][accept*="video"]');
if (!fileInput) {
fileInput = await this.page.$('input[type="file"]');
}
if (!fileInput) {
// Try clicking gallery/upload button first
// Selectors based on common UI patterns, might need adjustment
const uploadSelectors = [
'button[title*="Upload"]',
'button[aria-label*="Upload"]',
'button[title*="Gallery"]',
'button[aria-label*="Gallery"]',
'button[title="Memories"]',
'button.gallery-button'
];
for (const selector of uploadSelectors) {
const btn = await this.page.$(selector);
if (btn) {
await btn.click();
await delay(1000);
fileInput = await this.page.$('input[type="file"]');
if (fileInput) break;
}
}
}
if (fileInput) {
await fileInput.uploadFile(videoPath);
console.log(' ✅ Video file uploaded');
await delay(5000); // Wait for video to process
} else {
throw new Error('Could not find file input element');
}
// Add caption if provided
if (caption) {
const captionBtn = await this.page.$('button.eUb32[title="Add a caption"]');
if (captionBtn) {
await captionBtn.click();
await delay(500);
const textareaSelector = 'textarea.B9QiX[aria-label="Caption Input"]';
await this.page.waitForSelector(textareaSelector, { visible: true });
await this.page.type(textareaSelector, caption, { delay: 50 });
console.log(' ✅ Caption added');
}
}
console.log('✅ Video ready to send!');
return true;
} catch (error) {
console.error('❌ Error uploading video:', error);
throw error;
}
}
// Post to My Story
async postToMyStory() {
try {
console.log("Looking for story send button...");
const button = await this.page.$("button.YatIx.fGS78.eKaL7.Bnaur");
if (button) {
await button.click();
await delay(1000);
// Look for "My Story" option
const myStorySelector = "div[aria-label='My Story'], button[title='My Story'], div:has-text('My Story')";
const myStoryButton = await this.page.$(myStorySelector);
if (myStoryButton) {
await myStoryButton.click();
console.log("✅ Selected My Story");
} else {
// Fallback: click first story option (usually My Story)
const storyOptions = await this.page.$$("ul.UxcmY li div");
if (storyOptions && storyOptions.length > 0) {
await storyOptions[0].click();
console.log("✅ Selected first story option (likely My Story)");
}
}
await delay(500);
const sendButton = await this.page.$("button[type='submit']");
if (sendButton) {
await sendButton.click();
console.log("✅ Posted to My Story!");
await delay(2000);
}
} else {
throw new Error("Send button not found");
}
} catch (error) {
console.error("Error posting to My Story:", error);
throw error;
}
}
// Post to Spotlight
async postToSpotlight() {
try {
console.log("Looking for story send button...");
const button = await this.page.$("button.YatIx.fGS78.eKaL7.Bnaur");
if (button) {
await button.click();
await delay(1000);
// Look for "Spotlight" or "Public" option
const spotlightSelectors = [
"div[aria-label='Spotlight']",
"button[title='Spotlight']",
"div:has-text('Spotlight')",
"div:has-text('Public Story')"
];
let spotlightButton = null;
for (const selector of spotlightSelectors) {
spotlightButton = await this.page.$(selector);
if (spotlightButton) break;
}
if (spotlightButton) {
await spotlightButton.click();
console.log("✅ Selected Spotlight");
} else {
// Fallback: look in list items
const storyOptions = await this.page.$$("ul.UxcmY li div");
// Spotlight is usually second or third option
if (storyOptions && storyOptions.length > 1) {
await storyOptions[1].click();
console.log("✅ Selected secondary story option (possibly Spotlight)");
}
}
await delay(500);
const sendButton = await this.page.$("button[type='submit']");
if (sendButton) {
await sendButton.click();
console.log("✅ Posted to Spotlight!");
await delay(2000);
}
} else {
throw new Error("Send button not found");
}
} catch (error) {
console.error("Error posting to Spotlight:", error);
throw error;
}
}
// Select and send to specific recipients by display name
async sendToRecipients(names = []) {
if (!Array.isArray(names) || names.length === 0) {
throw new Error("names array is required for sendToRecipients");
}
// Open the send overlay
const button = await this.page.$("button.YatIx.fGS78.eKaL7.Bnaur");
if (button) {
await button.click();
}
await delay(1000);
const lower = names.map((n) => String(n).trim().toLowerCase());
// Wait for list of recipients and click matches
await this.page.waitForSelector("div.ReactVirtualized__Grid__innerScrollContainer");
const listItems = await this.page.$$("div[role='listitem']");
for (const listItem of listItems) {
const titleSpan = await listItem.$("span[id^='title-']");
if (!titleSpan) continue;
const name = await this.page.evaluate((el) => el.textContent.trim(), titleSpan);
if (lower.includes(name.trim().toLowerCase())) {
const isVisible = await listItem.evaluate((el) => el.offsetWidth > 0 && el.offsetHeight > 0);
if (isVisible) await listItem.click();
}
}
const sendButton = await this.page.$("button[type='submit']");
if (!sendButton) throw new Error("Send button not found");
await sendButton.click();
await delay(3000);
}
// Send a text message (string or string[]) to specific recipients by display name
async sendTextToRecipients(names = [], message = '') {
if (!Array.isArray(names) || names.length === 0) {
throw new Error('names array is required for sendTextToRecipients');
}
const msgs = Array.isArray(message) ? message : [String(message)];
await this.page.waitForSelector("div.ReactVirtualized__Grid__innerScrollContainer");
const lower = names.map((n) => String(n).trim().toLowerCase());
const listItems = await this.page.$$("div[role='listitem']");
for (const listItem of listItems) {
const titleSpan = await listItem.$("span[id^='title-']");
if (!titleSpan) continue;
const displayName = await this.page.evaluate((el) => el.textContent.trim(), titleSpan);
if (!lower.includes(displayName.toLowerCase())) continue;
// Open chat
await titleSpan.click();
await delay(500);
for (const msg of msgs) {
await this.page.waitForSelector('div[role="textbox"].euyIb');
await this.page.type('div[role="textbox"].euyIb', msg, { delay: 80 });
await this.page.keyboard.press('Enter');
await delay(200);
}
// Go back to the list (click title again)
try { await titleSpan.click(); } catch (_) { }
await delay(300);
}
}
async closeBrowser() {
await delay(5000);
await this.browser.close();
console.log("Snapchat closed");
}
async screenshot(obj) {
await this.page.screenshot(obj);
}
async logout() {
await this.page.waitForSelector("#downshift-1-toggle-button");
await this.page.click("#downshift-1-toggle-button");
await this.page.click("#downshift-1-item-9");
console.log("Logged Out");
await delay(12000);
}
async wait(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
//beta
async openFriendRequests() {
await this.page.waitForSelector('button[title="View friend requests"]');
const requests = await this.page.$('button[title="View friend requests"]');
await requests.click();
}
async listRecipients(limit = undefined) {
await this.page.waitForSelector("div.ReactVirtualized__Grid");
await this.page.waitForSelector("div.ReactVirtualized__Grid__innerScrollContainer");
const grid = await this.page.$("div.ReactVirtualized__Grid");
const seen = new Set();
const data = [];
const scrapeVisible = async () => {
const lists = await this.page.$$("div[role='listitem']");
for (const listItem of lists) {
const titleSpan = await listItem.$("span[id^='title-']");
if (!titleSpan) continue;
let id = await this.page.evaluate((el) => el.id, titleSpan);
const name = await this.page.evaluate((el) => el.textContent.trim(), titleSpan);
id = id.replace(/^title-/, "");
if (!seen.has(id)) {
seen.add(id);
data.push({ id, name });
if (limit && data.length >= limit) return true;
}
}
return false;
};
let stable = 0;
let lastCount = 0;
let iterations = 0;
// Initial scrape
let done = await scrapeVisible();
while (!done && iterations < 50 && stable < 3) {
iterations += 1;
await grid.evaluate((el) => { el.scrollBy(0, el.clientHeight); });
await delay(500);
done = await scrapeVisible();
if (data.length === lastCount) stable += 1; else stable = 0;
lastCount = data.length;
if (limit && data.length >= limit) break;
}
return limit ? data.slice(0, limit) : data;
}
async sendMessage(obj) {
await this.page.waitForSelector(
"div.ReactVirtualized__Grid__innerScrollContainer"
);
const lists = await this.page.$$("div[role='listitem']");
for (const listItem of lists) {
const titleSpan = await listItem.$("span[id^='title-']");
if (titleSpan) {
const id = await this.page.evaluate((el) => el.id, titleSpan);
let chatID = "title-" + obj.chat;
if (id === chatID) {
if (!obj.alreadyOpen) {
await titleSpan.click();
}
if (obj.message === "") {
// const cleanedID = obj.chat.replace(/^title-/, "");
// return this.extractChatData(cleanedID);
}
// if its an array
if (Array.isArray(obj.message)) {
for (let msg of obj.message) {
await this.page.waitForSelector('div[role="textbox"].euyIb');
await this.page.type('div[role="textbox"].euyIb', `${msg}`);
await this.page.keyboard.press("Enter");
}
}
//if string
if (typeof obj.message == "string") {
await this.page.waitForSelector('div[role="textbox"].euyIb');
await this.page.type('div[role="textbox"].euyIb', obj.message, {
delay: 200,
});
await this.page.keyboard.press("Enter");
}
if (obj.exit) {
// await delay(300);
await titleSpan.click(); // go back
}
}
}
}
}
async saveCookies(username) {
try {
const dir = process.env.COOKIES_DIR || path.resolve(process.cwd(), "data", "cookies");
try { fs.mkdirSync(dir, { recursive: true }); } catch (_) { }
const cookies = await this.page.cookies("https://web.snapchat.com");
const filePath = path.join(dir, `${username}-cookies.json`);
fs.writeFileSync(filePath, JSON.stringify(cookies, null, 2));
console.log("cookies saved to:", filePath);
} catch (error) {
console.error("Error in saving cookies", error);
}
}
async useCookies(username) {
try {
const dir = process.env.COOKIES_DIR || path.resolve(process.cwd(), "data", "cookies");
const filePath = path.join(dir, `${username}-cookies.json`);
const cookiesString = fs.readFileSync(filePath, "utf-8");
const cookies = JSON.parse(cookiesString);
const normalized = cookies.map((c) => (c.url || c.domain ? c : { ...c, url: "https://web.snapchat.com" }));
await this.page.setCookie(...normalized);
} catch (error) {
console.error("Error in using cookies", error);
}
}
async extractChatData(userId) {
return await this.page.evaluate((userId) => {
const output = [];
const $chatList = document.querySelector(`#cv-${userId}`);
if (!$chatList) return [];
const listItems = $chatList.querySelectorAll("li.T1yt2");
let currentTime = null;
let currentConvo = { time: "", conversation: [] };
listItems.forEach((li) => {
const timeElem = li.querySelector("time span");
if (timeElem) {
if (currentTime) output.push({ ...currentConvo });
currentTime = timeElem.textContent.trim();
currentConvo = { time: currentTime, conversation: [] };
return;
}
const messageBlocks = li.querySelectorAll("li");
if (messageBlocks.length > 0) {
messageBlocks.forEach((block) => {
let sender =
block.querySelector("header .nonIntl")?.textContent.trim() || "";
if (!sender) {
const borderElem = block.querySelector(".KB4Aq");
if (borderElem) {
const color = getComputedStyle(borderElem).borderColor;
if (color === "rgb(242, 60, 87)") sender = "Me";
else if (color === "rgb(14, 173, 255)") sender = "Eren Yeager";
else sender = "Unknown";
}
}
const texts = Array.from(block.querySelectorAll("span.ogn1z")).map(
(span) => span.textContent.trim()
);
texts.forEach((text) => {
if (text) currentConvo.conversation.push({ from: sender, text });
});
});
} else {
const borderElem = li.querySelector(".KB4Aq");
let sender = "Unknown";
if (borderElem) {
const color = getComputedStyle(borderElem).borderColor;
sender = color === "rgb(242, 60, 87)" ? "Me" : "Unknown";
}
const text = li.querySelector("span.ogn1z")?.textContent.trim();
if (text) currentConvo.conversation.push({ from: sender, text });
}
});
if (currentConvo.conversation.length > 0) {
output.push(currentConvo);
}
return output;
}, userId);
}
async userStatus() {
await this.page.waitForSelector(
"div.ReactVirtualized__Grid__innerScrollContainer"
);
const lists = await this.page.$$("div[role='listitem']");
const data = [];
for (const listItem of lists) {
const titleSpan = await listItem.$("span[id^='title-']");
if (titleSpan) {
const id = await this.page.evaluate((el) => el.id, titleSpan);
const name = await this.page.evaluate(
(el) => el.textContent.trim(),
titleSpan
);
// Get the status span container using the ID
const cleanedID = id.replace(/^title-/, "");
const statusContainer = await listItem.$(`#status-${cleanedID}`);
const statusParent = statusContainer
? await this.page.evaluateHandle(
(el) => el.parentElement,
statusContainer
)
: null;
let status = [];
if (statusParent) {
const statusSpans = await statusParent.$$("span");
status = await Promise.all(
statusSpans.map((span) =>
this.page.evaluate((el) => el.textContent.trim(), span)
)
);
}
let cleanedStatus = [
...new Set(
status
.map((text) => text?.trim())
.filter((text) => text && text !== "·")
),
];
let structuredStatus = {
type: cleanedStatus[0] || null,
time: cleanedStatus[1] || null,