forked from darktrojan/shrunked
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
456 lines (426 loc) · 14.8 KB
/
background.js
File metadata and controls
456 lines (426 loc) · 14.8 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
import * as utils from "./modules/utils.mjs"
var tabMap = new Map();
let logenabled=true;
let lastContextClickedFile;
// I could slap the WECG for this stupid specification, but yes, menus.onHidden
// does not report which menu items were actually hidden. We have to keep track
// of the last shown one on our own.
let lastShownMenuIds;
async function shouldResize(attachment, checkSize = true) {
if (!attachment.name.toLowerCase().match(/((\.jpe?g)|(\.png)|(\.bmp))$/)) {
return false;
}
if (!checkSize) {
return true;
}
let file = await browser.compose.getAttachmentFile(attachment.id);
return file.size >= fileSizeMinimum * 1024;
}
//get options and defaults from config
let options={}
let defaults={}
let fileSizeMinimum=100;
//check options on start
loadOptions().then((response)=>
{
options=response.options;
defaults=response.defaults;
fileSizeMinimum=response.fileSizeMinimum;
logenabled=response.options.logenabled;
if(logenabled)
console.info("Shrunked Extension: Debug is enabled");
});
// CHECK: Should the migration code not run *before* the options are loaded? The
// execution flow should probably be enforced by awaiting each statement
// and not spinning of the then() action at an arbitrary time later (once
// the background runs into the first await or is done).
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") {
//resizeFile is sent by content script when inline image is inserted. We need to tell the function that this is inline and that the action should trigger auto resize
return beginResize(sender.tab, message.file,true,true,true);
}
// 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") {
return doResize(message.tabId, message.maxWidth, message.maxHeight, message.quality);
}
if (message.type == "getOptions") {
if(options.resizeInReplyForward)
await processAllAttachments(sender.tab,null,true);
return options;
}
// Content script wants to update the context menu for the composer.
if (message.type == "updateComposeContextMenuEntry") {
await browser.menus.update(
"composeContextMenuEntry",
{
title: browser.i18n.getMessage(message.composeContextMenuEntryStatus.label),
enabled: !message.composeContextMenuEntryStatus.disabled,
}
);
await browser.menus.refresh();
lastContextClickedFile = message.file;
return;
}
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);
//tell beginResize that this is not inline and that this event should trigger auto resize
let destFile = await beginResize(tab, file,true,false,true);
if (!destFile) {
return;
}
await browser.compose.updateAttachment(tab.id, attachment.id, {
file: destFile,
name: utils.changeExtensionIfNeeded(destFile.name)
});
});
// Content context menu item.
browser.menus.create({
id: "composeContextMenuEntry",
contexts: ["compose_body"],
title: browser.i18n.getMessage("context.single"),
})
// Attachment context menu item.
browser.menus.create({
id: "attachmentContextMenuEntry",
contexts: ["compose_attachments"],
title: browser.i18n.getMessage("context.single"),
})
browser.menus.onShown.addListener(async (info, tab) => {
lastShownMenuIds = info.menuIds;
if (lastShownMenuIds.includes("attachmentContextMenuEntry")) {
let indicies = [];
for (let i = 0; i < info.attachments.length; i++) {
if (utils.imageIsAccepted(info.attachments[i].name)) {
indicies.push(i);
}
}
// Check if a message should be displayed when accessing context menu on
// unsupported images. If yes then set hidden to true, if not set it to disabled.
let attachmentContextMenuEntryStatus = {
enabled: true,
visible: true,
label: "context.single",
}
// Get the current value. The user might have flipped the setting while the
// compose window was open and we would not have the correct value in the
// pre-loaded options object.
let { "options.contextInfo": contextInfo } = await browser.storage.local.get({
"options.contextInfo": true
});
if (contextInfo) {
attachmentContextMenuEntryStatus.enabled = !!indicies.length;
attachmentContextMenuEntryStatus.visible = true;
} else {
attachmentContextMenuEntryStatus.enabled = true;
attachmentContextMenuEntryStatus.visible = !!indicies.length;
}
if (!indicies.length) {
if (logenabled) {
console.log("Not resizing - no attachments were JPEG/PNG/BMP and large enough");
}
attachmentContextMenuEntryStatus.label = "context.unsupportedFile";
} else if (indicies.length == 1) {
attachmentContextMenuEntryStatus.label = "context.single";
} else {
attachmentContextMenuEntryStatus.label = "context.plural";
}
// Only update the menu, if it is different from the default.
if (
attachmentContextMenuEntryStatus.label != "context.single" ||
!attachmentContextMenuEntryStatus.enabled ||
!attachmentContextMenuEntryStatus.visible
) {
await browser.menus.update(
"attachmentContextMenuEntry",
{
title: browser.i18n.getMessage(attachmentContextMenuEntryStatus.label),
enabled: attachmentContextMenuEntryStatus.enabled,
visible: attachmentContextMenuEntryStatus.visible,
}
);
await browser.menus.refresh();
}
}
// The onShown event for composeContextMenuEntry is handled in the compose
// script, because we need to know which element was clicked on.
});
browser.menus.onHidden.addListener(() => {
// Unhide the menu item, so it will trigger onShown again. Also set it to the
// default.
if (lastShownMenuIds.includes("attachmentContextMenuEntry")) {
browser.menus.update(
"attachmentContextMenuEntry",
{
title: browser.i18n.getMessage("context.single"),
visible: true,
enabled: true,
}
);
}
})
browser.menus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId == "attachmentContextMenuEntry") {
// CHECK: Should this be prevented, if there is already an open resize popup?
if (!info.attachments.length) {
return;
}
// Abort any pending resize promises.
cancelResize(tab.id);
for (let attachment of info.attachments) {
if (await shouldResize(attachment, false)) {
let file = await browser.compose.getAttachmentFile(attachment.id);
beginResize(tab, file, false).then(destFile => {
if (!destFile) {
return;
}
browser.compose.updateAttachment(tab.id, attachment.id, { file: destFile, name: utils.changeExtensionIfNeeded(destFile.name) });
}).catch(error => {
if(logenabled)
console.error('attachmentContextMenuEntry clicked', error);
});
}
}
showOptionsDialog(tab);
}
if (info.menuItemId == "composeContextMenuEntry") {
// CHECK: Should this be prevented, if there is already an open resize popup?
// Abort any pending resize promises, there could be one from the mutation
// observer in the content script registering the image being inserted.
cancelResize(tab.id);
beginResize(tab, lastContextClickedFile, false, true).then(destFile => {
if (!destFile) {
return;
}
browser.tabs.sendMessage(tab.id, {
type: "replaceTargetWithFile",
file: destFile
});
}).catch(error => {
if(logenabled)
console.error('composeContextMenuEntry clicked', error);
});
showOptionsDialog(tab);
}
})
// Message sending.
browser.compose.onBeforeSend.addListener(async (tab, details) => {
await processAllAttachments(tab,details,false);
});
async function processAllAttachments(tab, details,isOnDemand=false) {
let result = {};
if(!isOnDemand)
{
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, isOnDemand,false,true).then(async destFile => {
if (!destFile) {
return;
}
await browser.compose.updateAttachment(tab.id, a.id, { file: destFile, name: utils.changeExtensionIfNeeded(destFile.name) });
}).catch(error => {
if(logenabled)
console.error('onBeforeSend', error);
});
promises.push(promise);
}
}
if (!promises.length) {
return result;
}
if(!isOnDemand && (options.autoResize!="attached" || options.autoResize!="all"))
await showOptionsDialog(tab);
await Promise.all(promises).catch(() => {
result.cancel = true;
});
return result;
}
// Get a promise that resolves when resizing is complete.
// For auto resize we need to know if the image isInline and if this is an event that should trigger auto resize. This method is called multiple times, not only when adding attachemnt
function beginResize(tab, file, notification = true,isInline=false,shouldResizeAuto=false) {
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 });
//check if autoResize is on and if current event should trigger it (shouldResizeAuto) and also if auto resize is turned on for this type of image (inline/attachment)
if(options.autoResize!="off" && shouldResizeAuto && ((options.autoResize=="inline" && isInline) || (options.autoResize=="attached" && !isInline) || options.autoResize=="all"))
{
doResize(tab.id, defaults.maxWidth,defaults.maxHeight,defaults.quality,file);
}
//if not proceed with notification
else if (notification) {
browser.shrunked.showNotification(tab, sourceFiles.length);
} else {
browser.shrunked.showNotification(tab, 0);
}
}).catch(error => {
if(logenabled)
console.error('beginResize', error);
});
}
// 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);
if(sourceFiles === undefined)
return;
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.
// Since doResize usually tries to process all files, we need a special attribute (file) to tell it to process only one file when using auto resize.
// Without this each time a file is auto resized ALL of the attached files / inline images would be resized.
async function doResize(tabId, maxWidth, maxHeight, quality,file="") {
// 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;
}
for (let source of sourceFiles) {
if(file!="" && source.file!=file)
continue;
let destFile = await utils.resizeFile(
source.file,
maxWidth,
maxHeight,
quality,
options
);
source.promise.resolve(destFile);
}
}
async function loadOptions(selectedOption=null)
{
let options;
if(selectedOption!==null)
{
options = await browser.storage.local.get({
"options.logenabled": false,
"options.contextInfo": true
});
options=options[selectedOption];
}
else
{
options = await browser.storage.local.get({
"options.exif": true,
"options.orientation": true,
"options.gps": true,
"options.resample": true,
"options.newalgorithm": true,
"options.logenabled":false,
"options.contextInfo":true,
"options.autoResize":"off",
"options.resizeInReplyForward":false,
"default.maxWidth": 500,
"default.maxHeight": 500,
"default.quality": 75,
"default.saveDefault": true,
"fileSizeMinimum": 100
});
fileSizeMinimum=options['fileSizeMinimum'];
defaults = {
maxWidth: options['default.maxWidth'],
maxHeight: options['default.maxHeight'],
quality: options['default.quality'],
saveDefault: options['default.saveDefault'],
}
options = {
exif: options['options.exif'],
orientation: options['options.orientation'],
gps: options['options.gps'],
resample: options['options.resample'],
newalgorithm: options['options.newalgorithm'],
logenabled: options["options.logenabled"],
contextInfo: options["options.contextInfo"],
autoResize:options["options.autoResize"],
resizeInReplyForward:options["options.resizeInReplyForward"]
};
}
return {"options":options,"defaults":defaults,"fileSizeMinimum":fileSizeMinimum};
}
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);
});
//reload settings each time a new compose window is opened
browser.tabs.onCreated.addListener(tab => {
if(tab.type=="messageCompose")
{
loadOptions().then((response)=>
{
options=response.options;
defaults=response.defaults;
logenabled=response.options.logenabled;
if(logenabled)
console.info("Shrunked Extension: Debug is enabled");
});
}
});