demo.html 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Choose a File</title>
  5. <style>
  6. body {
  7. background-color: #101010;
  8. color: white;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <input type="button" onclick="uploadWebFile('drive')" value="Google Drive">
  14. <input type="button" onclick="uploadWebFile('dropbox')" value="Dropbox">
  15. <input type="button" onclick="uploadWebFile('onedrive')" value="OneDrive">
  16. <pre id="output"></pre>
  17. <script src="uauth.js"></script>
  18. <script>
  19. /* How to use:
  20. *
  21. * uauth.open(type, fileTypes, callback)
  22. * type: either "drive", "dropbox", or "onedrive"
  23. * fileTypes: array of file extensions (including dot before) (leave empty to allow all) [ignored on dropbox :(]
  24. * callback: function to call when either the file is ready, or the user cancelled it
  25. *
  26. * this object is passed to the callback function:
  27. * message: either "success", "cancelled", or "error"
  28. * if success, name: name of the file
  29. * if success, data: file contents (arrayBuffer)
  30. */
  31. var fileTypes = [".smc", ".sfc", ".nes"];
  32. var output = document.getElementById("output");
  33. function getTruncatedData(buffer, length) {
  34. return JSON.stringify(Array.from(new Uint8Array(buffer))).slice(0, length) + "...";
  35. }
  36. function handleWebFile(data) {
  37. if (data.message == "success") {
  38. output.textContent = "name: " + data.name + "\ndata (Uint8Array): " + getTruncatedData(data.data, 200);
  39. } else if (data.message == "error") {
  40. alert("There was an error.");
  41. } else if (data.message == "cancelled") {
  42. console.log("Cancelled.");
  43. }
  44. }
  45. function uploadWebFile(type) {
  46. uauth.open(type, fileTypes, handleWebFile);
  47. }
  48. </script>
  49. </body>
  50. </html>