-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacebook.js
More file actions
1034 lines (932 loc) · 45.3 KB
/
Copy pathfacebook.js
File metadata and controls
1034 lines (932 loc) · 45.3 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
const { workerData, parentPort } = require('worker_threads');
const puppeteer = require('puppeteer-extra');
const { createCursor } = require("ghost-cursor");
const stealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(stealthPlugin());
const fs = require('fs/promises');
//discord.js
const { Client, GatewayIntentBits, EmbedBuilder, ButtonBuilder, ActionRowBuilder, ButtonStyle } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.login(process.env.DISCORD_BOT_TOKEN);
//Closes browsers before terminating the task with facebook-delete command
parentPort.on('message', async (message) => {
if(message.action === 'closeBrowsers') {
await endTask();
}
else if(message.action === 'newAccount'){
try {
isDormant = false;
//set all new account data
burnerCookies = message.Cookies;
burnerUsername = message.Username;
burnerPassword = message.Password;
burnerProxy = message.Proxy;
burnerPlatform = message.Platform;
//close browser
if(mainBrowser != null){
await mainPage.close();
await mainBrowser.close();
mainBrowser = null;
}
startError = false;
//restart the main page
await start();
} catch (error) {
errorMessage("Error assigning new account: ", error);
}
}
});
client.on('ready', async () => {
try {
mainChannel = client.channels.cache.get(workerData.channel);
if(mainChannel == null){
mainChannel = await client.channels.fetch(workerData.channel);
}
//Adjust for testing -1228441458697179147
logChannel = client.channels.cache.get('1091532766522376243');
if(logChannel == null){
logChannel = await client.channels.fetch('1091532766522376243');
}
} catch (error) {
errorMessage('Error fetching channel', error);
}
//Start up
await start();
});
// Add cleanup logic on uncaught exception
process.on('uncaughtException', async (err) => {
try {
await logChannel.send('Uncaught Exception in ' + workerData.name + ': ' + err);
} catch (error) {
await logChannel.send('Error handling exception: ' + workerData.Name);
}
});
// Add cleanup logic on unhandled promise rejection
process.on('unhandledRejection', async (reason, promise) => {
try {
await logChannel.send('Unhandled Rejection in ' + workerData.name + ':' + reason);
} catch (error) {
await logChannel.send('Error handling rejection: ' + workerData.Name);
}
});
//error message send function
const errorMessage = (message, error) => {
try {
console.log(workerData.name + ': ' + message + ': ' + error);
logChannel.send(workerData.name + ': ' + message + ': ' + error);
} catch (error) {
logChannel.send('error with error message??? Who tf knows...' + error)
}
}
const endTask = async () => {
try {
await logChannel.send("Close Browsers");
burnerCookies = await mainPage.cookies();
if(mainBrowser != null){
await mainPage.close();
await mainBrowser.close();
mainBrowser = null;
}
if(itemBrowser != null){
await itemPage.close();
await itemBrowser.close();
itemBrowser = null;
}
parentPort.postMessage({messageCookies: messageCookies, cookies: burnerCookies});
} catch (error) {
errorMessage("Error closing browser: ", error);
}
}
//randomize time till post check
const getRandomInterval = () => {
try {
const minNumber = 520000; //9 mins
const maxNumber = 800000; //14 mins
const power = 1.5;
const random = Math.random();
const range = maxNumber - minNumber;
const number = minNumber + Math.pow(random, power) * range;
return Math.round(number);
} catch (error) {
errorMessage('error getting random interval', error);
}
}
//send content of the page to discord
const logPageContent = async (page) => {
try{
//html
const htmlContent = await page.content();
const { Readable } = require('stream');
const htmlStream = Readable.from([htmlContent]);
await logChannel.send({
files: [
{
attachment: htmlStream,
name: 'website.html',
},
],
});
//png
await page.screenshot({ path: 'screenshot.png' });
await logChannel.send({
files: ['screenshot.png'],
});
await fs.unlink('screenshot.png');
}catch(error){
errorMessage('error login content: ', error);
}
}
//pause for 0.5s-2s to humanize behavior
const pause = async () => {
try {
await new Promise(r => setTimeout(r, Math.floor(Math.random() * (1200 - 500 + 1)) + 500));
} catch (error) {
errorMessage('error with pause', error);
}
}
//convert platform string for a user agent
const platformConverter = (platform) => {
try {
if(platform === 'Windows'){
return 'Windows NT 10.0; Win64; x64';
}else if(platform === 'Linux'){
return 'X11; Linux x86_64';
}else if(platform === 'macOS'){
return 'Macintosh; Intel Mac OS X 10_15_7';
}
} catch (error) {
errorMessage('error with converting platform', error);
}
}
// Function to simulate typing with randomized speed
async function typeWithRandomSpeed(page, text) {
try {
for (const char of text) {
// Type a character
await page.keyboard.type(char, { delay: Math.floor(Math.random() * (100 - 50 + 1)) + 50 });
}
} catch (error) {
errorMessage('error with typing random speed', error);
}
}
//generates an array of possible prices based on the max price
const getPrices = () => {
let array = [];
let arrayValue = workerData.maxPrice;
try {
const generateArray = async (range, inc) => {
arrayValue = arrayValue + inc;
while(arrayValue <= workerData.maxPrice + range){
array.push(arrayValue);
arrayValue = arrayValue + inc;
}
}
if(workerData.maxPrice < 50){
generateArray(16, 2);
}else if(workerData.maxPrice < 100){
generateArray(40, 5);
}else if(workerData.maxPrice < 1000){
generateArray(80, 10);
}else if(workerData.maxPrice < 2500){
generateArray(160, 20);
}else if(workerData.maxPrice < 10000){
generateArray(400, 50);
}else if(workerData.maxPrice < 100000){
generateArray(1000, 100);
}else if(workerData.maxPrice < 500000){
generateArray(10000, 1000);
}
} catch (error) {
errorMessage('error with getting price array', error);
}
return array;
}
//timeout to rotate accounts
const accountRotation = () => {
setTimeout(async () => {
try {
let trys = 0;
while(isDormant == false && trys < 3){
console.log('task non dormant');
trys++;
await new Promise(r => setTimeout(r, 10000));
}
if(trys == 3){
logChannel.send('@everyone - is dormant thing');
}
burnerCookies = await mainPage.cookies();
//send message to main
parentPort.postMessage({action: 'rotateAccount', username: burnerUsername, cookies: burnerCookies});
accountRotation();
} catch (error) {
errorMessage("Error with account rotation: ", error);
}
}, (Math.random() * 7200000) + 14400000);//2-4 hours
}
const login = async () => {
try {
logChannel.send("Re-Login Required: " + burnerUsername);
//check for this weird shit
if(await mainPage.$('[title="Allow all cookies"]') != null){
await pause();
await mainCursor.click('[title="Allow all cookies"]');
try {
await mainPage.waitForNavigation();
} catch (error) {}
}
await mainCursor.click('[name="email"]');
await pause();
await typeWithRandomSpeed(mainPage, burnerUsername);
await pause();
await mainCursor.click('[name="pass"]');
await pause();
await typeWithRandomSpeed(mainPage, burnerPassword);
await pause();
await mainCursor.click('[name="login"]');
try{
await mainPage.waitForNavigation();
}catch (error) {}
if(!(mainPage.url()).includes('facebook.com/marketplace')){
await logPageContent(mainPage);
startError = true;
await logChannel.send("Ban on Re-login: " + errorMsg);
parentPort.postMessage({action: 'ban', username: burnerUsername});
}else{
//update burnerCookies
burnerCookies = await mainPage.cookies();
await setDistance();
mainPageInitiate = false;
}
} catch (error) {
startError = true;
await logChannel.send('error with re-login: ' + error);
parentPort.postMessage({action: 'rotateAccount', username: burnerUsername, cookies: null});
}
}
const sendMessage = async (link) => {
let messageCursor;
//browser with static isp
try {
itemBrowser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', `--proxy-server=${workerData.messageProxy}`],
timeout: 60000
});
let pages = await itemBrowser.pages();
itemPage = pages[0];
//close the notif popup
const context = itemBrowser.defaultBrowserContext();
context.overridePermissions("https://www.facebook.com", ["notifications"]);
//create a cursor
messageCursor = createCursor(itemPage);
//change the viewport
itemPage.setViewport({ width: 1366, height: 768 });
//change http headers
itemPage.setUserAgent(`Mozilla/5.0 (${platformConverter(burnerPlatform)}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36`);
itemPage.setExtraHTTPHeaders({
'Sec-Ch-Ua': 'Not.A/Brand";v="8", "Chromium";v="121", "Google Chrome";v="121',
'SEC-CH-UA-ARCH': '"x86"',
'Sec-Ch-Ua-Full-Version': "121.0.6167.185",
'SEC-CH-UA-MOBILE': '?0',
'Sec-Ch-Ua-Platform': `"${burnerPlatform}"`,
'SEC-CH-UA-PLATFORM-VERSION': '15.0.0',
'Referer': 'https://www.facebook.com/login'
});
//set cookies/login if the login was a success
await itemPage.setCookie(...messageCookies);
//network shit
await itemPage.setRequestInterception(true);
itemPage.on('request', async request => {
const resource = request.resourceType();
if(resource == 'image'){//resource != 'document' && resource != 'script' && resource != 'xhr' && resource != 'stylesheet'
request.abort();
}else{
request.continue();
}
});
} catch (error) {
errorMessage('Error with item page instantiation for cookie login', error);
}
try{
//navigate to the product page
await itemPage.goto(link, { waitUntil: 'networkidle0' });
//Update cookies
messageCookies = await itemPage.cookies();
messageCookies = messageCookies.filter(cookie => cookie.name === 'xs' || cookie.name === 'datr' || cookie.name === 'sb' || cookie.name === 'c_user');
//Send the message
if(!itemPage.url().includes('facebook.com/login')){
if(!itemPage.url().includes('unavailable_product')){
if(await itemPage.$('div.x1daaz14 [aria-label="Send seller a message"]')){//regular, pickup listing
console.log("local pickup only message sequence");
if(workerData.message != null){
await pause();
await messageCursor.click('div.x1daaz14 [aria-label="Send seller a message"]');
await itemPage.keyboard.press('Backspace');
await typeWithRandomSpeed(itemPage, 'div.x1daaz14 [aria-label="Send seller a message"]', workerData.message);
}
await pause();
await messageCursor.click('div.x1daaz14 div.x14vqqas div.xdt5ytf');
await itemPage.waitForSelector('[aria-label="Message Again"]');
mainChannel.send("Message Sent!");
}else if(await itemPage.$('[aria-label="Message"]') && await itemPage.$('span.x1xlr1w8.x1a1m0xk') == null){//shipping listing
console.log("shipping message sequence");
await pause();
await messageCursor.click('[aria-label="Message"]');
await itemPage.waitForSelector('[aria-label="Please type your message to the seller"]');
if(workerData.message != null){
await pause();
await messageCursor.click('[aria-label="Please type your message to the seller"]');
await typeWithRandomSpeed(itemPage, '[aria-label="Please type your message to the seller"]', workerData.message);
}
await pause();
//await itemPage.waitForSelector('[aria-label="Message Again"]'); //!Gotta test this
await messageCursor.click('[aria-label="Send Message"]');
await itemPage.waitForSelector('[aria-label="Message Again"]');
mainChannel.send("Message Sent!");
}else if(await itemPage.$('span.x1xlr1w8.x1a1m0xk')){//check for a regular pending/sold listing
let listingConditionText = await itemPage.evaluate(() => {return document.querySelector('span.x1xlr1w8.x1a1m0xk').innerText});
mainChannel.send("Message Failed: item " + listingConditionText);
}else if(await itemPage.$('span.xk50ysn.x1a1m0xk')){//Check for the weird out of stock thing //!I have no clue if this is actually a thing
let listingConditionText = await itemPage.evaluate(() => {return document.querySelector('span.xk50ysn.x1a1m0xk').innerText});
mainChannel.send("Message Failed: item " + listingConditionText);
}else{
mainChannel.send("Message Failed");
}
}else{
mainChannel.send("Product Unavailable");
}
}else{
mainChannel.send("Cookies Expired\n\tUpdate the cookies and restart the task to use messaging");
}
} catch (error){
errorMessage('Error with messaging', error);
}
try {
if(workerData.messageType != 1){//if its not auto messaging
await itemBrowser.close();
itemBrowser = null;
}
} catch (error) {
errorMessage('Error with closing itemBrowser', error);
}
}
let startError = false; //stops script on error
let isInitiation = true;
let mainBrowser;
let mainPage;
let itemPage;
let itemBrowser;
let mainListingStorage;
let isDormant = true; //true if task can be deleted
let mainCursor;
let prices = getPrices(); //array of all possible prices for max price
let mainPageInitiate;
let mainChannel;
let logChannel;
let startRetries = 0;
//changeable account stuff
let messageCookies = workerData.messageCookies;
let burnerCookies = workerData.burnerCookies;
let burnerUsername = workerData.burnerUsername;
let burnerPassword = workerData.burnerPassword;
let burnerProxy = workerData.burnerProxy;
let burnerPlatform = workerData.burnerPlatform;
const start = async () => {
try{
isDormant = false;
mainPageInitiate = true;
//initialize the static isp proxy page
mainBrowser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', `--proxy-server=${burnerProxy}`],
timeout: 60000
});
let pages = await mainBrowser.pages();
mainPage = pages[0];
// Listen for the 'targetdestroyed' event
/*mainBrowser.on('targetdestroyed', async (target) => {
if (target.type() === 'page') {
await logChannel.send('Page closed: ' + workerData.name);
//parentPort.postMessage({action: 'rotateAccount', username: burnerUsername, cookies: null});
}
});*/
//?Maybe check for specifically the main page close?
//close the notif popup
const context = mainBrowser.defaultBrowserContext();
context.overridePermissions("https://www.facebook.com", ["notifications"]);
//create a cursor
mainCursor = createCursor(mainPage);
//change the viewport
mainPage.setViewport({ width: 1366, height: 768 });
//change http headers
mainPage.setUserAgent(`Mozilla/5.0 (${platformConverter(burnerPlatform)}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36`);
mainPage.setExtraHTTPHeaders({
'Sec-Ch-Ua': 'Not.A/Brand";v="8", "Chromium";v="121", "Google Chrome";v="121',
'SEC-CH-UA-ARCH': '"x86"',
'Sec-Ch-Ua-Full-Version': "121.0.6167.185",
'SEC-CH-UA-MOBILE': '?0',
'Sec-Ch-Ua-Platform': `"${burnerPlatform}"`,
'SEC-CH-UA-PLATFORM-VERSION': '15.0.0',
'Referer': 'https://www.facebook.com/login'
});
//network shit
mainPage.on('response', async response => {
try {
//detect redirection
if ([300, 301, 302, 303, 307, 308].includes(response.status())) {
const redirectURL = await response.headers()['location'];
if(await redirectURL.split('?')[0] != (workerData.link).split('?')[0]){
mainPageInitiate = true;
console.log(`Redirected to: ${redirectURL}`);
logChannel.send(`${workerData.name} redirected to: ${redirectURL}`);
startError = true;
if(redirectURL.includes('/checkpoint/')){
try {
//await mainPage.reload();
await mainPage.waitForSelector('[aria-label="Dismiss"]', {timeout: 30000});
} catch (error) {logChannel.send("Error waiting for dismiss: " + error)}
await logPageContent(mainPage);
if(await mainPage.$('[aria-label="Dismiss"]') != null){
await pause();
await mainCursor.click('[aria-label="Dismiss"]');
await logChannel.send("dismiss warming");
mainPageInitiate = false;
}else{
await logChannel.send('Account banned: ' + burnerUsername);
console.log('Account banned: ' + burnerUsername);
//message the main script to delete the burner account
parentPort.postMessage({action: 'ban', username: burnerUsername});
}
}else if(redirectURL.includes('/login/?next')){
try{
await mainPage.waitForSelector('[name="email"]');
}catch(error){}
await login();
}else{
//message the main script to get a new accounts
logChannel.send("Rotate Account: " + burnerUsername);
if(mainBrowser != null){
await logPageContent(mainPage);
}
parentPort.postMessage({action: 'rotateAccount', username: burnerUsername, cookies: null});
}
}
}
}catch (error) {
parentPort.postMessage({action: 'rotateAccount', username: burnerUsername, cookies: null});
errorMessage("Error with handling network response", error);
if(mainBrowser != null){
await logPageContent(mainPage);
}
}
});
await mainPage.setRequestInterception(true);
mainPage.on('request', async request => {
const resource = request.resourceType();
if(mainPageInitiate){
if(resource != 'document' && resource != 'script' && resource != 'xhr' && resource != 'stylesheet' && resource != 'other' && resource != 'fetch'){
request.abort();
}else{
request.continue();
}
}else{
if(resource != 'document' && resource != 'xhr' && resource != 'fetch' && resource != 'other'){
request.abort();
}else{
request.continue();
}
}
});
//Set cookies in browser
await mainPage.setCookie(...burnerCookies);
//go to the search page
try {
await mainPage.goto(workerData.link, { waitUntil: 'load', timeout: 50000});//networkidle2
} catch (error) {await logChannel.send("Timeout on going to link")}
//update burnerCookies
if(startError == false){
burnerCookies = await mainPage.cookies();
// Detect the current language
const language = await mainPage.evaluate(() => document?.documentElement?.lang);
if (language != 'en' && language != null && language != '') {
logChannel.send('Language Wrong: ' + language + " -> " + burnerUsername);
startError = true;
parentPort.postMessage({action: 'languageWrong', username: burnerUsername});
}
}
//make sure the url is correct
if(await mainPage.url().split('?')[0] != (workerData.link).split('?')[0] && startError == false){
startError = true;
await logChannel.send("startError = true: " + workerData.Name);
if(await mainPage.$('[name="login"]') == null && !(mainPage.url()).includes('/login/?next')){
await logChannel.send("Login required: " + mainPage.url() + " at account: " + burnerUsername);
await mainPage.close();
await mainBrowser.close();
mainBrowser = null;
parentPort.postMessage({action: 'rotateAccount', username: burnerUsername, cookies: burnerCookies});
}
}else{
await setDistance();
}
isDormant = true;
}catch(error){
await logPageContent(mainPage);
if(startRetries < 2){
startRetries++;
await logChannel.send("Start Retry " + startRetries + ": " + workerData.name)
await mainPage.reload({ waitUntil: 'load', timeout: 50000});
start();
}else{
isDormant = true;
errorMessage('error with start', error);
}
}
}
const setDistance = async () => {
//set distance
if(startError == false){
try {
//apply a random element
await mainPage.waitForSelector('div.x1y1aw1k.xl56j7k > div.x1iyjqo2', {visible: true});
await pause();
await mainCursor.click('div.x1y1aw1k.xl56j7k > div.x1iyjqo2');
await mainPage.waitForSelector('div.x9f619.x14vqqas.xh8yej3', {visible: true});
await pause();
await mainCursor.click('div.x9f619.x14vqqas.xh8yej3');
await pause();
//click a random element
await mainCursor.click(`[role="listbox"] div.x4k7w5x > :nth-child(${Math.floor(Math.random() * 11 + 1)})`);
await pause();
await mainCursor.click('[aria-label="Apply"]');
//long pause
await new Promise(r => setTimeout(r, Math.random() * 15000 + 5000));
//apply real distance
await mainCursor.click('div.x1y1aw1k.xl56j7k > div.x1iyjqo2');
await mainPage.waitForSelector('div.x9f619.x14vqqas.xh8yej3', {visible: true});
await pause();
await mainCursor.click('div.x9f619.x14vqqas.xh8yej3');
await pause();
await typeWithRandomSpeed(mainPage, (workerData.distance).toString());
await pause();
await mainPage.keyboard.press("Enter");
await pause();
await mainCursor.click('[aria-label="Apply"]');
//wait for the results to update, we aren't concerned about time
await new Promise(r => setTimeout(r, 10000));
//finish rest of the start process
await setListingStorage();
if(isInitiation){
accountRotation();
interval();
isInitiation = false;
}
startRetries = 0;
mainPageInitiate = false;
} catch (error) {
await logPageContent(mainPage);
if(startRetries < 2){
startRetries++;
await logChannel.send("Distance Set Retry " + startRetries + ": " + workerData.name)
await mainPage.reload({ waitUntil: 'load', timeout: 50000});
if(mainBrowser != null){
if(mainPage != null){
await mainPage.close();
}
await mainBrowser.close();
}
start();
}else{
errorMessage('Error with setting distance', error);
//finish rest of the start process
await setListingStorage();
if(isInitiation){
accountRotation();
interval();
isInitiation = false;
}
startRetries = 0;
mainPageInitiate = false;
}
}
}
}
const setListingStorage = async () => {
// Set listingStorage, run once in the begging of the day
try{
if(startError == false){
mainListingStorage = await mainPage.evaluate(() => {
if(document.querySelector('div.xx6bls6') == null && document.querySelector('[aria-label="Browse Marketplace"]') == null){
let links = [document.querySelector("div.x1xfsgkm > :nth-child(1) div > :nth-child(1) a"), document.querySelector("div.x1xfsgkm > :nth-child(1) div > :nth-child(2) a"), document.querySelector("div.x1xfsgkm > :nth-child(1) div > :nth-child(3) a")];
return links.map((link) => {
if(link != null){
let href = link.href;
return href.substring(0, href.indexOf("?"));
}else{
return null;
}
})
}else{
return [null, null, null];
}
});
console.log("Main Storage: " + mainListingStorage);
}
}catch (error){
errorMessage('Error with setting listing storage', error);
}
}
//the meat and cheese
function interval() {
let reloadBlock = false;
let newPost;
let postArr = []; //**For SS MAX Testing */
setTimeout(async () => {
isDormant = false;
//get a value from the start of the array
let value;
try {
value = prices.splice((Math.floor(Math.random() * (prices.length - 6))), 1);
prices.push(value[0]);
} catch (error) {
errorMessage('error with getting price value', error);
}
let resultsRefreshMaxAttempts = 0;
const resultsRefresh = async () => {
try {
//change link for results change
await mainPage.goto((workerData.link).replace(/maxPrice=([^&]+)/, `maxPrice=${value}`));//, {waitUntil: 'networkidle2', timeout: 60000}
try {
await mainPage.waitForSelector(".x3ct3a4 a");
} catch (error) {await logChannel.send("Error waiting for listing selector")}
console.log(mainPage.url());
//if the listings dont exist on the page, refresh
if(await mainPage.$('.xbbxn1n .xqui205 [aria-label="Reload Page"]') != null){
reloadBlock = true;
burnerCookies = await mainPage.cookies();
parentPort.postMessage({action: 'rotateAccount', username: burnerUsername, cookies: burnerCookies});
logChannel.send("Reload block: " + workerData.name);
}else if(await mainPage.$(".x1lliihq .x3ct3a4 a") == null && await mainPage.$('[aria-label="Browse Marketplace"]') == null && await mainPage.$('div.xx6bls6') == null){
await mainPage.reload({waitUntil: 'domcontentloaded'});
await logPageContent(mainPage);
logChannel.send('Refresh for null .href error');
}
//check to make sure distance is correct
//!This still won't work :(
/*let actualDistance = await mainPage.evaluate(() => {return document.querySelector('.x1xmf6yo > div.x78zum5 > div > span').innerText});
if(!actualDistance.includes(" " + workerData.distance + " ")){
await logChannel.send("Distance is WRONG at: " + workerData.name + " Actual value: " + actualDistance + " Expected value: " + workerData.distance);
}*/
} catch(error) {
if(error.message.includes('TargetCloseError')){
logChannel.send("Page Closed");
if(mainBrowser != null){
await mainPage.close();
await mainBrowser.close();
mainBrowser = null;
await start();
}
}else if(error.message.includes('ERR_TUNNEL_CONNECTION_FAILED') && resultsRefreshMaxAttempts < 3){
resultsRefreshMaxAttempts++;
await resultsRefresh();
}else{
errorMessage('Error with results refresh', error);
}
}
};
await resultsRefresh();
if(reloadBlock == false){
try {
//check for new posts
newPost = await mainPage.evaluate(() => {
if(document.querySelector('div.xx6bls6') == null && document.querySelector('[aria-label="Browse Marketplace"]') == null){
let link = document.querySelector(".x3ct3a4 a").href;
return link.substring(0, link.indexOf("?"));
}else{
return null;
}
});
} catch (error) {
errorMessage('Error with getting results', error);
}
//make sure listing storage didn't get all fucked up
if(mainListingStorage == null){
await setListingStorage();
}
//newPost is actually new
if(mainListingStorage[0] != newPost && mainListingStorage[1] != newPost && mainListingStorage[2] != newPost && mainListingStorage[3] != newPost && newPost != null){
let isNotification = false;
let postNum = 1;
//get the price of the post
let price;
try {
price = await mainPage.evaluate(() => { return document.querySelector("div.x1xfsgkm > :nth-child(1) div > :nth-child(1) a span.x78zum5 span.x193iq5w").innerText });
if(price == 'FREE' || price == 'Free'){
price = 0;
}else{
const numbersOnly = price.match(/\d+/g);
if(numbersOnly == null){
await logChannel.send(price);
}else{
price = parseInt(numbersOnly.join(''), 10);
}
}
} catch (error) {
errorMessage('Error with getting price', error);
}
while(mainListingStorage[0] != newPost && mainListingStorage[1] != newPost && mainListingStorage[2] != newPost && mainListingStorage[3] != newPost && postNum <= 20 && newPost != null){
//check if "The price is right"
if(price <= workerData.maxPrice){
console.log("New Post: " + newPost + " post num: " + postNum);
isNotification = true;
let postObj;
let itemPageFullLoad = false;
try{
itemPage = await mainBrowser.newPage();
await itemPage.setRequestInterception(true);
itemPage.on('request', async request => {
const resource = request.resourceType();
if(itemPageFullLoad){
request.continue();
}else{
if(resource != 'document'){
request.abort();
}else{
request.continue();
}
}
});
//create a cursor
messageCursor = createCursor(itemPage);
//change the viewport
itemPage.setViewport({ width: 1366, height: 768 });
//change http headers
itemPage.setUserAgent(`Mozilla/5.0 (${platformConverter(burnerPlatform)}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36`);
itemPage.setExtraHTTPHeaders({
'Sec-Ch-Ua': 'Not.A/Brand";v="8", "Chromium";v="121", "Google Chrome";v="121',
'SEC-CH-UA-ARCH': '"x86"',
'Sec-Ch-Ua-Full-Version': "121.0.6167.185",
'SEC-CH-UA-MOBILE': '?0',
'Sec-Ch-Ua-Platform': `"${burnerPlatform}"`,
'SEC-CH-UA-PLATFORM-VERSION': '15.0.0',
'Referer': workerData.link
});
await itemPage.goto(newPost, { waitUntil: 'domcontentloaded', timeout: 60000});
}catch(error){
errorMessage('Error with product page initiation, no message', error);
}
//get post data
try{
//check for video
if(await itemPage.$('.xcg96fm img') == null){
itemPageFullLoad = true;
await itemPage.reload();
try {
await itemPage.waitForSelector('.xcg96fm img');
} catch (error) {await logChannel.send("Error waiting for image selector")}
}
//set post data obj
postObj = await itemPage.evaluate(() => {
const getValueOrDefault = (value) => {
return value !== null ? value : '';
};
return {
imgs: Array.from(document.querySelectorAll('[aria-label*="Thumbnail"] img'))?.map((img) => img?.src),
title: getValueOrDefault(document.querySelector('div.xyamay9 h1')?.innerText),
date: getValueOrDefault(document.querySelector('[aria-label="Buy now"]') != null ? document.querySelector('div.xyamay9 div.x6ikm8r > :nth-child(2)')?.innerText : (document.querySelector('div.x1yztbdb span.x1cpjm7i.x1sibtaa') != null ? document.querySelector('div.x1yztbdb span.x1cpjm7i.x1sibtaa')?.innerText : document.querySelector('div.x1xmf6yo > div > div:nth-child(2) span')?.innerText)),
description: getValueOrDefault(document.querySelector('div.xz9dl7a.x4uap5.xsag5q8.xkhd6sd.x126k92a span')?.innerText),
shipping: getValueOrDefault(document.querySelector('div.xyamay9 div.x6ikm8r') != null ? document.querySelector('div.xyamay9 div.x6ikm8r span')?.innerText : document.querySelector('div.xod5an3 div.x1gslohp span')?.innerText),
specifics: Array.from(document.querySelectorAll("div.x1n2onr6 > div:nth-child(5) > div.x1gslohp > div.xurb0ha"))?.map((el) => el.innerText),
ownerLink: getValueOrDefault(document.querySelector('div.xjp7ctv > div > div > div > div > .x1ja2u2z a')?.href),
ownerName: getValueOrDefault(document.querySelector('div.xjp7ctv > div > div > div > div > .x1ja2u2z a > span.xzsf02u')?.innerText),
ownerImg: getValueOrDefault(document.querySelector('div.xjp7ctv > div > div > div > div > .xdt5ytf image')?.href?.baseVal),
};
});
await itemPage.close();
itemPage = null;
} catch(error){
if(itemPage != null){
await logPageContent(itemPage);
await itemPage.close();
itemPage = null;
}
errorMessage(`Error with getting item data at ${newPost}`, error);
}
//check for listing deleted and collection error
if(postObj != null){
//manage description
if(postObj?.description != null){
if(postObj?.description?.length > 700){
postObj.description = (postObj?.description)?.substring(0, 700) + '...';
}
}
//Handle Discord messaging
if(workerData.messageType != 2){//if its not manual messaging
try{
mainChannel.send({ content: "$" + price.toLocaleString() + " - " + postObj?.title, embeds: [new EmbedBuilder()
.setColor(0x0099FF)
.setTitle("$" + price.toLocaleString() + " - " + postObj?.title)
.setURL(newPost)
.setAuthor({ name: workerData.name })
.setDescription(postObj?.description || ' ')
.addFields({ name: postObj?.date || ' ', value: postObj?.shipping || ' '})
.setImage(postObj?.imgs?.length > 0 ? postObj?.imgs[0] : null)
.setTimestamp(new Date())
]});
}catch(error){
await logChannel.send("Post Oject - " + JSON.stringify(postObj));
errorMessage('Error with item notification', error);
}
}else{
let notification;
try{
notification = await mainChannel.send({ content: "$" + price.toLocaleString() + " - " + postObj?.title, embeds: [new EmbedBuilder()
.setColor(0x0099FF)
.setTitle("$" + price.toLocaleString() + " - " + postObj?.title)
.setURL(newPost)
.setAuthor({ name: workerData.name })
.setDescription(postObj?.description)
.addFields({ name: postObj?.date, value: postObj?.shipping })
.setImage(postObj?.imgs[0])
.setTimestamp(new Date())
], components: [new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId('message-' + newPost)
.setLabel('Message')
.setStyle(ButtonStyle.Primary),
)
]});
}catch(error){
errorMessage('Error with new item notification with message button', error);
}
try {
const filter = i => i.customId.split("-")[0] == 'message';
const collector = await notification.createMessageComponentCollector({ filter, time: 14400000 }); //4 hours, I think
collector.on('collect', async i => {
i.reply("Sending...");
sendMessage(i.customId.split("-")[1]);
collector.stop();
});
collector.on('end', () => {
notification.edit({ components: [] });
});
} catch (error) {
errorMessage('Error collecting new item notification button', error);
}
}
}
//**For SS MAX Testing */
if(workerData.userId == "330527268021731330" || workerData.userId == "456168609639694376" || workerData.messageType == null){
postObj.URL = newPost;
postObj.price = price;
postArr.push(postObj);
}
}else{
console.log("\n\nThe Price is Wrong, price: " + price + " max: " + workerData.maxPrice + "\n\n");
if(price == NaN){
logChannel.send("Price NaN: " + newPost);
}
}
//Update newPost
postNum++;
try {
//check if there is another listing that exists
if(await mainPage.$(`div.x1xfsgkm > :nth-child(1) div > :nth-child(${postNum}) a`) != null){
newPost = await mainPage.evaluate((num) => {
let link = document.querySelector(`div.x1xfsgkm > :nth-child(1) div > :nth-child(${num}) a`).href;
return link.substring(0, link.indexOf("?"));
}, postNum);
price = await mainPage.evaluate((num) => {return document.querySelector(`div.x1xfsgkm > :nth-child(1) div > :nth-child(${num}) a span.x78zum5 span.x193iq5w`).innerText}, postNum);
if(price == 'FREE' || price == 'Free'){
price = 0;