-
Notifications
You must be signed in to change notification settings - Fork 13
/
spamHeaders.js
348 lines (296 loc) · 16 KB
/
spamHeaders.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
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
/* global ChromeUtils, libExperiments, globalThis */
/* exported spamHeaders */
"use strict";
/* eslint-disable no-var */
var {ExtensionCommon} = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm");
var Services = globalThis.Services || ChromeUtils.import("resource://gre/modules/Services.jsm").Services;
var [majorVersion] = Services.appinfo.platformVersion.split(".", 1);
/* eslint-enable no-var */
// eslint-disable-next-line no-var
var spamHeaders = class extends ExtensionCommon.ExtensionAPI {
// eslint-disable-next-line max-lines-per-function
getAPI(context) {
const {ExtensionParent} =
ChromeUtils.import("resource://gre/modules/ExtensionParent.jsm");
const extension = ExtensionParent.GlobalManager
.getExtension("rspamd-spamness@alexander.moisseev");
Services.scriptloader.loadSubScript(extension.getURL("experiments/libExperiments.js"));
let maxHeight = null;
function addHeadersToWindowById(windowId, tabIndex) {
const document = libExperiments.getDocumentByTabIndex(windowId, tabIndex);
(function loadCSS() {
["content/spinner.css", "experiments/spamHeaders.css"].forEach((href, index) => {
const elementId = "rspamd-spamness-css-" + index;
const element = document.getElementById(elementId);
if (element) return;
const link = document.createElement("link");
link.id = elementId;
link.rel = "stylesheet";
link.href = extension.rootURI.resolve(href);
if (majorVersion < 91) {
const referenceNode = document.getElementById("navigation-toolbox");
referenceNode.parentNode.insertBefore(link, referenceNode);
} else {
document.head.appendChild(link);
}
});
}());
const expandedHeaders2 = document
.getElementById(majorVersion < 100 ? "expandedHeaders2" : "extraHeadersArea");
function scoreHeaderRowValue() {
const headerRowValue = document.createElement("td");
const hbox = document.createXULElement("hbox");
const scoreVbox = document.createXULElement("vbox");
const scoreIcon = document.createXULElement("image");
scoreIcon.id = "rspamdSpamnessScoreIcon";
scoreVbox.appendChild(scoreIcon);
hbox.appendChild(scoreVbox);
const scoreHeader = document.createXULElement("mail-headerfield");
scoreHeader.id = "rspamdSpamnessScoreHeader";
hbox.appendChild(scoreHeader);
const bayesVbox = document.createXULElement("vbox");
const bayesIcon = document.createXULElement("image");
bayesIcon.id = "rspamdSpamnessBayesIcon";
bayesVbox.appendChild(bayesIcon);
hbox.appendChild(bayesVbox);
const bayesHeader = document.createXULElement("mail-headerfield");
bayesHeader.id = "rspamdSpamnessBayesHeader";
hbox.appendChild(bayesHeader);
const fuzzyVbox = document.createXULElement("vbox");
const fuzzyIcon = document.createXULElement("image");
fuzzyIcon.id = "rspamdSpamnessFuzzyIcon";
fuzzyVbox.appendChild(fuzzyIcon);
hbox.appendChild(fuzzyVbox);
const fuzzyHeader = document.createXULElement("mail-headerfield");
fuzzyHeader.id = "rspamdSpamnessFuzzyHeader";
hbox.appendChild(fuzzyHeader);
const scanTimeHeader = document.createXULElement("mail-headerfield");
scanTimeHeader.id = "rspamdSpamnessScanTimeHeader";
hbox.appendChild(scanTimeHeader);
const actionHeader = document.createXULElement("mail-headerfield");
actionHeader.id = "rspamdSpamnessActionHeader";
hbox.appendChild(actionHeader);
const notificationVbox = document.createXULElement("vbox");
notificationVbox.id = "rspamdSpamnessNotificationArea";
notificationVbox.classList.add("hidden");
hbox.appendChild(notificationVbox);
const spinner = document.createElement("div");
spinner.id = "in-progress-spinner";
spinner.className = "spinner spinner-hidden";
hbox.appendChild(spinner);
headerRowValue.appendChild(hbox);
return headerRowValue;
}
function rulesHeaderRowValue() {
const headerRowValue = document.createXULElement("mail-multi-linkField");
headerRowValue.id = "expandedRspamdSpamnessRulesBox";
const hbox = document.createXULElement("hbox");
hbox.id = "rulesHeaderValueBox";
const div = document.createXULElement("div");
div.classList.add("headerValue");
div.id = "links";
hbox.appendChild(div);
headerRowValue.appendChild(hbox);
const button = document.createElement("button");
button.id = "heightButton";
button.classList.add("toolbarbutton-1");
headerRowValue.appendChild(button);
return headerRowValue;
}
function createHeaderRow(row) {
const rows = {
rules: {
id: "expandedRspamdSpamnessRulesRow",
titleLabel: {
control: "expandedRspamdSpamnessRulesBox",
id: "expandedRspamdSpamnessRulesLabel",
value: context.extension.localeData
.localizeMessage("spamness.rulesMessageHeader.label")
},
value: rulesHeaderRowValue(),
},
score: {
id: "expandedRspamdSpamnessRow",
titleLabel: {
control: "rspamdSpamnessScoreHeader",
id: "expandedRspamdSpamnessLabel",
value: context.extension.localeData
.localizeMessage("spamness.messageHeader.label")
},
value: scoreHeaderRowValue(),
}
};
const element = document.createElement(majorVersion < 100 ? "tr" : "div");
element.hidden = true;
element.id = rows[row].id;
element.classList.add("message-header-row");
const headerRowTitleLabel = document.createXULElement("label");
headerRowTitleLabel.id = rows[row].titleLabel.id;
headerRowTitleLabel.classList.add(majorVersion < 100 ? "headerName" : "message-header-label");
headerRowTitleLabel.value = rows[row].titleLabel.value;
headerRowTitleLabel.control = rows[row].titleLabel.control;
if (majorVersion < 100) {
const headerRowTitle = document.createElement("th");
headerRowTitle.appendChild(headerRowTitleLabel);
element.appendChild(headerRowTitle);
} else {
element.appendChild(headerRowTitleLabel);
}
element.appendChild(rows[row].value);
expandedHeaders2.appendChild(element);
}
if (expandedHeaders2) {
if (!document.getElementById("expandedRspamdSpamnessRow")) createHeaderRow("score");
if (!document.getElementById("expandedRspamdSpamnessRulesRow")) {
createHeaderRow("rules");
const button = document.getElementById("heightButton");
button.addEventListener("click", function () {
toggleHeaderHeight(document, button.value);
});
}
toggleHeaderHeight(document);
} else {
throw Error("Could not find the expandedHeaders2 element");
}
}
function toggleHeaderHeight(document, value) {
const headerRowValue = document.getElementById("expandedRspamdSpamnessRulesBox");
const button = document.getElementById("heightButton");
if (value === "expand") {
button.value = "collapse";
headerRowValue.classList.remove("fieldCollapsed");
headerRowValue.style["max-height"] = null;
} else {
button.value = "expand";
headerRowValue.classList.add("fieldCollapsed");
headerRowValue.style["max-height"] = maxHeight;
}
const svgPathCommands = {
collapse: "M2 5h3v-3m0 3l-4 -4M10 2v3h3m-3 0l4 -4M2 10h3v3m0 -3l-4 4M10 13v-3h3m-3 0l4 4",
expand: "M1 5V1h4m-4 0l4 4M10 1h4v4m0-4l-4 4M1 10v4h4m-4 0l4 -4M10 14h4v-4m0 4l-4 -4"
};
button.innerHTML =
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 15" width="16" height="16"
fill="none" stroke="currentColor" stroke-linecap="round">
<path d="${svgPathCommands[button.value]}"/>
</svg>`;
if (majorVersion < 100) document.getElementById("expandedHeaderView").removeAttribute("height");
}
context.callOnClose(this);
return {
spamHeaders: {
addHeadersToWindowById(windowId, tabIndex) {
addHeadersToWindowById(windowId, tabIndex);
},
addSymbol(windowId, tabIndex, symbolClass, displayText, tooltiptext) {
const document = libExperiments.getDocumentByTabIndex(windowId, tabIndex);
if (!document) return;
const link = document.createXULElement("mail-link");
if (symbolClass !== "") {
link.classList.add(symbolClass);
}
if (typeof tooltiptext !== "undefined") {
link.setAttribute("tooltiptext", tooltiptext);
}
const description = document.createXULElement("description");
description.classList.add("linkDisplayButton");
description.role = "textbox";
description["aria-readonly"] = true;
["click", "contextmenu"].forEach((e) => {
description.addEventListener(e, (event) => {
const popup = document.getElementById("rspamdSpamnessSymbolPopup");
popup.headerField = event.target;
popup.openPopupAtScreen(event.screenX, event.screenY, true);
});
});
description.innerHTML = displayText;
link.appendChild(description);
const comma = document.createXULElement("description");
comma.classList.add("linkSeparator");
comma.innerHTML = ",";
const links = document.getElementById("links");
if (links.lastChild) links.lastChild.appendChild(comma);
links.appendChild(link);
},
clearSymbolsHeader(windowId, tabIndex, show_n_lines) {
const document = libExperiments.getDocumentByTabIndex(windowId, tabIndex);
const parent = document.getElementById("links");
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
if (show_n_lines === "0") {
maxHeight = "auto";
document.getElementById("heightButton").hidden = true;
document.getElementById("expandedRspamdSpamnessRulesBox").classList.remove("fieldCollapsed");
} else {
const {lineHeight} = document.defaultView.getComputedStyle(parent, null);
const numericLineHeight = (lineHeight === "normal") ? "1em" : lineHeight;
maxHeight = "calc((" + numericLineHeight + " + 0.1em + 2px) * " + show_n_lines + ")";
document.getElementById("heightButton").hidden = false;
toggleHeaderHeight(document, "collapse");
}
},
setHeaderHidden(windowId, tabIndex, elementId, hidden) {
// Ensure that the all visible labels have the same size.
function syncGridColumnWidths(document) {
const allHeaderLabels = document
.querySelectorAll(".message-header-row:not([hidden]) .message-header-label");
// Clear existing style.
for (const label of allHeaderLabels) {
label.style.minWidth = null;
}
const minWidth = Math.max(...Array.from(allHeaderLabels, (i) => i.clientWidth));
for (const label of allHeaderLabels) {
label.style.minWidth = `${minWidth}px`;
}
}
const document = libExperiments.getDocumentByTabIndex(windowId, tabIndex);
if (!document) return;
const element = document.getElementById(elementId);
if (element) {
element.hidden = hidden;
if (majorVersion >= 100) syncGridColumnWidths(document);
}
},
setHeaderValue(windowId, tabIndex, elementId, prop, value) {
const document = libExperiments.getDocumentByTabIndex(windowId, tabIndex);
if (!document) return;
const element = document.getElementById(elementId);
if (prop === "src") {
element[prop] = extension.rootURI.resolve(value);
} else if (prop === "notificationClass") {
if (elementId === "rspamdSpamnessNotificationArea") {
const validLogLevels = ["log", "info", "warn", "error"];
validLogLevels.forEach((level) => element.classList.remove(level));
element.classList.add(value.toLowerCase());
}
} else if (prop === "classListAdd") {
element.classList.add(value);
} else if (prop === "classListRemove") {
element.classList.remove(value);
} else {
element.textContent = value;
if (elementId === "rspamdSpamnessActionHeader") {
element.style["background-color"] =
["no action", "rewrite subject", "add header"].includes(value)
? "var(--" + value.replace(/\s/g, "-") + ")"
: null;
}
// Hide notification area border if content is empty.
if (elementId === "rspamdSpamnessNotificationArea")
element.classList.toggle("hidden", !value.trim());
}
},
},
};
}
// eslint-disable-next-line class-methods-use-this
close() {
libExperiments.removeElements([
"expandedRspamdSpamnessRow",
"expandedRspamdSpamnessRulesRow",
"rspamd-spamness-css-0",
"rspamd-spamness-css-1",
], majorVersion < 100);
}
};