This repository was archived by the owner on Mar 25, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
105 lines (93 loc) · 3.01 KB
/
Copy pathbackground.js
File metadata and controls
105 lines (93 loc) · 3.01 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
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.composeAction.disable();
browser.composeScripts.register({
js: [
{
file: "compose_script.js",
},
],
});
let sendDeferred;
browser.runtime.onMessage.addListener(async (message, sender, callback) => {
// Image added to body of message. Return a promise to the sender.
if (message.type == "beginResize") {
return updateEnabledState(sender.tab, true);
}
// Image removed from body of message.
if (message.type == "updateEnabledState") {
await updateEnabledState(sender.tab, false);
}
// Options window requesting a file.
if (message.type == "fetchFile") {
return Promise.resolve(tabMap.get(message.tabId)[message.index].file);
}
// Pop-up OK button.
if (message.type == "completeSend") {
await updateEnabledState(sender.tab, false);
sendDeferred?.resolve();
}
// Pop-up cancel button.
if (message.type == "cancelSend") {
await updateEnabledState(sender.tab, false);
sendDeferred?.reject();
}
return undefined;
});
async function updateEnabledState(tab, showPopup, ignoreResized) {
let inlineImages = await browser.tabs.sendMessage(tab.id, { type: "listInlineImages", ignoreResized });
let attachments = [];
for (let a of await browser.compose.listAttachments(tab.id)) {
if (await shouldResize(a)) {
attachments.push(a);
}
}
console.debug(`found ${inlineImages.length} inline images and ${attachments.length} image attachments`);
if (inlineImages.length > 0 || attachments.length > 0) {
await browser.composeAction.enable(tab.id);
if (showPopup) {
await browser.composeAction.openPopup({ windowId: tab.window });
}
return true;
}
browser.composeAction.disable(tab.id);
return false;
}
// Attachment added to message.
browser.compose.onAttachmentAdded.addListener(async (tab) => {
let { resizeAttachmentsOnSend } = await browser.storage.local.get({
resizeAttachmentsOnSend: false,
});
updateEnabledState(tab, !resizeAttachmentsOnSend);
});
// Attachment removed from the message.
browser.compose.onAttachmentRemoved.addListener(tab => updateEnabledState(tab, false));
// Message sending.
browser.compose.onBeforeSend.addListener(async (tab, details) => {
let result = {};
let { resizeAttachmentsOnSend } = await browser.storage.local.get({
resizeAttachmentsOnSend: false,
});
if (!resizeAttachmentsOnSend) {
return result;
}
sendDeferred = Promise.withResolvers();
if (await updateEnabledState(tab, true, true)) {
await sendDeferred.promise.catch(() => (result.cancel = true));
}
return result;
});
// Clean up.
browser.tabs.onRemoved.addListener(tabId => {
tabMap.delete(tabId);
});