index.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Tauri</title>
  7. </head>
  8. <body>
  9. <h3>Counter</h3>
  10. <div>
  11. <button id="increment-btn">Increment counter</button>
  12. </div>
  13. <h3>Database</h3>
  14. <div>
  15. <input id="store-input" placeholder="The value to store" />
  16. <button id="store-btn">Store</button>
  17. </div>
  18. <div>
  19. <button id="read-btn">Read</button>
  20. </div>
  21. <div id="response"></div>
  22. <script>
  23. const KEY = 'db-key'
  24. const storeBtn = document.querySelector('#store-btn')
  25. const readBtn = document.querySelector('#read-btn')
  26. const incrementBtn = document.querySelector('#increment-btn')
  27. const storeInput = document.querySelector('#store-input')
  28. const responseContainer = document.querySelector('#response')
  29. function updateResponse(response) {
  30. responseContainer.innerText =
  31. typeof response === 'string' ? response : JSON.stringify(response)
  32. }
  33. incrementBtn.addEventListener('click', () => {
  34. window.__TAURI__
  35. .invoke('increment_counter')
  36. .then(updateResponse)
  37. .catch(updateResponse)
  38. })
  39. storeBtn.addEventListener('click', () => {
  40. window.__TAURI__
  41. .invoke('db_insert', {
  42. key: KEY,
  43. value: storeInput.value
  44. })
  45. .then(updateResponse)
  46. .catch(updateResponse)
  47. })
  48. readBtn.addEventListener('click', () => {
  49. window.__TAURI__
  50. .invoke('db_read', {
  51. key: KEY
  52. })
  53. .then(updateResponse)
  54. .catch(updateResponse)
  55. })
  56. </script>
  57. </body>
  58. </html>