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
/
compose_script.js
134 lines (125 loc) · 3.96 KB
/
compose_script.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
let config = {
attributes: false,
childList: true,
characterData: false,
subtree: true,
};
let observer = new MutationObserver(function(mutations) {
for (let mutation of mutations) {
if (mutation.addedNodes && mutation.addedNodes.length) {
console.log("Nodes added to message: " + mutation.addedNodes.length);
for (let target of mutation.addedNodes) {
maybeResizeInline(target);
}
}
}
});
observer.observe(document.body, config);
async function maybeResizeInline(target) {
if (target.nodeName == "IMG") {
try {
console.log(
"<IMG> found, source is " +
target.src.substring(0, 100) +
(target.src.length <= 100 ? "" : "\u2026")
);
let parent = target.parentNode;
while (parent && "classList" in parent) {
if (parent.classList.contains("moz-signature")) {
console.log("Not resizing - image is part of signature");
return;
}
if (parent.getAttribute("type") == "cite") {
console.log("Not resizing - image is part of message being replied to");
return;
}
if (parent.classList.contains("moz-forward-container")) {
console.log("Not resizing - image is part of forwarded message");
return;
}
parent = parent.parentNode;
}
if (!target.complete) {
target.addEventListener(
"load",
() => {
console.log("Image now loaded, calling maybeResizeInline");
maybeResizeInline(target);
},
{ once: true }
);
console.log("Image not yet loaded");
return;
}
if (target.hasAttribute("shrunked:resized")) {
console.log("Not resizing - image already has shrunked attribute");
return;
}
if (!imageIsJPEG(target)) {
console.log("Not resizing - image is not JPEG");
return;
}
if (target.width < 500 && target.height < 500) {
console.log("Not resizing - image is too small");
return;
}
let src = target.getAttribute("src");
if (/^data:/.test(src)) {
let srcSize = ((src.length - src.indexOf(",") - 1) * 3) / 4;
if (src.endsWith("=")) {
srcSize--;
if (src.endsWith("==")) {
srcSize--;
}
}
let { fileSizeMinimum } = await browser.storage.local.get({
fileSizeMinimum: 100,
});
if (srcSize < fileSizeMinimum * 1024) {
console.log("Not resizing - image file size is too small");
return;
}
}
let srcName = "";
let nameParts = target.src.match(/;filename=([^,;]*)[,;]/);
if (nameParts) {
srcName = decodeURIComponent(nameParts[1]);
}
let response = await fetch(src);
let srcBlob = await response.blob();
let srcFile = new File([srcBlob], srcName);
let destFile = await browser.runtime.sendMessage({
type: "resizeFile",
file: srcFile,
});
if (destFile === null) {
return;
}
let destURL = await new Promise(resolve => {
let reader = new FileReader();
reader.onloadend = function() {
let dataURL = reader.result;
dataURL =
"data:image/jpeg;filename=" + encodeURIComponent(destFile.name) + dataURL.substring(15);
resolve(dataURL);
};
reader.readAsDataURL(destFile);
});
target.setAttribute("src", destURL);
target.removeAttribute("width");
target.removeAttribute("height");
target.setAttribute("shrunked:resized", "true");
} catch (ex) {
console.error(ex);
}
} else if (target.nodeType == Node.ELEMENT_NODE) {
console.log("<" + target.nodeName + "> found, checking children");
for (let child of target.children) {
maybeResizeInline(child);
}
}
}
function imageIsJPEG(image) {
let src = image.src.toLowerCase();
return src.startsWith("data:image/jpeg") || src.endsWith(".jpg");
}