This repository was archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
110 lines (99 loc) · 3.81 KB
/
index.js
File metadata and controls
110 lines (99 loc) · 3.81 KB
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
var varray;
window.onload = () => {
document.querySelector('#upload').addEventListener('change', handleFile, false)
document.getElementById('encode').onchange = function() {
let encode = this.options[this.selectedIndex].value;
$('#preview pre').text((new TextDecoder(encode)).decode(varray.slice(0,100)))
}
function handleFile(e) {
let reader = new FileReader;
let file = e.target.files[0];
let fileName = e.target.files[0].name
$('#uploader span.description').text(fileName)
$('#download a').attr('download', fileName).html(`<i class='download icon'></i>下載 ${fileName}`)
reader.onload = async(e) => {
let buffer = e.target.result
varray = new Uint8Array(buffer)
$('#download,#error').attr('style', 'display:none')
//預覽
$('#preview pre').text((new TextDecoder(getEncode())).decode(varray.slice(0, 96)))
$('#preview').removeAttr('style')
//轉換囉
$('[data-start]').click(function() {
$('#preview').attr('style', 'display:none')
$('#loader').addClass("active indeterminate")
let text = (new TextDecoder(getEncode())).decode(varray)
convert(text)
})
}
reader.readAsArrayBuffer(file);
}
}
function getEncode() {
let node = document.getElementById('encode');
return node.options[node.selectedIndex].value;
}
function getByteLength(s) {
return (new TextEncoder()).encode(s).length
}
async function splitText(t, maxTextLength, convert = x => x) {
const keywords = ['\n', ',', '。',';',':','、','?', ',', '.', ' '];
// Split into tiny chunks
let splitted = [t];
while(splitted.some(x => x.length > maxTextLength)) {
for(let i = 0; i < splitted.length; i++) {
if(splitted[i].length > maxTextLength) {
for(const keyword of keywords){
if (
splitted[i].includes(keyword) &&
splitted[i].split(keyword).filter(x => x).length > 1
){
splitted[i] = splitted[i].split(keyword);
for(let j = 0; j < splitted[i].length - 1; j++){
splitted[i][j] += keyword;
}
break;
}
}
}
}
splitted = splitted.flat();
}
// Merge to maximum chunks
for(let i = 0; i+1 < splitted.length; i++) {
if (splitted[i].length + splitted[i+1].length <= maxTextLength) {
splitted[i] += splitted[i+1];
splitted.splice(i+1, 1);
i--;
}
}
// Prevent 429 Error
let result = "";
for(const s of splitted) {
result += await convert(s);
}
return result;
}
async function convert(t) {
try {
let isIOS = navigator.userAgent.toLowerCase().match(/(iPad|iPhone|iPod)/i);
let maxTextLength = parseInt(((await axios.get('https://zhc.rextw.com/service-info')).data.data.maxPostBodyBytes)/8)
let _convert = async(x) => (await axios({
method: 'post',
url: 'https://zhc.rextw.com/convert',
data: {
converter: 'Taiwan',
text: x
}
})).data.data.text
let result = await splitText(t, maxTextLength, _convert)
let data = new Blob([result], { type: isIOS ? 'application/octet-stream' :'text/plain;charset=utf-8;' });
let url = URL.createObjectURL(data);
$('#download').removeAttr('style')
$('#download a').attr('href', url)
} catch (error) {
$('#error').removeAttr('style')
$('#error p').text(error)
}
$('#loader').removeClass("active")
}