-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
315 lines (269 loc) · 9.16 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>JSONify - Convert Javascript Objects to JSON</title>
<link rel="shortcut icon" type="image/png" href="favicon.png"/>
<style>
html,
body {
margin: 0;
padding: 0;
height: 100%;
}
*,
body,
button,
html,
input,
select,
textarea {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
background: #20262e;
padding: 0;
margin: 0;
position: relative;
font-size: 14px;
color: #39464e;
}
header {
height: 60px;
background: #1c2128;
box-shadow: 0 0 5px rgba(28, 33, 40, 0);
position: relative;
z-index: 100;
width: 100vw;
}
header {
display: flex;
align-items: center;
}
header .title {
color: #0084ff;
padding: 0em 1em;
height: 100%;
display: flex;
/* align-content: center; */
align-items: center;
}
nav div {
float: left;
}
nav a:hover {
color: #0088ff;
}
nav a {
display: block;
outline: none;
text-decoration: none;
padding: 8px;
/* line-height: 60px; */
color: #fbfbfb;
background: rgba(14, 123, 235, 0.2);
}
nav svg {
vertical-align: middle;
position: relative;
top: -1px;
height: 16px;
}
body {
display: flex;
flex-direction: column;
}
.main {
display: flex;
flex-grow: 1;
flex-direction: column;
}
.main>div {
position: relative;
flex-grow: 1;
border: 1px solid black;
display: flex;
align-items: stretch;
flex-direction: column;
}
textarea {
font-family: Menlo, Consolas, 'Lucida Console', monospace;
background: transparent;
color: white;
resize: none;
height: 100%;
outline: none;
}
textarea:focus {
border-color: #0088ff;
}
.floating-tools {
position: absolute;
top: 5px;
right: 5px;
}
.floating-tools button {
color: #cfd0d2;
font-style: normal;
font-size: .8em;
/* height: 16px; */
display: inline-block;
padding: 0 6px;
line-height: 16px;
border-radius: 3px;
background: hsla(0, 0%, 100%, .1);
}
.floating-tools button:hover {
background: hsla(0, 0%, 100%, .15);
color: white;
}
</style>
<script type="text/javascript">
var $ = document.querySelector.bind(document);
function convert(str) {
var tempObj;
var outputText;
try {
var tempObj = eval('([' + str + '])')[0]; // TODO: make this less scary
outputText = JSON.stringify(tempObj, modifier, ' '); // TODO: allow spaces, tabs etc...
} catch (err) {
// console.error(err);
outputText = err;
}
setGlobals(tempObj, outputText);
console.log(tempObj);
return outputText;
}
function modifier(key, value) {
return value;
}
// set object to global so we can play around with it on the console
function setGlobals(obj, str) {
window.obj = obj;
window.str = str;
}
function download(text, name, type) {
var a = document.createElement("a");
try {
var file = new Blob([text], { type: type });
a.href = URL.createObjectURL(file);
a.download = name;
a.click();
} catch (err) {
alert('Not supported!');
}
}
function currentDate() {
function pad(n) {
return 10 > n ? "0" + n : n;
}
var d = new Date();
return d.getFullYear() +
'-' + pad(d.getMonth() + 1) +
'-' + pad(d.getDate()) +
'_' + pad(d.getHours()) +
'-' + pad(d.getMinutes()) +
'-' + pad(d.getSeconds());
}
window.onload = function () {
var inputTextarea = $('#inputTextarea');
var clearInputBtn = $('#inputDiv .clear');
var selectInputBtn = $('#inputDiv .select');
var outputTextarea = $('#outputTextarea');
var clearOutputBtn = $('#outputDiv .clear');
var selectOutputBtn = $('#outputDiv .select');
var saveOutputBtn = $('#outputDiv .save');
var convertBtn = $('#convertBtn');
convertBtn.onclick = function (e) {
e.preventDefault();
var inputText = inputTextarea.value || inputTextarea.placeholder;
var outputText = convert(inputText);
outputTextarea.value = outputText;
outputTextarea.select();
};
clearInputBtn.onclick = function () {
inputTextarea.value = '';
inputTextarea.focus();
delete localStorage.draft;
console.clear();
};
selectInputBtn.onclick = function () {
inputTextarea.focus();
inputTextarea.select();
document.execCommand('copy');
};
clearOutputBtn.onclick = function () {
outputTextarea.value = '';
outputTextarea.focus();
};
selectOutputBtn.onclick = function () {
outputTextarea.focus();
outputTextarea.select();
document.execCommand('copy');
};
saveOutputBtn.onclick = function () {
var defaultName = 'jsonify ' + currentDate();
var name = prompt('File name', defaultName);
if (name) {
var text = outputTextarea.value;
download(text, name + '.json', 'application/json');
}
};
// save input as you type
var debounceTimer;
inputTextarea.onkeyup = function () {
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = setTimeout(function () {
localStorage.draft = inputTextarea.value;
debounceTimer = undefined;
}, 600);
};
console.log('You can access the parsed object using the global variable `obj` and the string `str`. Have fun!');
// restore previous input
if (localStorage.draft) inputTextarea.value = localStorage.draft;
var sample = "/* javascript object */\n{\n foo: 'bar', // single quotes\n hello: 'world', // trailing comma\n}";
inputTextarea.placeholder = sample;
outputTextarea.placeholder = convert(sample);
};
</script>
</head>
<body>
<header>
<h4 class="title">
Javascript Object to JSON
</h4>
<nav>
<div>
<a id="convertBtn" href="#">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"
stroke-linejoin="round" class="feather feather-play">
<polygon points="5 3 19 12 5 21 5 3"></polygon>
</svg>
Convert
</a>
</div>
</nav>
</header>
<div class="main">
<div id="inputDiv">
<div class="floating-tools">
<button class="clear" title="Clear input">Clear</button>
<button class="select" style="display:none;">Copy</button>
</div>
<textarea id="inputTextarea" rows="40" autofocus></textarea>
</div>
<div id="outputDiv">
<div class="floating-tools">
<button class="clear" style="display:none;">Clear</button>
<button class="select" title="Copy content to clipboard">Copy</button>
<button class="save" title="Save content to a file">Save</button>
</div>
<textarea id="outputTextarea" rows="40"></textarea>
</div>
</div>
</body>
</html>