-
Notifications
You must be signed in to change notification settings - Fork 20
/
wayfarer-translate.user.js
222 lines (184 loc) · 6.26 KB
/
wayfarer-translate.user.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
// ==UserScript==
// @name Wayfarer Translate
// @version 0.3.3
// @description Add translate option to Wayfarer
// @namespace https://github.com/tehstone/wayfarer-addons
// @downloadURL https://github.com/tehstone/wayfarer-addons/raw/main/wayfarer-translate.user.js
// @homepageURL https://github.com/tehstone/wayfarer-addons
// @match https://wayfarer.nianticlabs.com/*
// ==/UserScript==
// Copyright 2024 AlfonsoML, tehstone, bilde
// This file is part of the Wayfarer Addons collection.
// This script is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This script is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You can find a copy of the GNU General Public License in the root
// directory of this script's GitHub repository:
// <https://github.com/tehstone/wayfarer-addons/blob/main/LICENSE>
// If not, see <https://www.gnu.org/licenses/>.
/* eslint-env es6 */
/* eslint no-var: "error" */
/* eslint indent: ['error', 4] */
function init() {
let tryNumber = 15;
let translateButton;
let candidate;
const SPACING = '\r\n\r\n';
let engine = localStorage['translate-engine'];
if (!engine) {
engine = 'Google';
localStorage['translate-engine'] = engine;
}
/**
* Overwrite the open method of the XMLHttpRequest.prototype to intercept the server calls
*/
(function (open) {
XMLHttpRequest.prototype.open = function (method, url) {
if (url == '/api/v1/vault/review') {
if (method == 'GET') {
this.addEventListener('load', parseCandidate, false);
}
if (method == 'POST') {
hideButton();
}
}
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
addCss();
function parseCandidate(e) {
try {
const response = this.response;
const json = JSON.parse(response);
if (!json) {
console.log(response);
alert('Failed to parse response from Wayfarer');
return;
}
// ignore if it's related to captchas
if (json.captcha)
return;
if (json.code != 'OK')
return;
candidate = json.result;
if (!candidate) {
console.log(json);
alert('Wayfarer\'s response didn\'t include a candidate.');
return;
}
addTranslateButton();
} catch (e) {
console.log(e); // eslint-disable-line no-console
}
}
function getTranslatorLink() {
switch (engine) {
case 'Google':
return 'https://translate.google.com/?sl=auto&q=';
default:
return 'https://www.deepl.com/translator#auto/' + navigator.language + '/';
}
}
function createButton(ref) {
if (!translateButton) {
const div = document.createElement('div');
div.className = 'wayfarertranslate';
const link = document.createElement('a');
link.className = '';
link.title = 'Translate nomination';
link.innerHTML = '<svg viewBox="0 0 24 24"><path d="M12.87 15.07l-2.54-2.51.03-.03A17.52 17.52 0 0014.07 6H17V4h-7V2H8v2H1v2h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04M18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12m-2.62 7l1.62-4.33L19.12 17h-3.24z"/></svg>';
link.target = '_blank';
const select = document.createElement('select');
select.title = 'Select translation engine';
const engines = [
{name: 'Google', title: 'Google Translate'},
{name: 'DeepL', title: 'DeepL Translate'}
];
select.innerHTML = engines.map(item => `<option value="${item.name}" ${item.name == engine ? 'selected' : ''}>${item.title}</option>`).join('');
select.addEventListener('change', function () {
engine = select.value;
localStorage['translate-engine'] = engine;
link.href = getTranslatorLink() + encodeURIComponent(link.dataset.text);
});
div.appendChild(link);
div.appendChild(select);
translateButton = div;
}
const container = ref.parentNode.parentNode;
if (!container.contains(translateButton))
container.appendChild(translateButton);
}
function addTranslateButton() {
const ref = document.querySelector('wf-logo');
if (!ref) {
if (tryNumber === 0) {
document.querySelector('body')
.insertAdjacentHTML('afterBegin', '<div class="alert alert-danger"><strong><span class="glyphicon glyphicon-remove"></span> Wayfarer Translate initialization failed, refresh page</strong></div>');
return;
}
setTimeout(addTranslateButton, 1000);
tryNumber--;
return;
}
let text = '';
if (candidate.type == 'NEW' || candidate.type == 'NEW_B') {
text = candidate.title + SPACING + candidate.description + SPACING + candidate.statement;
}
if (candidate.type == 'EDIT'|| candidate.type == 'EDIT_B') {
const title = candidate.title || candidate.titleEdits.map(d=>d.value).join(SPACING);
const description = candidate.description || candidate.descriptionEdits.map(d=>d.value).join(SPACING);
text = title + SPACING + SPACING + description;
}
if (candidate.type == 'PHOTO'|| candidate.type == 'PHOTO_B') {
text = candidate.title + SPACING + candidate.description;
}
if (text != '') {
createButton(ref);
const link = translateButton.querySelector('a');
link.dataset.text = text;
link.href = getTranslatorLink() + encodeURIComponent(text);
translateButton.classList.add('wayfarertranslate__visible');
}
}
function hideButton() {
translateButton.classList.remove('wayfarertranslate__visible');
}
function addCss() {
const css = `
.wayfarertranslate {
color: #333;
margin-left: 2em;
padding-top: 0.3em;
text-align: center;
display: none;
}
.wayfarertranslate__visible {
display: inline;
}
.wayfarertranslate svg {
width: 24px;
height: 24px;
filter: none;
fill: currentColor;
margin: 0 auto;
}
.dark .wayfarertranslate {
color: #ddd;
}
.dark .wayfarertranslate select,
.dark .wayfarertranslate option {
background: #000;
}
`;
const style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
document.querySelector('head').appendChild(style);
}
}
init();