-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
68 lines (63 loc) · 2.71 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.3/gh-fork-ribbon.min.css" />
<title>Online Pickle to JSON Convertor</title>
</head>
<body>
<section>
<a class="github-fork-ribbon" href="https://github.com/ewfian/pickleparser" data-ribbon="Fork me on GitHub" title="Fork me on GitHub">Fork me on GitHub</a>
<h1>Pickle Parser Demo</h1>
<div>
<span>Select a pickle file: </span>
<input type="file" id="file_selector">
</div>
<hr />
<div>Tips: The converted object will be mounted as `window.obj`, which can be inspected using developer tools.</div>
<pre id="json_result_previewer"><pre>
</section>
<script src="https://unpkg.com/pickleparser/dist/index.js"></script>
<script>
const fileSelector = document.getElementById('file_selector');
const jsonResultPreviewer = document.getElementById('json_result_previewer');
fileSelector.addEventListener('change', function (e) {
const file = fileSelector.files[0];
const reader = new FileReader();
reader.onload = function (event) {
try {
const buffer = new Uint8Array(event.target.result);
const parser = new pickleparser.Parser({
unpicklingTypeOfSet: 'Set',
unpicklingTypeOfDictionary: 'Map',
});
const obj = parser.parse(buffer);
window.obj = obj;
const replacer = (_, value) => {
if (value instanceof Map) {
return Object.fromEntries(value);
}
if (value instanceof Set) {
return Array.from(value);
}
if (typeof value === 'bigint') {
return value.toString();
}
if (value?.constructor?.name === 'PFunction') {
return value.args;
}
return value;
};
const json = JSON.stringify(obj, replacer, 4);
jsonResultPreviewer.innerText = json;
} catch (error) {
jsonResultPreviewer.innerText = error;
}
}
reader.readAsArrayBuffer(file);
});
</script>
</body>
</html>