-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
139 lines (114 loc) · 4.3 KB
/
main.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
// ==UserScript==
// @name GoogleTranslate_paperHelper
// @version 2.8.0
// @description ez way to C and V
// @author NDM
// @include https://translate.google.com*
// @grant unsafeWindow
// @namespace https://greasyfork.org/users/180333
// ==/UserScript==
(function () {
classList = []
classList["inputText"] = ['text-dummy', 'D5aOJc'];
classList["outputText"] = ['tlid-translation', 'J0lOec'];
classList["buttonArea"] = ['tlid-input-button-container', 'hgbeOc'];
classList["textSource"] = ['tlid-source-text-input', 'er8xn'];
function getElement(s = '') {
r = document.getElementsByClassName(classList[s][0]);
return r[0] ? r[0] : document.getElementsByClassName(classList[s][1])[0];
}
var buttonSite = getElement('buttonArea');
var newButton1 = document.createElement("span");
var newButton3 = document.createElement("span");
var newButton4 = document.createElement("span");
var s = ""
newButton1.innerHTML = "排版";
newButton1.style.borderStyle = "solid";
newButton1.style.cursor = "pointer";
newButton1.className = "mishka";
newButton1.onclick = () => {
source = getElement("textSource");
//preprocessing
source.value = source.value.replace(/\n/g, " ");
source.value = source.value.replace(/\;/g, ";.");
source.value = source.value.replace(/i\.e\./g, "i*e*"); // i.e.
source.value = source.value.replace(" al.", " al"); // ... al.
//format
source.value = source.value.replace(/\. /g, ".\n\n");
source.value = source.value.replace(/\? /g, "?\n\n");
source.value = source.value.replace(/\! /g, "!\n\n");
source.value = source.value.replace(/i\*e\*/g, "i.e."); // i.e.
source.value = source.value.replace(/ /g, " ");
source.value = source.value.replace(/\n /g, "\n");
source.value = source.value.replace(/\;\./g, ";");
// s = source.value.split(".\n\n");
source.value += "\n\n";
s = "done"
}
newButton3.innerHTML = "比對(英+中)";
newButton3.style.borderStyle = "solid";
newButton3.style.cursor = "pointer";
newButton3.className = "mishka";
newButton3.onclick = () => {
org = getElement('inputText');
tran = getElement('outputText');
orgText = org.innerHTML.split('\n')
tranText = tran.innerText.split('\n')
var r = true
if (tranText.length >= orgText.length * 1.5)
r = confirm("已產生比對結果,再執行會毀掉畫面,仍要執行?\n若要重新比對,先讓Goolge重新翻譯")
if (r == false)
return
result = ''
for (var i = 0; i <= tranText.length; i++) {
if (orgText[i] == null)
break
if (orgText[i] != '')
temp = '<span style="color:blue;">' + orgText[i] + '</span>' + '<br>' + '<span>' + tranText[i] + '</span><br>'
else
temp = '<br>'
result += temp
}
tran.innerHTML = result
}
newButton4.innerHTML = "比對(中+英)";
newButton4.style.borderStyle = "solid";
newButton4.style.cursor = "pointer";
newButton4.className = "mishka";
newButton4.onclick = () => {
org = getElement('inputText');
tran = getElement('outputText');
orgText = org.innerHTML.split('\n')
tranText = tran.innerText.split('\n')
var r = true
if (tranText.length >= orgText.length * 1.5)
r = confirm("已產生比對結果,再執行會毀掉畫面,仍要執行?\n若要重新比對,先讓Goolge重新翻譯")
if (r == false)
return
result = ''
for (var i = 0; i <= tranText.length; i++) {
if (orgText[i] == null)
break
if (orgText[i] != '')
temp = '<span>' + tranText[i] + '</span>' + '<br>' + '<span style="color:blue;font-size:10pt"> ' + orgText[i] + '</span><br>'
else
temp = '<br>'
result += temp
}
tran.innerHTML = result
}
buttonSite.appendChild(newButton1);
buttonSite.appendChild(newButton3);
buttonSite.appendChild(newButton4);
var cssId = 'mishka'; // you could encode the css path itself to generate id..
if (!document.getElementById(cssId)) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'https://raw.githack.com/aniki0220/Paper-Translate-Format-Helper-in-Google-Translator/master/style.css';
link.media = 'all';
head.appendChild(link);
}
})();