marlin.js 684 B

123456789101112131415161718192021222324
  1. document.addEventListener('DOMContentLoaded', () => {
  2. const ws = new WebSocket(`ws://${location.host}/ws`);
  3. ws.onmessage = (e) => {
  4. if (typeof e.data === 'string') {
  5. let node = document.createElement('li');
  6. let text = document.createTextNode(e.data);
  7. node.appendChild(text);
  8. document.getElementById('serial-output').appendChild(node);
  9. }
  10. };
  11. document.getElementById('serial-command-form').addEventListener('submit', (e) => {
  12. e.preventDefault();
  13. let value = document.getElementById('serial-command').value.trim();
  14. if (!value) return;
  15. ws.send(`${value}\n`);
  16. document.getElementById('serial-command').value = '';
  17. });
  18. });