JSON import snippet from somewhere

This commit is contained in:
Nox Sluijtman 2023-11-01 17:13:20 +01:00
parent 9d0f09df78
commit c41f60587b
2 changed files with 24 additions and 2 deletions

View file

@ -10,9 +10,13 @@
<main> <main>
<h1>Brown Wall</h1> <h1>Brown Wall</h1>
<h3>A wall which has been hit with a considerable amount of faeces.</h3> <h3>A wall which has been hit with a considerable amount of faeces.</h3>
<noscript>Enable JS to use this application.</noscript>
<div id="menu-container" class="menu-container"> <div id="menu-container" class="menu-container">
<noscript>Enable JS to use this application.</noscript>
</div> </div>
<input type="file" id="selectFiles" value="Import"/>
<button id="import">Import</button>
<textarea id="result" cols="30" rows="10"></textarea>
</main> </main>
</body> </body>
<script src="main.js"></script> <script src="main.js"></script>

18
main.js
View file

@ -21,3 +21,21 @@ menus.forEach( menu =>{
dropdownSelect.append(Object.assign(document.createElement('option'), {id:item.toLowerCase(), innerHTML:item})) dropdownSelect.append(Object.assign(document.createElement('option'), {id:item.toLowerCase(), innerHTML:item}))
}) })
}) })
document.getElementById('import').onclick = function() {
const files = document.getElementById('selectFiles').files;
if (files.length <= 0) {
return false;
}
var fr = new FileReader();
fr.onload = function(e) {
console.log(e);
var result = JSON.parse(e.target.result);
var formatted = JSON.stringify(result, null, 2);
document.getElementById('result').value = formatted;
}
fr.readAsText(files.item(0));
};