forked from tomouchuu/mastodon-translate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.user.js
215 lines (182 loc) · 7.99 KB
/
index.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
// ==UserScript==
// @name Mastodon Translate
// @namespace https://jabberwocky.moe/@alice
// @version 1.23.0
// @description Provides a translate toot option for Mastodon users via Google Translate. Works with Mastodon 3.2.1
// @author [email protected] / https://niu.moe/@tomo / umonaca / [email protected]
// @match https://*/web/*
// @match https://*/settings/preferences/appearance
// @connect translate.uchuu.io
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @grant GM.getValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @grant GM_xmlHttpRequest
// @grant GM.xmlHttpRequest
// ==/UserScript==
(function() {
'use strict';
// Set Defaults if not set
if(!localStorage.getItem('lang')) {
localStorage.setItem('lang', GM_getValue('lang', 'en'));
};
function stripMentions(text) {
var regex = /(@[a-z0-9]+)/;
var mentions = [];
var cleanText = text.replace(regex, function(match) {
mentions.push(match);
return '';
}).trim();
return {
mentions,
text: cleanText
};
}
function getTranslation(status, language) {
var statusDetail = stripMentions(status.querySelector('div.status__content').textContent);
var encodedText = encodeURIComponent(statusDetail.text);
var url = "https://translate.uchuu.io/"+language+'/'+encodedText;
GM.xmlHttpRequest({
method: 'GET',
headers: {
'Accept': 'application/json'
},
url: url,
onload: function(res) {
var resJson = JSON.parse(res.responseText);
var translatedText = statusDetail.mentions.join(' ') + ' ' + resJson.text;
var translateArea = document.createElement('p');
translateArea.classList.add('toot__translation');
translateArea.innerHTML = '<br>Translated: '+translatedText;
status.querySelector('div.status__content').appendChild(translateArea);
document.querySelector('li.translate__toot').remove();
if (document.querySelector('div.dropdown-menu') !== null) {
status.querySelector('i.fa.fa-ellipsis-h').click();
};
},
onabort: function() {
console.log('There was an abort');
},
ontimeout: function() {
console.log('It timeout');
},
onerror: function() {
console.log('There was an error');
}
});
}
function addTranslateLink(status) {
setTimeout(function() {
var dropdown = document.querySelector('div.dropdown-menu ul');
var separator = dropdown.querySelector('li.dropdown-menu__separator');
var listItem = document.createElement('li');
listItem.classList.add('dropdown-menu__item');
listItem.classList.add('translate__toot');
var link = document.createElement('a');
link.setAttribute('href', '#');
link.setAttribute('target', '_blank');
var target_lang = localStorage.getItem('lang');
if (target_lang == 'zh-CN') {
link.textContent = '翻译嘟文';
} else if (target_lang == 'zh-HK') {
link.textContent = '翻譯嘟文';
} else if (target_lang == 'zh-TW') {
link.textContent = '翻譯嘟文';
} else {
link.textContent = 'Translate Toot';
};
link.addEventListener('click', function(e) {
e.preventDefault();
if (status.querySelectorAll('p.toot__translation').length === 0) {
link.textContent = 'Loading...';
getTranslation(status, localStorage.getItem('lang'));
};
}, false);
listItem.appendChild(link);
dropdown.insertBefore(listItem, separator);
}, 100);
}
function saveSettings(event) {
if (event.target.tagName.toLowerCase() === 'button' && event.target.getAttribute('type') === 'submit') {
event.preventDefault();
var input = document.getElementById('translation_locale');
var selectedLanguage = input.options[input.selectedIndex].value;
if (selectedLanguage == 'zh-HK') {
localStorage.setItem('lang', 'zh-TW');
} else {
localStorage.setItem('lang', selectedLanguage);
};
setTimeout(function() {
document.querySelector('body').removeEventListener('click', saveSettings, false);
actions.children[0].click();
}, 500);
}
}
function chromeClickChecker(event) {
return(
event.target.tagName.toLowerCase() === 'i' &&
event.target.classList.contains('fa-ellipsis-h') &&
document.querySelector('div.dropdown-menu') === null
);
}
function firefoxClickChecker(event) {
return(
event.target.tagName.toLowerCase() === 'button' &&
event.target.classList.contains('icon-button') &&
document.querySelector('div.dropdown-menu') === null
);
}
function activateMastodonTranslate() {
document.querySelector('body').addEventListener('click', function(event) {
if (chromeClickChecker(event) || firefoxClickChecker(event)) {
// Get the status for this event
var status = event.target.parentNode.parentNode.parentNode.parentNode.parentNode;
addTranslateLink(status);
};
}, false);
}
// Launch Script
console.log(`Translate Script v${GM.info.script.version} Activating...`);
window.addEventListener("load", function() {
if (window.innerWidth > 630) {
// Checks we're on a mastodon instance
var settingsEl = document.querySelector('i.fa.fa-fw.fa-cog').parentElement;
var settingsUrl = settingsEl.getAttribute('href');
if (settingsUrl === '/settings/preferences') {
activateMastodonTranslate();
} else {
// Other cases
}
} else {
console.log('Sorry, desktop only');
}
}, false);
if (window.location.pathname === '/settings/preferences/appearance') {
// We're on the settings page
var form = document.querySelector('form.simple_form');
var actions = document.querySelector('div.actions');
var settingsGroup = form.querySelectorAll('div.fields-group')[2].cloneNode(true);
settingsGroup.children[0].remove();
var languageArea = form.querySelector('div.fields-group').cloneNode(true);
languageArea.classList.remove('fields-row__column');
languageArea.classList.remove('fields-row__column-6');
var interface_lang = languageArea.querySelector("select[name='user[locale]']").value;
var label = languageArea.querySelector(".select .optional[for=user_locale]");
label.setAttribute('for', 'translation_locale');
if (interface_lang == 'zh-CN') {
label.textContent = '使用以下语言翻译';
} else if (interface_lang == 'zh-HK') {
label.textContent = '使用以下語言翻譯';
} else if (interface_lang == 'zh-TW') {
label.textContent = '使用以下語言翻譯';
} else {
label.textContent = 'Translation Language';
};
var selector = languageArea.querySelector("select[name='user[locale]']");
selector.setAttribute('id', 'translation_locale');
selector.value = localStorage.getItem('lang');
settingsGroup.appendChild(languageArea);
form.insertBefore(settingsGroup, form.firstChild);
document.querySelector('body').addEventListener('click', saveSettings, false);
}
})();