- Onto the HTML and JS, the most terrifying aspect here is the fact that user-inputted JS can run in the browser. In security consideration terms, this could open a door to XSS attacks. Fortunately this project is raw HTML with no external dependencies and run client-side, so any modern web browser should give protections by default. However sanitizing any given HTML would be an excellent practice. I'd also take a look at the other "best practices."
- It's unclear which JS version you're targeting, but assuming it's in the ballpark of
ES2019
you should be using let
over var
so your variables are scoped to enclosed blocks.
- Your
preview
function is effectively acting like a main
function. Create a proper main
function that registers each listener so your element selector variables can be scoped properly. This again ties into security.
- I'd swap
function() {
iterations with () => {
just because it's a bit cleaner.
- Your
resetter
event listener (the only significant logic here) can be greatly simplified by checking if the editor value is default first, then placing the first block in an else-if with confirm
as the condition.
- Separate your JS into a file and link it like so:
<script type="text/javascript" src="editor.js"></script>
document.getElementById('resetter').addEventListener('click', () => {
fileChooser.value = '';
downloader.download = 'template.html';
if (editor.value != editor.defaultValue && confirm('Your input will be lost.\nAre you sure you want to reset?')) {
editor.value = editor.defaultValue;
preview();
}
});
- Separate your JS into a file and link it like so:
<script type="text/javascript" src="editor.js"></script>