Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. <script>
  2. function ping() {
  3. const ping_url = '/ping';
  4.  
  5. hostname = document.getElementById('textarea').value;
  6. // add fancy loading gif
  7. input = document.getElementById('textarea_div');
  8.  
  9. input.className += ' is-loading';
  10.  
  11. // send the command to the backend
  12. data = {
  13. 'host': hostname
  14. };
  15. fetch(ping_url, {
  16. method: 'POST',
  17. headers: {
  18. 'Content-Type': 'application/json'
  19. },
  20. body: JSON.stringify(data)
  21. })
  22. .then((response) => {
  23. return response.json();
  24. })
  25. .then(function (data) {
  26. command = document.getElementById('command');
  27. return_code = document.getElementById('status');
  28.  
  29. if (data.status != 'ok') {
  30. if ('command' in data) {
  31. command.innerText = data.command;
  32. } else {
  33. command.innerText = 'No command executed.';
  34. }
  35. return_code.innerText = 'Something went wrong:' + data.status;
  36. return
  37. }
  38. command.innerText = data.command;
  39.  
  40. return_code.innerText = 'Return code: ' + data.return_code;
  41. }).finally(function () {
  42. input.className = 'control';
  43. hljs.highlightBlock(command);
  44. hljs.highlightBlock(return_code);
  45. })
  46. }
  47. window.onload = function () {
  48. return_code = document.getElementById('status');
  49. command = document.getElementById('command');
  50. hljs.highlightBlock(return_code);
  51. hljs.highlightBlock(command);
  52. }
  53. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement