-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
643 lines (552 loc) · 23.6 KB
/
content.js
File metadata and controls
643 lines (552 loc) · 23.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
// Prevent multiple initializations
if (!window.redditCopycatInitialized) {
window.redditCopycatInitialized = true;
console.log('[Reddit Copycat] Content script loaded');
// Global lock for operations
let isOperationInProgress = false;
// Cache for API responses
const apiCache = {
userSession: null,
subredditDetails: new Map(),
lastJoinTimestamp: 0
};
const RATE_LIMIT = {
MIN_DELAY: 2000, // Minimum 2 seconds between requests
BATCH_SIZE: 25, // Process 25 subreddits at a time
BATCH_DELAY: 60000, // 1 minute delay between batches
MAX_RETRIES: 3, // Maximum number of retries per subreddit
BACKOFF_BASE: 5000 // Base delay for exponential backoff
};
async function getAllUserSubreddits() {
try {
console.log('[Reddit Copycat] Starting to fetch subreddits');
let subreddits = [];
let after = null;
let retryCount = 0;
const maxRetries = 3;
let pageCount = 0;
do {
try {
const url = after
? `https://www.reddit.com/subreddits/mine/subscriber.json?limit=100&after=${after}&raw_json=1`
: 'https://www.reddit.com/subreddits/mine/subscriber.json?limit=100&raw_json=1';
console.log(`[Reddit Copycat] Fetching page ${++pageCount}, after: ${after || 'start'}`);
const response = await fetch(url, {
credentials: 'include',
headers: {
'Accept': 'application/json'
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (!data.data) {
throw new Error('Invalid response format');
}
const newSubs = data.data.children.map(child => child.data.display_name);
subreddits = subreddits.concat(newSubs);
after = data.data.after;
console.log(`[Reddit Copycat] Found ${newSubs.length} subreddits on page ${pageCount}. Total: ${subreddits.length}`);
retryCount = 0;
} catch (error) {
console.error(`[Reddit Copycat] Error in pagination:`, error);
retryCount++;
if (retryCount >= maxRetries) {
throw new Error('Max retries reached while fetching subreddits');
}
console.log(`[Reddit Copycat] Retry ${retryCount}/${maxRetries}`);
await new Promise(resolve => setTimeout(resolve, 1000));
continue;
}
await new Promise(resolve => setTimeout(resolve, 300));
} while (after);
console.log(`[Reddit Copycat] Successfully fetched all subreddits. Total: ${subreddits.length}`);
return subreddits;
} catch (error) {
console.error('[Reddit Copycat] Error fetching subreddits:', error);
return null;
}
}
async function getRedditCsrfToken() {
try {
console.log('[Reddit Copycat] Getting CSRF token');
// First try to get token from Reddit's API endpoint
try {
const response = await fetch('https://www.reddit.com/api/me.json', {
credentials: 'include'
});
if (response.ok) {
const token = response.headers.get('x-reddit-session');
if (token) {
console.log('[Reddit Copycat] CSRF token obtained from API');
return token;
}
}
} catch (error) {
console.log('[Reddit Copycat] Failed to get token from API, trying alternative methods');
}
// Try to get token from the page
const response = await fetch('https://www.reddit.com', {
credentials: 'include'
});
const text = await response.text();
// Try different token patterns
const patterns = [
/"csrf_token"\s*:\s*"([^"]+)"/,
/csrf_token=([^;]+)/,
/data-csrf-token="([^"]+)"/,
/&csrf_token=([^&"]+)/
];
for (const pattern of patterns) {
const match = text.match(pattern);
if (match) {
console.log('[Reddit Copycat] CSRF token obtained from pattern:', pattern);
return match[1];
}
}
// Try to get token from cookies
const cookies = document.cookie.split(';');
for (const cookie of cookies) {
const [name, value] = cookie.trim().split('=');
if (name === 'csrf_token') {
console.log('[Reddit Copycat] CSRF token obtained from cookies');
return value;
}
}
throw new Error('CSRF token not found in any location');
} catch (error) {
console.error('[Reddit Copycat] Error getting CSRF token:', error);
return null;
}
}
async function getUserSession() {
if (apiCache.userSession) {
return apiCache.userSession;
}
const meResponse = await fetch('https://www.reddit.com/api/me.json', {
credentials: 'include',
headers: {
'Accept': 'application/json'
}
});
if (!meResponse.ok) {
throw new Error('Failed to get user session');
}
const meData = await meResponse.json();
const cookies = document.cookie.split(';');
let csrfToken = '';
for (const cookie of cookies) {
const [name, value] = cookie.trim().split('=');
if (name === 'csrf_token') {
csrfToken = value;
break;
}
}
if (!csrfToken) {
throw new Error('CSRF token not found in cookies');
}
apiCache.userSession = {
modhash: meData.data?.modhash,
csrfToken
};
return apiCache.userSession;
}
async function getSubredditDetails(subredditName) {
if (apiCache.subredditDetails.has(subredditName)) {
return apiCache.subredditDetails.get(subredditName);
}
const aboutResponse = await fetch(`https://www.reddit.com/r/${subredditName}/about.json`, {
credentials: 'include',
headers: {
'Accept': 'application/json'
}
});
if (!aboutResponse.ok) {
throw new Error(`Subreddit not found: ${aboutResponse.status}`);
}
const aboutData = await aboutResponse.json();
const details = { fullName: aboutData.data.name };
apiCache.subredditDetails.set(subredditName, details);
return details;
}
async function joinSubreddit(subredditName) {
try {
console.log(`[Reddit Copycat] Attempting to join r/${subredditName}`);
// Implement rate limiting
const now = Date.now();
const timeSinceLastJoin = now - apiCache.lastJoinTimestamp;
if (timeSinceLastJoin < RATE_LIMIT.MIN_DELAY) {
await new Promise(resolve => setTimeout(resolve, RATE_LIMIT.MIN_DELAY - timeSinceLastJoin));
}
// Get cached or fetch new session data
const session = await getUserSession();
// Get cached or fetch new subreddit details
const subredditDetails = await getSubredditDetails(subredditName);
// Prepare the request
const formData = new URLSearchParams();
formData.append('action', 'sub');
formData.append('sr', subredditDetails.fullName);
formData.append('skip_initial_defaults', 'true');
if (session.modhash) formData.append('uh', session.modhash);
// Try joining with exponential backoff
let retryCount = 0;
while (retryCount < RATE_LIMIT.MAX_RETRIES) {
try {
const response = await fetch('https://www.reddit.com/api/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'x-csrf-token': session.csrfToken,
'x-requested-with': 'XMLHttpRequest',
'User-Agent': 'web',
'Origin': 'https://www.reddit.com',
'Referer': `https://www.reddit.com/r/${subredditName}`
},
body: formData.toString(),
credentials: 'include'
});
if (response.ok) {
console.log(`[Reddit Copycat] Successfully joined r/${subredditName}`);
apiCache.lastJoinTimestamp = Date.now();
return true;
}
const responseText = await response.text();
if (response.status === 429 || responseText.includes('rate')) {
const backoffDelay = RATE_LIMIT.BACKOFF_BASE * Math.pow(2, retryCount);
console.log(`[Reddit Copycat] Rate limited, waiting ${backoffDelay}ms before retry`);
await new Promise(resolve => setTimeout(resolve, backoffDelay));
retryCount++;
continue;
}
throw new Error(`Join request failed: ${response.status}`);
} catch (error) {
console.error(`[Reddit Copycat] Error in join attempt ${retryCount + 1}:`, error);
retryCount++;
if (retryCount >= RATE_LIMIT.MAX_RETRIES) {
throw error;
}
}
}
throw new Error('Max retries reached');
} catch (error) {
console.error(`[Reddit Copycat] Error joining r/${subredditName}:`, error);
return false;
}
}
async function leaveSubreddit(subredditName) {
try {
console.log(`[Reddit Copycat] Attempting to leave r/${subredditName}`);
// Implement rate limiting
const now = Date.now();
const timeSinceLastJoin = now - apiCache.lastJoinTimestamp;
if (timeSinceLastJoin < RATE_LIMIT.MIN_DELAY) {
await new Promise(resolve => setTimeout(resolve, RATE_LIMIT.MIN_DELAY - timeSinceLastJoin));
}
// Get cached or fetch new session data
const session = await getUserSession();
// Get cached or fetch new subreddit details
const subredditDetails = await getSubredditDetails(subredditName);
// Prepare the request
const formData = new URLSearchParams();
formData.append('action', 'unsub');
formData.append('sr', subredditDetails.fullName);
if (session.modhash) formData.append('uh', session.modhash);
// Try leaving with exponential backoff
let retryCount = 0;
while (retryCount < RATE_LIMIT.MAX_RETRIES) {
try {
const response = await fetch('https://www.reddit.com/api/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'x-csrf-token': session.csrfToken,
'x-requested-with': 'XMLHttpRequest',
'User-Agent': 'web',
'Origin': 'https://www.reddit.com',
'Referer': `https://www.reddit.com/r/${subredditName}`
},
body: formData.toString(),
credentials: 'include'
});
if (response.ok) {
console.log(`[Reddit Copycat] Successfully left r/${subredditName}`);
apiCache.lastJoinTimestamp = Date.now();
return true;
}
const responseText = await response.text();
if (response.status === 429 || responseText.includes('rate')) {
const backoffDelay = RATE_LIMIT.BACKOFF_BASE * Math.pow(2, retryCount);
console.log(`[Reddit Copycat] Rate limited, waiting ${backoffDelay}ms before retry`);
await new Promise(resolve => setTimeout(resolve, backoffDelay));
retryCount++;
continue;
}
throw new Error(`Leave request failed: ${response.status}`);
} catch (error) {
console.error(`[Reddit Copycat] Error in leave attempt ${retryCount + 1}:`, error);
retryCount++;
if (retryCount >= RATE_LIMIT.MAX_RETRIES) {
throw error;
}
}
}
throw new Error('Max retries reached');
} catch (error) {
console.error(`[Reddit Copycat] Error leaving r/${subredditName}:`, error);
return false;
}
}
// Message handler function
async function handleMessage(request, sender, sendResponse) {
console.log('[Reddit Copycat] Received message:', request);
// Handle ping message
if (request.action === 'ping') {
console.log('[Reddit Copycat] Received ping, responding...');
sendResponse({ success: true });
return;
}
if (isOperationInProgress) {
console.log('[Reddit Copycat] Operation already in progress, ignoring request');
sendResponse({ success: false, error: 'Operation in progress' });
return;
}
try {
isOperationInProgress = true;
if (request.action === 'saveSubreddits') {
console.log('[Reddit Copycat] Processing saveSubreddits request');
const subreddits = await getAllUserSubreddits();
if (subreddits && subreddits.length > 0) {
console.log(`[Reddit Copycat] Saving ${subreddits.length} subreddits`);
await new Promise((resolve) => {
chrome.storage.local.set({ savedSubreddits: subreddits }, () => {
sendResponse({ success: true, count: subreddits.length, subreddits: subreddits });
resolve();
});
});
} else {
console.log('[Reddit Copycat] No subreddits found or error occurred');
sendResponse({ success: false });
}
}
else if (request.action === 'joinSubreddits') {
console.log('[Reddit Copycat] Processing joinSubreddits request');
const result = await new Promise(resolve => {
chrome.storage.local.get(['savedSubreddits'], async (result) => {
const subsToProcess = request.subreddits || result.savedSubreddits;
if (!subsToProcess || subsToProcess.length === 0) {
console.log('[Reddit Copycat] No subreddits to join');
resolve({ success: false, error: 'No subreddits to join' });
return;
}
console.log(`[Reddit Copycat] Found ${subsToProcess.length} subreddits to process`);
const currentSubs = await getAllUserSubreddits();
if (!currentSubs) {
console.log('[Reddit Copycat] Failed to fetch current subreddits');
resolve({ success: false, error: 'Failed to fetch current subreddits' });
return;
}
const subsToJoin = subsToProcess.filter(sub => !currentSubs.includes(sub));
console.log(`[Reddit Copycat] ${subsToJoin.length} subreddits to join`);
if (subsToJoin.length === 0) {
console.log('[Reddit Copycat] All subreddits are already joined');
resolve({ success: false, error: 'You are already a member of all selected subreddits!' });
return;
}
let successCount = 0;
let totalAttempted = 0;
let failedSubs = [];
// Process in batches
for (let i = 0; i < subsToJoin.length; i += RATE_LIMIT.BATCH_SIZE) {
const batch = subsToJoin.slice(i, i + RATE_LIMIT.BATCH_SIZE);
// Update progress for batch start
chrome.runtime.sendMessage({
action: 'joinProgress',
current: totalAttempted,
total: subsToJoin.length,
status: `Starting batch ${Math.floor(i/RATE_LIMIT.BATCH_SIZE) + 1}...`
});
// Process each subreddit in the batch
for (const subreddit of batch) {
totalAttempted++;
if (currentSubs.includes(subreddit)) {
console.log(`[Reddit Copycat] Skipping r/${subreddit} (already joined)`);
chrome.runtime.sendMessage({
action: 'joinProgress',
current: totalAttempted,
total: subsToJoin.length,
status: `Already joined r/${subreddit} (${totalAttempted}/${subsToJoin.length})`
});
continue;
}
const success = await joinSubreddit(subreddit);
if (success) {
successCount++;
console.log(`[Reddit Copycat] Successfully joined r/${subreddit}`);
} else {
failedSubs.push(subreddit);
console.log(`[Reddit Copycat] Failed to join r/${subreddit}`);
}
chrome.runtime.sendMessage({
action: 'joinProgress',
current: totalAttempted,
total: subsToJoin.length,
status: success
? `Joined r/${subreddit} (${totalAttempted}/${subsToJoin.length})`
: `Failed to join r/${subreddit} (${totalAttempted}/${subsToJoin.length})`
});
}
// Add delay between batches if not the last batch
if (i + RATE_LIMIT.BATCH_SIZE < subsToJoin.length) {
console.log('[Reddit Copycat] Batch complete, waiting before next batch...');
chrome.runtime.sendMessage({
action: 'joinProgress',
current: totalAttempted,
total: subsToJoin.length,
status: `Batch complete. Waiting ${RATE_LIMIT.BATCH_DELAY/1000} seconds before next batch...`
});
await new Promise(resolve => setTimeout(resolve, RATE_LIMIT.BATCH_DELAY));
}
}
console.log(`[Reddit Copycat] Joining complete. Success: ${successCount}, Failed: ${failedSubs.length}`);
if (failedSubs.length > 0) {
console.log('[Reddit Copycat] Failed subreddits:', failedSubs);
}
resolve({
success: successCount > 0,
totalProcessed: totalAttempted,
successCount: successCount,
failedSubs: failedSubs
});
});
});
sendResponse(result);
return true;
}
else if (request.action === 'getCurrentSubreddits') {
console.log('[Reddit Copycat] Getting current subreddits');
const subreddits = await getAllUserSubreddits();
if (subreddits) {
console.log(`[Reddit Copycat] Found ${subreddits.length} current subreddits`);
sendResponse({ success: true, subreddits: subreddits });
} else {
console.log('[Reddit Copycat] Failed to get current subreddits');
sendResponse({ success: false, error: 'Failed to fetch current subreddits', subreddits: [] });
}
return true;
}
else if (request.action === 'leaveSubreddits') {
console.log('[Reddit Copycat] Processing leaveSubreddits request');
const result = await new Promise(resolve => {
chrome.storage.local.get(['savedSubreddits'], async (result) => {
const subsToProcess = request.subreddits || result.savedSubreddits;
if (!subsToProcess || subsToProcess.length === 0) {
console.log('[Reddit Copycat] No subreddits to leave');
resolve({ success: false, error: 'No subreddits to leave' });
return;
}
console.log(`[Reddit Copycat] Found ${subsToProcess.length} subreddits to process`);
const currentSubs = await getAllUserSubreddits();
if (!currentSubs) {
console.log('[Reddit Copycat] Failed to fetch current subreddits');
resolve({ success: false, error: 'Failed to fetch current subreddits' });
return;
}
const subsToLeave = subsToProcess.filter(sub => currentSubs.includes(sub));
console.log(`[Reddit Copycat] ${subsToLeave.length} subreddits to leave`);
if (subsToLeave.length === 0) {
console.log('[Reddit Copycat] Not a member of any selected subreddits');
resolve({ success: false, error: 'You are not a member of any selected subreddits!' });
return;
}
let successCount = 0;
let totalAttempted = 0;
let failedSubs = [];
// Process in batches
for (let i = 0; i < subsToLeave.length; i += RATE_LIMIT.BATCH_SIZE) {
const batch = subsToLeave.slice(i, i + RATE_LIMIT.BATCH_SIZE);
// Update progress for batch start
chrome.runtime.sendMessage({
action: 'leaveProgress',
current: totalAttempted,
total: subsToLeave.length,
status: `Starting batch ${Math.floor(i/RATE_LIMIT.BATCH_SIZE) + 1}...`
});
// Process each subreddit in the batch
for (const subreddit of batch) {
totalAttempted++;
if (!currentSubs.includes(subreddit)) {
console.log(`[Reddit Copycat] Skipping r/${subreddit} (not joined)`);
chrome.runtime.sendMessage({
action: 'leaveProgress',
current: totalAttempted,
total: subsToLeave.length,
status: `Not a member of r/${subreddit} (${totalAttempted}/${subsToLeave.length})`
});
continue;
}
const success = await leaveSubreddit(subreddit);
if (success) {
successCount++;
console.log(`[Reddit Copycat] Successfully left r/${subreddit}`);
} else {
failedSubs.push(subreddit);
console.log(`[Reddit Copycat] Failed to leave r/${subreddit}`);
}
chrome.runtime.sendMessage({
action: 'leaveProgress',
current: totalAttempted,
total: subsToLeave.length,
status: success
? `Left r/${subreddit} (${totalAttempted}/${subsToLeave.length})`
: `Failed to leave r/${subreddit} (${totalAttempted}/${subsToLeave.length})`
});
}
// Add delay between batches if not the last batch
if (i + RATE_LIMIT.BATCH_SIZE < subsToLeave.length) {
console.log('[Reddit Copycat] Batch complete, waiting before next batch...');
chrome.runtime.sendMessage({
action: 'leaveProgress',
current: totalAttempted,
total: subsToLeave.length,
status: `Batch complete. Waiting ${RATE_LIMIT.BATCH_DELAY/1000} seconds before next batch...`
});
await new Promise(resolve => setTimeout(resolve, RATE_LIMIT.BATCH_DELAY));
}
}
console.log(`[Reddit Copycat] Leaving complete. Success: ${successCount}, Failed: ${failedSubs.length}`);
if (failedSubs.length > 0) {
console.log('[Reddit Copycat] Failed subreddits:', failedSubs);
}
resolve({
success: successCount > 0,
totalProcessed: totalAttempted,
successCount: successCount,
failedSubs: failedSubs
});
});
});
sendResponse(result);
return true;
}
else {
console.log('[Reddit Copycat] Unknown message action:', request.action);
sendResponse({ success: false, error: 'Unknown action' });
}
} catch (error) {
console.error('[Reddit Copycat] Error handling message:', error);
sendResponse({ success: false, error: error.message });
} finally {
isOperationInProgress = false;
}
}
// Set up message listener only once
console.log('[Reddit Copycat] Setting up message listener');
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
handleMessage(request, sender, sendResponse);
return true; // Keep the message channel open
});
console.log('[Reddit Copycat] Content script initialized successfully');
} else {
console.log('[Reddit Copycat] Content script already initialized, skipping...');
}