-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
167 lines (141 loc) · 5.25 KB
/
Copy pathbackground.js
File metadata and controls
167 lines (141 loc) · 5.25 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
// background.js
importScripts('unzipit.min.js');
let zipSessions = new Map();
let sessionIdCounter = 0;
const downloadedByExtension = new Set();
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'getZipInfo') {
const sessionId = sessionIdCounter++;
getZipInfo(message.url, sessionId).then(info => {
sendResponse(info);
}).catch(error => {
sendResponse({error: error.message});
});
return true;
} else if (message.type === 'downloadSelected') {
downloadSelected(message.sessionId, message.names, message.url).then(() => {
sendResponse({success: true});
zipSessions.delete(message.sessionId);
}).catch(error => {
sendResponse({error: error.message});
});
return true;
} else if (message.type === 'downloadFull') {
downloadFull(message.url).then(() => {
sendResponse({success: true});
}).catch(error => {
sendResponse({error: error.message});
});
return true;
}
});
chrome.downloads.onCreated.addListener(function(downloadItem) {
if (downloadItem.byExtensionId === chrome.runtime.id || downloadedByExtension.has(downloadItem.url)) {
downloadedByExtension.delete(downloadItem.url);
return;
}
const lowerUrl = downloadItem.url.toLowerCase();
const lowerFilename = downloadItem.filename ? downloadItem.filename.toLowerCase() : '';
const lowerMime = downloadItem.mime ? downloadItem.mime.toLowerCase() : '';
const isZip = lowerUrl.endsWith('.zip') ||
lowerFilename.endsWith('.zip') ||
['application/zip', 'application/x-zip', 'application/x-zip-compressed'].includes(lowerMime);
if (isZip) {
chrome.downloads.cancel(downloadItem.id);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs.length > 0) {
chrome.tabs.sendMessage(tabs[0].id, {type: 'show_zip_modal', zipUrl: downloadItem.url});
}
});
}
});
async function getZipInfo(url, sessionId) {
let fullSize = 0;
const headResponse = await fetch(url, { method: 'HEAD' });
fullSize = parseInt(headResponse.headers.get('Content-Length') || '0', 10);
let supportsRanges = false;
const rangeResponse = await fetch(url, {
method: 'HEAD',
headers: { 'Range': 'bytes=0-0' }
});
if (rangeResponse.status === 206 || rangeResponse.headers.get('Accept-Ranges') === 'bytes') {
supportsRanges = true;
const contentRange = rangeResponse.headers.get('Content-Range');
if (contentRange) {
const match = contentRange.match(/bytes .*\/(\d+)/);
if (match) fullSize = parseInt(match[1], 10);
}
}
const { zip, entries } = await unzipit.unzip(url);
zipSessions.set(sessionId, {zip, entries, fullSize, supportsRanges});
const fileEntries = Object.values(entries)
.filter(entry => !entry.isDirectory)
.map(entry => ({name: entry.name, size: entry.size, compressedSize: entry.compressedSize}));
return {
sessionId,
supportsRanges,
fileEntries,
fullSize
};
}
async function downloadSelected(sessionId, names, url) {
const session = zipSessions.get(sessionId);
if (!session) {
throw new Error('Session not found');
}
const { entries, fullSize, supportsRanges } = session;
let totalDownloaded = names.reduce((sum, name) => sum + (entries[name]?.compressedSize || 0), 0);
if (!supportsRanges) {
totalDownloaded = fullSize;
}
let saved = supportsRanges ? (fullSize - totalDownloaded) : 0;
for (const name of names) {
const entry = entries[name];
if (!entry) continue;
const blob = await entry.blob();
const arrayBuffer = await blob.arrayBuffer();
const base64 = arrayBufferToBase64(arrayBuffer);
const dataUrl = `data:application/octet-stream;base64,${base64}`;
let filename = name.split('/').pop();
if (filename.startsWith('.')) {
filename = '_' + filename.slice(1);
}
await chrome.downloads.download({
url: dataUrl,
filename: filename,
saveAs: false
});
}
await updateStorage(totalDownloaded, saved);
}
function arrayBufferToBase64(buffer) {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
async function downloadFull(url) {
const headResponse = await fetch(url, { method: 'HEAD' });
const fullSize = parseInt(headResponse.headers.get('Content-Length') || '0', 10);
downloadedByExtension.add(url);
await chrome.downloads.download({
url: url,
saveAs: false
});
await updateStorage(fullSize, 0);
}
async function updateStorage(downloaded, saved) {
const now = new Date();
const yearMonth = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`;
const data = await chrome.storage.local.get(['totalDownloaded', 'totalSaved', 'monthlyDownloaded', 'monthlySaved']);
data.totalDownloaded = (data.totalDownloaded || 0) + downloaded;
data.totalSaved = (data.totalSaved || 0) + saved;
data.monthlyDownloaded = data.monthlyDownloaded || {};
data.monthlyDownloaded[yearMonth] = (data.monthlyDownloaded[yearMonth] || 0) + downloaded;
data.monthlySaved = data.monthlySaved || {};
data.monthlySaved[yearMonth] = (data.monthlySaved[yearMonth] || 0) + saved;
await chrome.storage.local.set(data);
}