-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
978 lines (855 loc) · 27.9 KB
/
Copy pathbackground.js
File metadata and controls
978 lines (855 loc) · 27.9 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
// FitGirl FuckingFast Batch Downloader - background service worker (MV3)
// Default maximum number of FuckingFast links we will actively process at once
const DEFAULT_MAX_CONCURRENT_DOWNLOADS = 5;
const DEFAULT_SETTINGS = {
concurrency: DEFAULT_MAX_CONCURRENT_DOWNLOADS
};
function swapProtocol(url) {
if (url.startsWith("https://")) return url.replace("https://", "http://");
if (url.startsWith("http://")) return url.replace("http://", "https://");
return url;
}
// In-memory sessions keyed by Chrome tab ID
// sessions[tabId] = { tabId, sourceUrl, hasStarted, items: [{ url, label, state, downloadId|null }] }
let sessions = {};
let sessionsLoaded = false;
// Map downloadId -> { tabId, index } into sessions[tabId].items
let downloadIdToKey = new Map();
let downloadMapLoaded = false;
const ACTIVE_ITEM_STATES = new Set(["queued", "starting", "downloading", "paused"]);
function clampConcurrency(value) {
const n = Number(value);
if (!Number.isFinite(n) || n <= 0) return DEFAULT_MAX_CONCURRENT_DOWNLOADS;
// Hard limit between 1 and 20 to avoid abuse
return Math.max(1, Math.min(20, Math.round(n)));
}
async function getCurrentSettings() {
const stored = await chrome.storage.sync.get(DEFAULT_SETTINGS);
return {
concurrency: clampConcurrency(stored.concurrency)
};
}
async function loadDownloadMap() {
if (downloadMapLoaded) return;
const stored = await chrome.storage.local.get("ffDownloadMap");
if (stored.ffDownloadMap && typeof stored.ffDownloadMap === "object") {
downloadIdToKey = new Map(
Object.entries(stored.ffDownloadMap).map(([id, key]) => [
Number(id),
key
])
);
}
downloadMapLoaded = true;
}
async function saveDownloadMap() {
const obj = {};
for (const [id, key] of downloadIdToKey.entries()) {
obj[id] = key;
}
await chrome.storage.local.set({ ffDownloadMap: obj });
}
async function loadSessions() {
if (sessionsLoaded) return;
const stored = await chrome.storage.local.get("ffSessions");
if (stored.ffSessions && typeof stored.ffSessions === "object") {
sessions = stored.ffSessions;
}
sessionsLoaded = true;
}
async function saveSessions() {
await chrome.storage.local.set({ ffSessions: sessions });
}
function normalizeLink(link) {
if (!link || typeof link.url !== "string" || !link.url.length) return null;
return {
url: link.url,
label: typeof link.label === "string" && link.label.length ? link.label : link.url
};
}
function toQueuedItem(link) {
return {
url: link.url,
label: link.label || link.url,
state: "queued",
downloadId: null
};
}
function normalizeSessionItem(item) {
const link = normalizeLink(item);
if (!link) return null;
return {
url: link.url,
label: link.label,
state: typeof item?.state === "string" ? item.state : "queued",
downloadId: Number.isInteger(item?.downloadId) ? item.downloadId : null
};
}
function normalizeSessionShape(session) {
if (!session || typeof session !== "object") {
return {
tabId: null,
sourceUrl: "",
title: "",
hasStarted: false,
paused: false,
generation: 0,
allItems: [],
items: []
};
}
const normalizedItems = Array.isArray(session.items)
? session.items.map(normalizeSessionItem).filter(Boolean)
: [];
const rawAllItems = Array.isArray(session.allItems) ? session.allItems : [];
const normalizedAllItems = rawAllItems.map(normalizeLink).filter(Boolean);
const allItems =
normalizedAllItems.length > 0
? normalizedAllItems
: normalizedItems.map((item) => ({ url: item.url, label: item.label }));
return {
...session,
sourceUrl: typeof session.sourceUrl === "string" ? session.sourceUrl : "",
title: typeof session.title === "string" ? session.title : "",
hasStarted: session.hasStarted === true,
paused: session.paused === true,
generation: Number.isInteger(session.generation) ? session.generation : 0,
allItems,
items: normalizedItems
};
}
function getSessionGeneration(session) {
return Number.isInteger(session?.generation) ? session.generation : 0;
}
function hasActiveItems(session) {
if (!session || session.hasStarted !== true || !Array.isArray(session.items)) {
return false;
}
return session.items.some((item) => ACTIVE_ITEM_STATES.has(item.state));
}
function normalizeSessionUrl(url) {
if (typeof url !== "string" || !url.length) return "";
try {
const parsed = new URL(url);
parsed.hash = "";
return parsed.toString();
} catch (e) {
return url.split("#")[0];
}
}
function shouldReuseSessionForTab(session, tab) {
if (!session) return false;
const activeRun = hasActiveItems(session);
const samePage =
normalizeSessionUrl(session.sourceUrl) === normalizeSessionUrl(tab?.url);
if (activeRun) {
// While work is actively queued/running, preserve tab-scoped batch state
// even if the user navigates within the tab.
return true;
}
// Pre-start state can be reused on the same page.
if (!session.hasStarted && samePage) {
return true;
}
// Terminal or cancelled runs should not linger across page changes.
return false;
}
function isSessionGenerationCurrent(tabId, generation) {
const session = sessions[tabId];
if (!session) return false;
return getSessionGeneration(session) === generation;
}
function getSessionItemForRun(tabId, index, generation) {
if (!isSessionGenerationCurrent(tabId, generation)) return null;
const session = sessions[tabId];
if (!session || !Array.isArray(session.items) || !session.items[index]) {
return null;
}
return session.items[index];
}
function createSessionFromExtractedLinks(tab, title, links, generation) {
const allItems = Array.isArray(links) ? links.map(normalizeLink).filter(Boolean) : [];
return normalizeSessionShape({
tabId: tab.id,
sourceUrl: tab.url,
title: title || "",
hasStarted: false,
paused: false,
generation,
allItems,
items: allItems.map(toQueuedItem)
});
}
function searchDownloadById(downloadId) {
return new Promise((resolve) => {
chrome.downloads.search({ id: downloadId }, (results) => {
if (chrome.runtime.lastError) {
resolve(null);
return;
}
resolve(Array.isArray(results) && results.length ? results[0] : null);
});
});
}
async function reconcileSessionForTab(tabId) {
await loadSessions();
await loadDownloadMap();
const existing = sessions[tabId];
if (!existing) return;
const session = normalizeSessionShape(existing);
let sessionsChanged = false;
let mapChanged = false;
for (let i = 0; i < session.items.length; i++) {
const item = session.items[i];
if (item.downloadId == null) {
if (item.state === "starting") {
continue;
}
if (item.state === "downloading" || item.state === "paused") {
item.state = "error";
sessionsChanged = true;
}
continue;
}
const downloadId = item.downloadId;
const download = await searchDownloadById(downloadId);
if (!download) {
downloadIdToKey.delete(downloadId);
mapChanged = true;
item.downloadId = null;
if (ACTIVE_ITEM_STATES.has(item.state)) {
item.state = "cancelled";
}
sessionsChanged = true;
continue;
}
if (download.state === "complete") {
downloadIdToKey.delete(downloadId);
mapChanged = true;
item.downloadId = null;
item.state = "completed";
sessionsChanged = true;
continue;
}
if (download.state === "interrupted") {
downloadIdToKey.delete(downloadId);
mapChanged = true;
item.downloadId = null;
item.state = download.error === "USER_CANCELED" ? "cancelled" : "error";
sessionsChanged = true;
continue;
}
const nextState = download.paused ? "paused" : "downloading";
if (item.state !== nextState) {
item.state = nextState;
sessionsChanged = true;
}
const existingKey = downloadIdToKey.get(downloadId);
const generation = getSessionGeneration(session);
if (
!existingKey ||
existingKey.tabId !== tabId ||
existingKey.index !== i ||
existingKey.generation !== generation
) {
downloadIdToKey.set(downloadId, { tabId, index: i, generation });
mapChanged = true;
}
}
if (session.hasStarted && !hasActiveItems(session)) {
session.hasStarted = false;
session.paused = false;
sessionsChanged = true;
}
if (sessionsChanged) {
sessions[tabId] = session;
broadcastSessionUpdate(tabId);
}
if (sessionsChanged || mapChanged) {
await Promise.all([
sessionsChanged ? saveSessions() : Promise.resolve(),
mapChanged ? saveDownloadMap() : Promise.resolve()
]);
}
}
async function rebuildSessionForTab(tab, generation) {
const { title, items } = await extractFuckingFastLinks(tab.id);
const rebuilt = createSessionFromExtractedLinks(tab, title, items, generation);
sessions[tab.id] = rebuilt;
await saveSessions();
return rebuilt;
}
function broadcastSessionUpdate(tabId) {
// In MV3, sendMessage may reject with "Receiving end does not exist"
// when no views (popup, etc.) are open. We explicitly ignore that.
const session = sessions[tabId];
if (!session) return;
try {
chrome.runtime.sendMessage({ type: "session_updated", tabId, session }, () => {
// Accessing lastError clears it and prevents "Uncaught (in promise)" noise
// when there is no receiver (e.g. popup closed).
void chrome.runtime.lastError;
});
} catch (e) {
// Ignore synchronous errors as well (very rare)
}
}
/**
* Extract all FuckingFast links from the current FitGirl page.
* Runs in the context of the page via chrome.scripting.executeScript.
*/
async function extractFuckingFastLinks(tabId) {
const [{ result }] = await chrome.scripting.executeScript({
target: { tabId },
world: "MAIN",
func: () => {
const rawTitle = document.title || "";
const title = rawTitle.replace(/ - FitGirl Repacks.*/i, "").trim();
// Try to scope to the main post content if possible
const containers = [
document.querySelector(".entry-content"),
document.querySelector(".post"),
document.body
].filter(Boolean);
const items = [];
for (const container of containers) {
const anchors = container.querySelectorAll(
'a[href^="https://fuckingfast.co/"], a[href^="http://fuckingfast.co/"]'
);
anchors.forEach((a) => {
if (a.href) {
const label = a.textContent.trim() || a.href;
items.push({ url: a.href, label });
}
});
// If we found some inside a more specific container, no need to fall back further
if (items.length > 0 && container !== document.body) {
break;
}
}
// De-duplicate by URL while preserving first label
const seen = new Set();
const unique = [];
for (const item of items) {
if (seen.has(item.url)) continue;
seen.add(item.url);
unique.push(item);
}
return { title, items: unique };
}
});
if (result && Array.isArray(result.items)) {
return { title: result.title || "", items: result.items };
}
return { title: "", items: [] };
}
/**
* Given a FuckingFast landing URL, fetch its HTML and extract the
* underlying direct /dl/... URL that the page's download() function
* would open. FuckingFast currently serves these from dl.fuckingfast.co.
*/
async function getDirectDownloadUrl(fuckingFastUrl) {
const attempts = [fuckingFastUrl, swapProtocol(fuckingFastUrl)];
let lastError;
for (const url of attempts) {
try {
const res = await fetch(url, { credentials: "include" });
if (!res.ok) {
lastError = new Error(
`Failed to fetch FuckingFast page (${res.status} ${res.statusText})`
);
continue;
}
const html = await res.text();
const match = html.match(
/https?:\/\/(?:dl\.)?fuckingfast\.co\/dl\/[^\s"'<>\\]+/
);
if (!match) {
lastError = new Error("Direct /dl/ URL not found in FuckingFast page HTML");
continue;
}
return match[0];
} catch (err) {
lastError = err;
}
}
throw lastError;
}
/**
* Start a Chrome download for the given URL and resolve with its downloadId.
*/
function startDownload(dlUrl) {
return new Promise((resolve, reject) => {
chrome.downloads.download(
{
url: dlUrl,
saveAs: false
},
(downloadId) => {
if (chrome.runtime.lastError || downloadId === undefined) {
reject(
chrome.runtime.lastError ||
new Error("chrome.downloads.download() failed")
);
return;
}
resolve(downloadId);
}
);
});
}
/**
* Ensure we have a session for the given active FitGirl tab.
*/
async function ensureSessionForTab(tab) {
await loadSessions();
await reconcileSessionForTab(tab.id);
const existing = sessions[tab.id];
let session = existing ? normalizeSessionShape(existing) : null;
if (session) {
sessions[tab.id] = session;
if (shouldReuseSessionForTab(session, tab)) {
return session;
}
}
const nextGeneration = session ? getSessionGeneration(session) + 1 : 0;
return rebuildSessionForTab(tab, nextGeneration);
}
/**
* Pump the queue for a specific FitGirl URL: start new downloads
* up to the concurrency limit. Called when a batch starts and whenever
* a download for that URL completes.
*/
async function pumpQueueForTab(tabId, expectedGeneration) {
await loadSessions();
const existingSession = sessions[tabId];
if (!existingSession) return;
const session = normalizeSessionShape(existingSession);
sessions[tabId] = session;
if (!session || !Array.isArray(session.items) || !session.items.length) return;
const runGeneration =
expectedGeneration == null ? getSessionGeneration(session) : expectedGeneration;
if (!isSessionGenerationCurrent(tabId, runGeneration)) return;
const { concurrency } = await getCurrentSettings();
const maxConcurrent = clampConcurrency(concurrency);
if (session.paused) return;
const activeCount = session.items.filter(
(item) => item.state === "starting" || item.state === "downloading"
).length;
let availableSlots = maxConcurrent - activeCount;
if (availableSlots <= 0) return;
await loadDownloadMap();
for (let i = 0; i < session.items.length && availableSlots > 0; i++) {
const item = getSessionItemForRun(tabId, i, runGeneration);
if (!item) continue;
if (item.state !== "queued") continue;
availableSlots--;
item.state = "starting";
(async (index, generation) => {
try {
const activeItemBeforeFetch = getSessionItemForRun(tabId, index, generation);
if (!activeItemBeforeFetch || activeItemBeforeFetch.state !== "starting") {
return;
}
const dlUrl = await getDirectDownloadUrl(activeItemBeforeFetch.url);
const activeItemBeforeDownload = getSessionItemForRun(tabId, index, generation);
if (!activeItemBeforeDownload || activeItemBeforeDownload.state !== "starting") {
return;
}
let downloadId;
try {
downloadId = await startDownload(dlUrl);
} catch (dlErr) {
const altUrl = swapProtocol(dlUrl);
console.warn("Download failed, retrying with", altUrl, dlErr);
downloadId = await startDownload(altUrl);
}
const activeItemAfterDownload = getSessionItemForRun(tabId, index, generation);
if (!activeItemAfterDownload || activeItemAfterDownload.state !== "starting") {
try {
chrome.downloads.cancel(downloadId);
} catch (e) {
// ignore
}
return;
}
activeItemAfterDownload.downloadId = downloadId;
activeItemAfterDownload.state = "downloading";
downloadIdToKey.set(downloadId, { tabId, index, generation });
await Promise.all([saveSessions(), saveDownloadMap()]);
broadcastSessionUpdate(tabId);
} catch (err) {
const activeItemOnError = getSessionItemForRun(tabId, index, generation);
if (!activeItemOnError) return;
console.warn("Failed to start FuckingFast URL:", activeItemOnError.url, err);
activeItemOnError.state = "error";
activeItemOnError.downloadId = null;
await saveSessions();
broadcastSessionUpdate(tabId);
pumpQueueForTab(tabId, generation);
}
})(i, runGeneration);
}
}
// Track completion of underlying Chrome downloads
chrome.downloads.onChanged.addListener((delta) => {
if (!delta.state || !delta.state.current) return;
const state = delta.state.current;
if (state !== "complete" && state !== "interrupted") return;
(async () => {
await loadDownloadMap();
const key = downloadIdToKey.get(delta.id);
if (!key) return;
await loadSessions();
const existingSession = sessions[key.tabId];
if (!existingSession) {
downloadIdToKey.delete(delta.id);
await saveDownloadMap();
return;
}
const session = normalizeSessionShape(existingSession);
sessions[key.tabId] = session;
if (!isSessionGenerationCurrent(key.tabId, key.generation)) {
downloadIdToKey.delete(delta.id);
await saveDownloadMap();
return;
}
if (!session || !session.items || !session.items[key.index]) {
downloadIdToKey.delete(delta.id);
await saveDownloadMap();
return;
}
const item = session.items[key.index];
downloadIdToKey.delete(delta.id);
item.downloadId = null;
if (state === "complete") {
item.state = "completed";
} else {
item.state = delta.error?.current === "USER_CANCELED" ? "cancelled" : "error";
}
await Promise.all([saveSessions(), saveDownloadMap()]);
broadcastSessionUpdate(key.tabId);
// Try to start the next queued download
pumpQueueForTab(key.tabId, key.generation);
})();
});
// Clean up tab-scoped sessions when a tab is closed
chrome.tabs.onRemoved.addListener((tabId) => {
(async () => {
await loadSessions();
if (sessions[tabId]) {
delete sessions[tabId];
await saveSessions();
}
await loadDownloadMap();
let changed = false;
for (const [downloadId, key] of Array.from(downloadIdToKey.entries())) {
if (key.tabId === tabId) {
downloadIdToKey.delete(downloadId);
changed = true;
}
}
if (changed) {
await saveDownloadMap();
}
})();
});
// Message-based API for popup UIs
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (!message || typeof message.type !== "string") {
return;
}
if (message.type === "scan_current_tab") {
(async () => {
try {
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true
});
if (!tab || !tab.id || !tab.url?.includes("fitgirl-repacks.site")) {
sendResponse({
ok: false,
error: "Please open a FitGirl repack page first."
});
return;
}
const sess = await ensureSessionForTab(tab);
sendResponse({ ok: true, session: sess, tabId: tab.id });
} catch (err) {
console.error("scan_current_tab failed:", err);
sendResponse({ ok: false, error: err.message || String(err) });
}
})();
return true; // keep channel open for async response
}
if (message.type === "start_downloads") {
(async () => {
try {
await loadSessions();
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true
});
if (!tab || !tab.url) {
sendResponse({
ok: false,
error: "No active FitGirl tab found."
});
return;
}
const tabId = tab.id;
const session = normalizeSessionShape(await ensureSessionForTab(tab));
sessions[tabId] = session;
const selectedUrls = new Set(
(message.items || [])
.map((i) => i.url)
.filter((u) => typeof u === "string" && u.length)
);
if (!selectedUrls.size) {
sendResponse({
ok: false,
error: "No files selected for download."
});
return;
}
const candidateLinks =
Array.isArray(session.allItems) && session.allItems.length
? session.allItems
: (session.items || []).map((item) => ({
url: item.url,
label: item.label || item.url
}));
const selectedItems = candidateLinks
.filter((item) => selectedUrls.has(item.url))
.map(toQueuedItem);
if (!selectedItems.length) {
sendResponse({
ok: false,
error: "Selected files were not found in the current page scan."
});
return;
}
// Start a new run generation to invalidate any stale async workers.
session.generation = getSessionGeneration(session) + 1;
const runGeneration = session.generation;
session.items = selectedItems;
session.hasStarted = true;
session.paused = false;
// Clear mapping entries for this tab
await loadDownloadMap();
for (const [downloadId, key] of Array.from(downloadIdToKey.entries())) {
if (key.tabId === tabId) {
downloadIdToKey.delete(downloadId);
}
}
sessions[tabId] = session;
await Promise.all([saveSessions(), saveDownloadMap()]);
broadcastSessionUpdate(tabId);
// Start the queue pump for this tab (will respect concurrency limit)
pumpQueueForTab(tabId, runGeneration);
sendResponse({ ok: true });
} catch (err) {
console.error("start_downloads failed:", err);
sendResponse({ ok: false, error: err.message || String(err) });
}
})();
return true;
}
if (message.type === "get_settings") {
(async () => {
try {
const current = await getCurrentSettings();
sendResponse({ ok: true, settings: current });
} catch (err) {
sendResponse({ ok: false, error: err.message || String(err) });
}
})();
return true;
}
if (message.type === "save_settings") {
const newConcurrency = clampConcurrency(message?.settings?.concurrency);
chrome.storage.sync.set(
{ concurrency: newConcurrency },
() => {
if (chrome.runtime.lastError) {
sendResponse({
ok: false,
error: chrome.runtime.lastError.message
});
} else {
sendResponse({
ok: true,
settings: { concurrency: newConcurrency }
});
}
}
);
return true;
}
if (message.type === "get_session") {
(async () => {
try {
await loadSessions();
sendResponse({ ok: true, sessions });
} catch (err) {
sendResponse({ ok: false, error: err.message || String(err) });
}
})();
return true;
}
if (message.type === "cancel_downloads") {
(async () => {
try {
await loadSessions();
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true
});
if (!tab || !tab.url) {
sendResponse({
ok: false,
error: "No active FitGirl tab found."
});
return;
}
const tabId = tab.id;
const existingSession = sessions[tabId];
if (!existingSession) {
sendResponse({ ok: true });
return;
}
const session = normalizeSessionShape(existingSession);
// Invalidate current run first so in-flight workers stop mutating state.
session.generation = getSessionGeneration(session) + 1;
session.hasStarted = false;
session.paused = false;
// Cancel all active and queued downloads
await loadDownloadMap();
for (let i = 0; i < session.items.length; i++) {
const item = session.items[i];
if (item.downloadId != null) {
try {
chrome.downloads.cancel(item.downloadId);
} catch (e) {
// ignore
}
downloadIdToKey.delete(item.downloadId);
item.downloadId = null;
}
if (ACTIVE_ITEM_STATES.has(item.state)) {
item.state = "cancelled";
}
}
sessions[tabId] = session;
await Promise.all([saveSessions(), saveDownloadMap()]);
// Rebuild immediately for the current page so popup opens into fresh
// detection state without requiring a manual reset action.
if (tab.url?.includes("fitgirl-repacks.site")) {
const refreshed = await rebuildSessionForTab(tab, session.generation);
sessions[tabId] = refreshed;
}
broadcastSessionUpdate(tabId);
sendResponse({ ok: true });
} catch (err) {
sendResponse({ ok: false, error: err.message || String(err) });
}
})();
return true;
}
if (message.type === "pause_downloads" || message.type === "resume_downloads") {
(async () => {
try {
await loadSessions();
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true
});
if (!tab || !tab.url) {
sendResponse({
ok: false,
error: "No active FitGirl tab found."
});
return;
}
const tabId = tab.id;
const existingSession = sessions[tabId];
if (!existingSession) {
sendResponse({ ok: true });
return;
}
const session = normalizeSessionShape(existingSession);
const shouldPause = message.type === "pause_downloads";
session.paused = shouldPause;
for (let i = 0; i < session.items.length; i++) {
const item = session.items[i];
if (item.downloadId == null) continue;
try {
if (shouldPause && item.state === "downloading") {
chrome.downloads.pause(item.downloadId);
item.state = "paused";
} else if (!shouldPause && item.state === "paused") {
chrome.downloads.resume(item.downloadId);
item.state = "downloading";
}
} catch (e) {
// ignore
}
}
sessions[tabId] = session;
await saveSessions();
broadcastSessionUpdate(tabId);
if (!shouldPause) {
pumpQueueForTab(tabId);
}
sendResponse({ ok: true });
} catch (err) {
sendResponse({ ok: false, error: err.message || String(err) });
}
})();
return true;
}
if (message.type === "retry_failed") {
(async () => {
try {
await loadSessions();
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true
});
if (!tab || !tab.url) {
sendResponse({ ok: false, error: "No active FitGirl tab found." });
return;
}
const tabId = tab.id;
const existingSession = sessions[tabId];
if (!existingSession) {
sendResponse({ ok: true });
return;
}
const session = normalizeSessionShape(existingSession);
let requeued = 0;
for (const item of session.items) {
if (item.state === "error") {
item.state = "queued";
item.downloadId = null;
requeued++;
}
}
if (requeued > 0) {
session.generation = getSessionGeneration(session) + 1;
const runGeneration = session.generation;
session.hasStarted = true;
session.paused = false;
sessions[tabId] = session;
await saveSessions();
broadcastSessionUpdate(tabId);
pumpQueueForTab(tabId, runGeneration);
}
sendResponse({ ok: true });
} catch (err) {
sendResponse({ ok: false, error: err.message || String(err) });
}
})();
return true;
}
});