index.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Tauri</title>
  8. </head>
  9. <body>
  10. <h1>Tauri Commands</h1>
  11. <div id="response">Response:</div>
  12. <div id="container"></div>
  13. <script>
  14. const responseDiv = document.querySelector('#response')
  15. function runCommand(commandName, args) {
  16. window.__TAURI__.invoke(commandName, args).then(response => {
  17. responseDiv.innerHTML = `Response: Ok(${response})`
  18. }).catch(error => {
  19. responseDiv.innerHTML = `Response: Err(${error})`
  20. })
  21. }
  22. const container = document.querySelector('#container')
  23. const commands = [
  24. { name: 'simple_command', required: true },
  25. { name: 'stateful_command', required: false },
  26. { name: 'async_simple_command', required: true },
  27. { name: 'async_stateful_command', required: false },
  28. { name: 'simple_command_with_result', required: true },
  29. { name: 'stateful_command_with_result', required: false },
  30. { name: 'async_simple_command_with_result', required: true },
  31. { name: 'async_stateful_command_with_result', required: false },
  32. ]
  33. for (command of commands) {
  34. const { name, required } = command
  35. const button = document.createElement('button')
  36. button.innerHTML = `Run ${name}`;
  37. button.addEventListener("click", function () {
  38. runCommand(name, { argument: 'value' })
  39. if (!required) {
  40. setTimeout(() => {
  41. runCommand(name, {})
  42. }, 1000)
  43. }
  44. });
  45. container.appendChild(button);
  46. }
  47. </script>
  48. </body>
  49. </html>