/
index.js
43 lines (35 loc) · 1.18 KB
/
index.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
const { parse, AST } = require('json-ast');
const $paste = document.getElementById('paste');
const $result = document.getElementById('json');
const $instructions = document.getElementById('instructions');
const $error = document.getElementById('error');
const ERROR_SYMBOL = Symbol();
function formatJSON(json_string) {
try {
const ast = parse(json_string, { junker: true });
const parsed = AST.JsonNode.toJSON(ast);
return JSON.stringify(parsed, null, 2);
} catch (e) {
return ERROR_SYMBOL;
}
}
$paste.onpaste = (event) => {
let raw_paste;
if (window.clipboardData && window.clipboardData.getData) {
raw_paste = window.clipboardData.getData('Text');
} else if (event.clipboardData && event.clipboardData.getData) {
raw_paste = event.clipboardData.getData('text/plain');
}
const result = formatJSON(raw_paste);
if (result === ERROR_SYMBOL) {
$result.style.display = 'none';
$instructions.style.display = 'none';
$error.style.display = 'block';
} else {
$result.style.display = 'block';
$result.innerHTML = result;
$instructions.style.display = 'none';
$error.style.display = 'none';
}
return false; // prevent default
};