index.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. <h3>Counter</h3>
  11. <div>
  12. <button id="increment-btn">Increment counter</button>
  13. </div>
  14. <h3>Database</h3>
  15. <div>
  16. <input id="store-input" placeholder="The value to store" />
  17. <button id="store-btn">Store</button>
  18. </div>
  19. <div>
  20. <button id="read-btn">Read</button>
  21. </div>
  22. <div id="response"></div>
  23. <script>
  24. const KEY = 'db-key'
  25. const storeBtn = document.querySelector('#store-btn')
  26. const readBtn = document.querySelector('#read-btn')
  27. const incrementBtn = document.querySelector('#increment-btn')
  28. const storeInput = document.querySelector('#store-input')
  29. const responseContainer = document.querySelector('#response')
  30. function updateResponse(response) {
  31. responseContainer.innerText =
  32. typeof response === 'string' ? response : JSON.stringify(response)
  33. }
  34. incrementBtn.addEventListener('click', () => {
  35. window.__TAURI__
  36. .invoke('increment_counter')
  37. .then(updateResponse)
  38. .catch(updateResponse)
  39. })
  40. storeBtn.addEventListener('click', () => {
  41. window.__TAURI__
  42. .invoke('db_insert', {
  43. key: KEY,
  44. value: storeInput.value
  45. })
  46. .then(updateResponse)
  47. .catch(updateResponse)
  48. })
  49. readBtn.addEventListener('click', () => {
  50. window.__TAURI__
  51. .invoke('db_read', {
  52. key: KEY
  53. })
  54. .then(updateResponse)
  55. .catch(updateResponse)
  56. })
  57. </script>
  58. </body>
  59. </html>