This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
background.js
229 lines (203 loc) · 5.97 KB
/
background.js
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
var tabMap = new Map();
async function shouldResize(attachment, checkSize = true) {
if (!attachment.name.toLowerCase().match(/\.jpe?g$/)) {
return false;
}
if (!checkSize) {
return true;
}
let { fileSizeMinimum } = await browser.storage.local.get({ fileSizeMinimum: 100 });
let file = await browser.compose.getAttachmentFile(attachment.id);
return file.size >= fileSizeMinimum * 1024;
}
browser.shrunked.migrateSettings().then(prefsToStore => {
if (prefsToStore) {
browser.storage.local.set(prefsToStore);
}
});
browser.composeScripts.register({
js: [
{
file: "compose_script.js",
},
],
});
browser.runtime.onMessage.addListener(async (message, sender, callback) => {
// Image added to body of message. Return a promise to the sender.
if (message.type == "resizeFile") {
return beginResize(sender.tab, message.file);
}
// Options window requesting a file.
if (message.type == "fetchFile") {
return Promise.resolve(tabMap.get(message.tabId)[message.index].file);
}
// Options window starting resize.
if (message.type == "doResize") {
doResize(message.tabId, message.maxWidth, message.maxHeight, message.quality);
}
return undefined;
});
// Attachment added to message. Just update the attachment.
browser.compose.onAttachmentAdded.addListener(async (tab, attachment) => {
let { resizeAttachmentsOnSend } = await browser.storage.local.get({
resizeAttachmentsOnSend: false,
});
if (resizeAttachmentsOnSend) {
return;
}
if (!(await shouldResize(attachment))) {
return;
}
let file = await browser.compose.getAttachmentFile(attachment.id);
let destFile = await beginResize(tab, file);
if (destFile === null) {
return;
}
await browser.compose.updateAttachment(tab.id, attachment.id, {
file: destFile,
});
});
// Content context menu item.
browser.shrunked.onComposeContextClicked.addListener(async (tab, file) => {
tabMap.delete(tab.id);
return new Promise((resolve, reject) => {
beginResize(tab, file, false).then(resolve, reject);
showOptionsDialog(tab);
});
});
// Attachment menu item.
browser.shrunked.onAttachmentContextClicked.addListener(async (tab, indicies) => {
if (!indicies.length) {
return;
}
tabMap.delete(tab.id);
let attachments = await browser.compose.listAttachments(tab.id);
for (let i of indicies) {
let a = attachments[i];
if (await shouldResize(a, false)) {
let file = await browser.compose.getAttachmentFile(a.id);
beginResize(tab, file, false).then(destFile => {
if (destFile === null) {
return;
}
browser.compose.updateAttachment(tab.id, a.id, { file: destFile });
});
}
}
showOptionsDialog(tab);
});
// Message sending.
browser.compose.onBeforeSend.addListener(async (tab, details) => {
let result = {};
let { resizeAttachmentsOnSend } = await browser.storage.local.get({
resizeAttachmentsOnSend: false,
});
if (!resizeAttachmentsOnSend) {
return result;
}
tabMap.delete(tab.id);
let promises = [];
let attachments = await browser.compose.listAttachments(tab.id);
for (let a of attachments) {
if (await shouldResize(a)) {
let file = await browser.compose.getAttachmentFile(a.id);
let promise = beginResize(tab, file, false).then(async destFile => {
if (destFile === null) {
return;
}
await browser.compose.updateAttachment(tab.id, a.id, { file: destFile });
});
promises.push(promise);
}
}
if (!promises.length) {
return result;
}
await showOptionsDialog(tab);
await Promise.all(promises).catch(() => {
result.cancel = true;
});
return result;
});
// Get a promise that resolves when resizing is complete.
function beginResize(tab, file, notification = true) {
return new Promise((resolve, reject) => {
if (!tabMap.has(tab.id)) {
tabMap.set(tab.id, []);
}
let sourceFiles = tabMap.get(tab.id);
sourceFiles.push({ promise: { resolve, reject }, file });
if (notification) {
browser.shrunked.showNotification(tab, sourceFiles.length);
} else {
browser.shrunked.showNotification(tab, 0);
}
});
}
// Notification response.
browser.shrunked.onNotificationAccepted.addListener(tab => showOptionsDialog(tab));
browser.shrunked.onNotificationCancelled.addListener(tab => cancelResize(tab.id));
async function showOptionsDialog(tab) {
let sourceFiles = tabMap.get(tab.id);
let optionsWindow = await browser.windows.create({
url: `content/options.xhtml?tabId=${tab.id}&count=${sourceFiles.length}`,
type: "popup",
width: 550,
height: 425,
});
let listener = windowId => {
if (windowId == optionsWindow.id) {
browser.windows.onRemoved.removeListener(listener);
cancelResize(tab.id);
}
};
browser.windows.onRemoved.addListener(listener);
}
// Actual resize operation.
async function doResize(tabId, maxWidth, maxHeight, quality) {
// Remove from tabMap immediately, then cancelResize will have nothing to do.
let sourceFiles = tabMap.get(tabId);
tabMap.delete(tabId);
// User opted not to resize.
if (maxWidth < 0 || maxHeight < 0) {
for (let source of sourceFiles) {
source.promise.resolve(null);
}
return;
}
let options = await browser.storage.local.get({
"options.exif": true,
"options.orientation": true,
"options.gps": true,
"options.resample": true,
});
options = {
exif: options.exif,
orientation: options.orientation,
gps: options.gps,
resample: options.resample,
};
for (let source of sourceFiles) {
let destFile = await browser.shrunked.resizeFile(
source.file,
maxWidth,
maxHeight,
quality,
options
);
source.promise.resolve(destFile);
}
}
function cancelResize(tabId) {
if (!tabMap.has(tabId)) {
return;
}
for (let source of tabMap.get(tabId)) {
source.promise.reject("Resizing cancelled.");
}
tabMap.delete(tabId);
}
// Clean up.
browser.tabs.onRemoved.addListener(tabId => {
tabMap.delete(tabId);
});