popuppicker.html 2.9 KB

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