-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepl-helper.js
67 lines (61 loc) · 2.85 KB
/
deepl-helper.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
// ==UserScript==
// @name DeepL - Auto VN Translation Extension Helper
// @version 1.2.4
// @grant GM.setClipboard
// @match https://www.deepl.com/translator
// @description Watch for target language element class change, copy new value of textarea to clipboard each time change has been detected.
// @author Zero_G
// @icon https://www.deepl.com/img/logo/deepl-logo-blue.svg
// @namespace Zero_G.autovntranslation
// ==/UserScript==
(function() {
'use strict';
// Filters to apply to translated text
// These were originally here, so leaving them as is for compatibility to previous caches (as filtering should be done in setClipboardText)
const filters = {
// '' : /\"+|\'\'+/g, // Remove " or ''
// '...' : /\.\.\.\.+/g // Change multiple dots (when there are more than 3 to '...' only)
}
const mutationObserver = new MutationObserver(callback)
// Observe the text div that contain the translation
// But as the translated text appears in a <p> in a ::before css we need to watch
// for added nodes
mutationObserver.observe(
document.querySelector('#translation-target-heading').parentNode, {
childList: true,
subtree: true
}
)
function callback(mutationsList) {
mutationsList.forEach(mutation => {
if (mutation.type === 'childList' && // Irrelevant as we are already watching for childList only but meh
mutation.addedNodes.length !== 0 && // Looking for a mutation with an added node
mutation.removedNodes.length !== 0 && // This condition is to prevent a repeat
!mutation.addedNodes[0].innerHTML.includes('<br')) { // Filter out garbage while translating
// Get text from <p> (get as value doesn't work)
let text = '';
// Check for added elments/mutations of element type <p>
for(let i in mutation.addedNodes){
let p = mutation.addedNodes[i]
if(p.tagName == 'P'){
// If <p> has spans, get text from them
if (p.firstChild) {
for(let i of p.childNodes){
if(i.innerHTML) text = text + i.textContent;
}
} else {
// If text is isn't inside spans
text = mutation.addedNodes[0].textContent;
}
}
}
// Apply filters
for (const [key, value] of Object.entries(filters)) {
text = text.replace(value, key);
}
// Copy to memory with GreaseMonkey special function (needs @grant)
if(text) GM.setClipboard(text);
}
})
}
})();