index.html 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Choose a File</title>
  5. </head>
  6. <body>
  7. <h2>I'm supposed to be an invisible iframe... (you shouldn't see this)</h2>
  8. <script src="https://www.dropbox.com/static/api/2/dropins.js"></script>
  9. <script src="https://js.live.net/v7.2/OneDrive.js"></script>
  10. <script src="tokens.js"></script>
  11. <script>
  12. var queries = Object.fromEntries(window.location.search.substring(1).split("&").map(i => i.split("=")).map(i => i.map(i => i && decodeURIComponent(i))));
  13. var fileTypes = queries.exts ? queries.exts.split(",") : [];
  14. function finish(message, name, data) {
  15. window.parent.postMessage({webretro: {timestamp: parseInt(queries.timestamp), message: message, name: name, data: data}}, "*");
  16. }
  17. function xhr(loc, success, error) {
  18. var xhr = new XMLHttpRequest();
  19. xhr.open("GET", loc, true);
  20. xhr.responseType = "arraybuffer";
  21. xhr.onload = function() {
  22. success(this.response);
  23. }
  24. xhr.onerror = function(e) {
  25. if (error) error(e);
  26. }
  27. xhr.send();
  28. }
  29. // Pass on data from Google Drive picker
  30. window.addEventListener("message", function(e) {
  31. if (e.origin == window.location.origin && e.data.webretro) finish(e.data.webretro.message, e.data.webretro.name, e.data.webretro.data);
  32. }, false);
  33. if (queries.type == "drive") {
  34. // Google Drive
  35. var dwidth = window.screen.width - 320;
  36. var dheight = window.screen.height - 240;
  37. var dleft = 160;
  38. var dtop = 120;
  39. window.open("drive.html?exts=" + fileTypes.join(","), "Choose a File", "left=" + dleft + ",top=" + dtop + ",width=" + dwidth + ",height=" + dheight);
  40. } else if (queries.type == "dropbox") {
  41. // Dropbox
  42. Dropbox.appKey = dropboxAppKey;
  43. Dropbox.choose({
  44. success: function(files) {
  45. var file = files[0];
  46. xhr(file.link, function(data) {
  47. finish("success", file.name, data);
  48. }, function() {
  49. finish("error");
  50. });
  51. },
  52. cancel: function() {
  53. finish("cancelled");
  54. },
  55. linkType: "direct",
  56. multiselect: false,
  57. folderselect: false
  58. });
  59. } else if (queries.type == "onedrive") {
  60. // OneDrive
  61. OneDrive.open({
  62. clientId: onedriveClientId,
  63. action: "download",
  64. multiSelect: false,
  65. advanced: {
  66. filter: fileTypes.join(",")
  67. },
  68. success: function(response) {
  69. var name = response.value[0].name;
  70. var link = response.value[0]["@microsoft.graph.downloadUrl"];
  71. xhr(link, function(data) {
  72. finish("success", name, data);
  73. }, function() {
  74. finish("error");
  75. });
  76. },
  77. cancel: function() {
  78. finish("cancelled");
  79. },
  80. error: function(error) {
  81. finish("error");
  82. }
  83. });
  84. }
  85. </script>
  86. </body>
  87. </html>