-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
123 lines (99 loc) · 2.91 KB
/
index.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<script src="lzma.js"></script>
<script src="./jquery.min.js"></script>
<style>
body {
background-color: #000;
}
</style>
<body>
<button class="str">转码</button>
<button class="byte">解码</button>
<textarea class="content" style="width: 500px;height: 300px"></textarea>
</body>
<script>
var str = '{ "tables": [ { "id": "2735b77052d64a5ca4498324b963eb39", "parentId": "ee61a8ad2cde4b4dac7b8c61979a41cd", "name": "g", "status": 3, "open": true, "attr": "menuC", "drop": false, "show": false, "click": true, "existsTab": 1, "hasDefault": false, "children": [] } ]}';
$('.content').text(str);
var my_lzma = new LZMA('./lzma_worker.js');
function convert_formated_hex_to_bytes(hex_str) {
var count = 0,
hex_arr,
hex_data = [],
hex_len,
i;
if (hex_str.trim() == '') return [];
/// Check for invalid hex characters.
if (/[^0-9a-fA-F\s]/.test(hex_str)) {
return false;
}
hex_arr = hex_str.split(/([0-9a-fA-F]+)/g);
hex_len = hex_arr.length;
for (i = 0; i < hex_len; ++i) {
if (hex_arr[i].trim() == '') {
continue;
}
hex_data[count++] = parseInt(hex_arr[i], 16);
}
return hex_data;
}
function convert_to_formated_hex(byte_arr) {
var hex_str = '',
i,
len,
tmp_hex;
function is_array(input) {
return typeof(input) === 'object' && (input instanceof Array);
}
if (!is_array(byte_arr)) {
return false;
}
len = byte_arr.length;
for (i = 0; i < len; ++i) {
if (byte_arr[i] < 0) {
byte_arr[i] = byte_arr[i] + 256;
}
if (byte_arr[i] === undefined) {
alert('Boom ' + i);
byte_arr[i] = 0;
}
tmp_hex = byte_arr[i].toString(16);
// Add leading zero.
if (tmp_hex.length == 1) tmp_hex = '0' + tmp_hex;
if ((i + 1) % 16 === 0) {
tmp_hex += '\n';
} else {
tmp_hex += ' ';
}
hex_str += tmp_hex;
}
return hex_str.trim();
}
var my_lzma = new LZMA('./lzma_worker.js');
var toByte, toStr;
$('.str').on('click', function () {
my_lzma.compress(str, 1, function (result) {
if (result === false) {
alert('An error occurred during LZMA compression.');
return;
}
toByte = convert_to_formated_hex(result);
$('.content').text(toByte);
}, function (percent) {});
});
$('.byte').on('click', function () {
var byte_arr = convert_formated_hex_to_bytes(toByte);
if (byte_arr == false) {
alert('invalid compressed input');
return false;
}
my_lzma.decompress(byte_arr, function (result) {
if (result === false) {
alert('An error occurred during decompression.');
return;
}
toStr = result;
$('.content').text(toStr);
}, function (percent) {});
});
</script>