-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (79 loc) · 3.16 KB
/
index.js
File metadata and controls
87 lines (79 loc) · 3.16 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
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,100)))
$('#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, keywords = ['\n', ',', '。', ',', '.', ' '], convert = x => x) {
for (let keyword of keywords) {
let i = 0, arr = []
while(i < t.length) {
let index = t.lastIndexOf(keyword, i+maxTextLength-1)+1
if (i == index) {
arr.push(t.substring(i, Infinity))
break;
} else {
arr.push(t.substring(i, index))
i = index
}
}
if (!(arr.every(x => x.length <= maxTextLength))) continue;
let result = (await Promise.all(arr.map(convert))).join('')
return result
}
}
async function convert(t) {
try {
let isIOS = navigator.userAgent.toLowerCase().match(/(iPad|iPhone|iPod)/i);
let maxTextLength = parseInt(((await axios.get('https://fhj.sciuridae.me/service-info')).data.data.maxPostBodyBytes)/4)
let convert = async(x) => (await axios({
method: 'post',
url: 'https://fhj.sciuridae.me/convert',
data: {
converter: 'Taiwan',
text: x
}
})).data.data.text
let result = await splitText(t, maxTextLength, ['\n', ',', '。', ',', '.', ' '], convert)
let data = new Blob([result], { type: isIOS ? 'application/octet-strea' :'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")
}