index.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <script>
  5. function callBackEnd(route, args) {
  6. console.log(route)
  7. console.log(args)
  8. var xhr = new XMLHttpRequest()
  9. xhr.onload = function () {
  10. console.log(route, args, 'done', xhr.status)
  11. }
  12. xhr.onerror = function () {
  13. console.log(route, args, 'error with ' + xhr.status)
  14. }
  15. xhr.open('POST', 'http://localhost:7000/' + route)
  16. xhr.setRequestHeader('Content-type', 'application/json')
  17. xhr.send(JSON.stringify(args))
  18. }
  19. function reply(args) {
  20. return callBackEnd('reply', args)
  21. }
  22. function sendError(error) {
  23. return callBackEnd('error', error)
  24. }
  25. function testFs(dir) {
  26. var contents = 'TAURI E2E TEST FILE'
  27. var commandSuffix = (dir ? 'WithDir' : '')
  28. var options = {
  29. dir: dir || null
  30. }
  31. return window.__TAURI__.fs.writeFile({
  32. file: 'tauri-test.txt',
  33. contents: contents
  34. }, options).then(function (res) {
  35. reply({
  36. cmd: 'writeFile' + commandSuffix
  37. })
  38. return window.__TAURI__.fs.readTextFile('tauri-test.txt', options).then(function (res) {
  39. if (res === contents) {
  40. reply({
  41. cmd: 'readFile' + commandSuffix
  42. })
  43. return window.__TAURI__.fs.readDir('.', options).then(res => {
  44. reply({
  45. cmd: 'readDir' + commandSuffix
  46. })
  47. return window.__TAURI__.fs.copyFile('tauri-test.txt', 'tauri-test-copy.txt', options)
  48. .then(function (res) {
  49. reply({
  50. cmd: 'copyFile' + commandSuffix
  51. })
  52. return window.__TAURI__.fs.createDir('tauri-test-dir', options).then(function (res) {
  53. reply({
  54. cmd: 'createDir' + commandSuffix
  55. })
  56. return window.__TAURI__.fs.removeDir('tauri-test-dir', options).then(function (res) {
  57. reply({
  58. cmd: 'removeDir' + commandSuffix
  59. })
  60. })
  61. }).then(function (res) {
  62. return window.__TAURI__.fs.renameFile('tauri-test.txt', 'tauri.testt.txt', options)
  63. .then(function (res) {
  64. reply({
  65. cmd: 'renameFile' + commandSuffix
  66. })
  67. return Promise.all([
  68. window.__TAURI__.fs.removeFile('tauri.testt.txt', options),
  69. window.__TAURI__.fs.removeFile('tauri-test-copy.txt', options)
  70. ]).then(function (res) {
  71. reply({
  72. cmd: 'removeFile' + commandSuffix
  73. })
  74. })
  75. })
  76. })
  77. })
  78. })
  79. } else {
  80. sendError('expected "' + contents + '" found "' + res + '"')
  81. }
  82. })
  83. }).catch(sendError)
  84. }
  85. window.__TAURI__.event.listen('reply', function (res) {
  86. reply({
  87. cmd: 'listen'
  88. })
  89. })
  90. window.onTauriInit = function () {
  91. window.__TAURI__.event.emit('hello')
  92. }
  93. testFs(null).then(function () {
  94. testFs(window.__TAURI__.fs.Dir.Config)
  95. })
  96. setTimeout(function () {
  97. window.__TAURI__.invoke({
  98. cmd: 'exit'
  99. })
  100. }, 15000)
  101. </script>
  102. </body>
  103. </html>