-
Notifications
You must be signed in to change notification settings - Fork 13
/
trainButtons.js
140 lines (118 loc) · 5.94 KB
/
trainButtons.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
/* global ChromeUtils, libExperiments, globalThis */
/* exported trainButtons */
"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 trainButtons = class extends ExtensionCommon.ExtensionAPI {
// eslint-disable-next-line class-methods-use-this
onShutdown(isAppShutdown) {
if (isAppShutdown) return;
removeButtonsFromAllWindows();
}
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"));
function appendButtons(windowId, tabIndex) {
const targetIds = [];
const document = libExperiments.getDocumentByTabIndex(windowId, tabIndex);
const toolbar = document.getElementById("header-view-toolbar");
function appendToolbarButton(hamSpam, imageURL) {
const toolbarbuttonId = "rspamdSpamnessButton" + hamSpam;
if (document.getElementById(toolbarbuttonId)) return;
// The button container
const toolbarbutton = document.createXULElement("toolbarbutton");
toolbarbutton.setAttribute("is", "toolbarbutton-menu-button");
toolbarbutton.setAttribute("type", majorVersion < 111 ? "menu-button" : "menu");
toolbarbutton.id = toolbarbuttonId;
toolbarbutton.classList
.add("toolbarbutton-1", majorVersion < 100 ? "msgHeaderView-button" : "message-header-view-button");
toolbarbutton.setAttribute(
"tooltiptext",
context.extension.localeData
.localizeMessage("spamness.buttonTrain" + hamSpam + ".tooltip")
);
const primaryButton = document.createXULElement("toolbarbutton");
primaryButton.classList.add("box-inherit", "toolbarbutton-menubutton-button");
const img = document.createElement("img");
img.src = imageURL;
primaryButton.appendChild(img);
const label = document.createElement("label");
if (majorVersion < 110) label.style.display = "-moz-inline-box";
label.innerHTML = context.extension.localeData
.localizeMessage("spamness.buttonTrain" + hamSpam + ".label");
primaryButton.appendChild(label);
toolbarbutton.appendChild(primaryButton);
const dropmarker = document.createXULElement("dropmarker");
dropmarker.classList.add("toolbarbutton-menubutton-dropmarker");
toolbarbutton.appendChild(dropmarker);
const menupopup = document.createXULElement("menupopup");
["move", "copy", "bayes", "fuzzy", "check"].forEach((action) => {
const item = document.createXULElement("menuitem");
const itemLabel = context.extension.localeData.localizeMessage("spamness.action.label." + action);
item.setAttribute("label", itemLabel);
item.setAttribute("data-action", action);
menupopup.appendChild(item);
});
toolbarbutton.appendChild(menupopup);
dropmarker.addEventListener("click", (event) => {
event.stopPropagation();
menupopup.openPopup(toolbarbutton, "after_end", 0, 0, true, false);
});
toolbar.insertBefore(toolbarbutton, toolbar.firstChild);
targetIds.push(toolbarbuttonId);
}
if (toolbar) {
appendToolbarButton("Spam", extension.getURL("images/arrow-up.png"));
appendToolbarButton("Ham", extension.getURL("images/arrow-down.png"));
} else {
// eslint-disable-next-line no-console
console.error("Could not find the header-view-toolbar element");
}
return targetIds;
}
context.callOnClose(this);
return {
trainButtons: {
addButtonsToWindowById(windowId, tabIndex) {
return appendButtons(windowId, tabIndex);
},
onButtonCommand: new ExtensionCommon.EventManager({
context,
name: "trainButtons.onButtonCommand",
register(fire, targetId, windowId, tabIndex) {
function handleButtonClick(event) {
const selectedAction = event.target.dataset.action;
return fire.async(selectedAction);
}
const document = libExperiments.getDocumentByTabIndex(windowId, tabIndex);
const target = document.getElementById(targetId);
target.addEventListener("command", handleButtonClick);
return function () {
target.removeEventListener("command", handleButtonClick);
};
}
}).api(),
removeButtons() {
removeButtonsFromAllWindows();
},
},
};
}
// eslint-disable-next-line class-methods-use-this
close() {
removeButtonsFromAllWindows();
}
};
function removeButtonsFromAllWindows() {
libExperiments.removeElements([
"rspamdSpamnessButtonHam",
"rspamdSpamnessButtonSpam"
]);
}