-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
82 lines (71 loc) · 2.64 KB
/
background.js
File metadata and controls
82 lines (71 loc) · 2.64 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
const FILE_EXTENSIONS = [".pdf", ".epub", ".mobi", ".djvu", ".azw3", ".fb2", ".mp3"];
function hasBookExtension(url) {
const lower = url.toLowerCase().split("?")[0].split("#")[0];
return FILE_EXTENSIONS.some(ext => lower.endsWith(ext));
}
function sanitizeFilename(name) {
return name.replace(/[<>:"/\\|?*\x00-\x1f]/g, "_");
}
function extractFilename(url) {
try {
const decoded = decodeURIComponent(url);
const name = decoded.split("/").pop().split("?")[0].split("#")[0];
if (name && hasBookExtension(name)) return sanitizeFilename(name);
// Fallback: use hash from URL + guess extension
const ext = FILE_EXTENSIONS.find(e => decoded.toLowerCase().includes(e)) || ".bin";
return "welib-download" + ext;
} catch { return "welib-download.bin"; }
}
// Serial queue to avoid race conditions on chrome.storage.local
let storageQueue = Promise.resolve();
function withStorage(fn) {
storageQueue = storageQueue.then(fn, fn);
return storageQueue;
}
// Dedup by filename (not full URL) so token/query changes don't cause re-downloads
const pending = new Set();
function doDownload(url) {
if (!hasBookExtension(url)) return;
const filename = extractFilename(url);
if (pending.has(filename)) return;
pending.add(filename);
withStorage(() => new Promise((resolve) => {
chrome.storage.local.get({ enabled: true, downloadedFiles: [] }, ({ enabled, downloadedFiles }) => {
if (!enabled || downloadedFiles.includes(filename)) {
pending.delete(filename);
return resolve();
}
const updated = downloadedFiles.concat(filename);
const trimmed = updated.length > 500 ? updated.slice(updated.length - 500) : updated;
chrome.storage.local.set({ downloadedFiles: trimmed }, () => {
chrome.downloads.download({ url, filename, saveAs: false }, () => {
pending.delete(filename);
if (chrome.runtime.lastError) {
// Rollback: remove filename from history (runs inside the same queue via resolve chaining)
chrome.storage.local.set({
downloadedFiles: downloadedFiles.filter(f => f !== filename)
}, resolve);
} else {
resolve();
}
});
});
});
}));
}
chrome.runtime.onMessage.addListener((msg) => {
if (msg.type === "WELIB_DOWNLOAD" && msg.url) doDownload(msg.url);
});
chrome.webRequest.onBeforeRequest.addListener(
(details) => doDownload(details.url),
{
urls: [
"*://*.welib-premium.org/*",
"*://*.welib-public.org/*",
"*://welib-premium.org/*",
"*://welib-public.org/*",
"*://welib.org/audiobooks/*",
"*://*.welib.org/audiobooks/*"
]
}
);