-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwfes-AddTranslationButtons.js
172 lines (148 loc) · 5.18 KB
/
wfes-AddTranslationButtons.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
// @name Add Translation Buttons
// @version 2.1.5
// @description Adds a button to translate the text associated with a wayspot
// @author AlterTobi
(function() {
"use strict";
const myCSSId = "wfesTranslateCSS";
const myStyle = `.wfesTranslate {
color: #333;
margin-left: 2em;
padding-top: 0.3em;
text-align: center;
display: block;
}
.dark .wfesTranslate {
color: #ddd;
}
.wfesTranslate select {
margin-bottom: 0.2em; /* Abstand zwischen Dropdown und Button */
}
.wfesTranslateButton {
display: block; /* Button wird unterhalb des Selects angezeigt */
text-decoration: none;
color: #20B8E3;
}
.dark .wfesTranslate select,
.dark .wfesTranslate option {
background: #000;
}`;
const engines ={
Google: {name: "Google", title: "Google translate", url: "https://translate.google.com/?sl=auto&q=", target: "wfesTranslateGoogle"},
Deepl: {name: "Deepl", title: "DeepL translate", url: "https://www.deepl.com/translator#auto/"+navigator.language+"/", target: "wfesTranslateDeepl"}
};
const buttonID = "wfesTranslateButton";
const storageName = "wfes_translateEngine";
let currentEngine;
function removeButton() {
const button = document.getElementById(buttonID);
if (button !== null) {
button.remove();
}
}
function init() {
window.wfes.f.addCSS(myCSSId, myStyle);
window.wfes.f.localGet(storageName, "Deepl").then(e => {
currentEngine = e;
});
}
function createButton(text) {
window.wfes.f.waitForElem("wf-logo").then(elem=>{
const buttonEl = document.getElementById(buttonID);
if (null === buttonEl) {
const div = document.createElement("div");
div.className = "wfesTranslate";
div.id = buttonID;
const link = document.createElement("a");
link.title = "Translate nomination";
link.className = "wfesTranslateButton";
link.innerHTML = '<span class="material-icons">translate</span>';
const select = document.createElement("select");
select.title = "Select translation engine";
for (const engineName of Object.keys(engines)) {
const engine = engines[engineName];
const option = document.createElement("option");
option.value = engine.name;
if (engine.name === currentEngine) {
option.setAttribute("selected", "true");
link.target = engine.target;
link.href = engine.url + encodeURIComponent(text);
}
option.innerText = engine.title;
select.appendChild(option);
}
select.addEventListener("change", function() {
currentEngine = select.value;
window.wfes.f.localSave(storageName, currentEngine);
link.href = engines[currentEngine].url + encodeURIComponent(text);
link.target = engines[currentEngine].target;
});
div.appendChild(select);
div.appendChild(link);
const container = elem.parentNode.parentNode;
container.appendChild(div);
} else {
const a = buttonEl.querySelector("a");
a.href = engines[currentEngine].url + encodeURIComponent(text);
a.target = engines[currentEngine].url;
}
})
.catch((e) => {console.warn(GM_info.script.name, ": ", e);});
}
function addTranslationButtonsNew() {
const candidate = window.wfes.g.reviewPageData();
let allText = candidate.title + "\n\n";
allText += candidate.description + "\n\n";
if (candidate.supportingImageUrl) {
allText += candidate.statement;
}
createButton(allText);
}
function addTranslationButtonsEdit( ) {
const candidate = window.wfes.g.reviewPageData();
const edit = window.wfes.g.edit();
let allText = "";
// has title
if (candidate.title) {
allText += candidate.title + "\n\n";
}
// has description
if (candidate.description) {
allText += candidate.description + "\n\n";
}
// is title-edit
if (edit.what.title) {
for (let i = 0; i < candidate.titleEdits.length; i++) {
allText += candidate.titleEdits[i].value + "\n\n";
}
}
// is description-edit
if (edit.what.description) {
for (let i = 0; i < candidate.descriptionEdits.length; i++) {
const value = candidate.descriptionEdits[i].value;
if (value) {
allText += value + "\n\n";
}
}
}
createButton(allText);
}
function addTranslationButtonsPhoto() {
const candidate = window.wfes.g.reviewPageData();
let allText = "";
if(candidate.title) {
allText += candidate.title + "\n\n";
}
if(candidate.description) {
allText += candidate.description;
}
createButton(allText);
}
init();
window.addEventListener("WFESReviewPageNewLoaded", addTranslationButtonsNew);
window.addEventListener("WFESReviewPageEditLoaded", addTranslationButtonsEdit);
window.addEventListener("WFESReviewPagePhotoLoaded", addTranslationButtonsPhoto);
window.addEventListener("WFESReviewDecisionSent", removeButton);
/* we are done :-) */
console.log("Script loaded:", GM_info.script.name, "v" + GM_info.script.version);
})();