isolatedpicker.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. var receiverFrame = document.createElement("iframe");
  28. receiverFrame.style.display = "none";
  29. receiverFrame.crossorigin = "anonymous"; // soon...
  30. receiverFrame.src = queries.returnurl;
  31. var receiverLoaded = false;
  32. var frameTimer = setTimeout(function() {
  33. alert("The file receiver is taking an unusually long time to respond. This could be the result of Content-Security-Policy: frame-ancestors or X-Frame-Options blocking it. The frame is now shown:");
  34. receiverFrame.style.display = "block";
  35. }, 5000);
  36. receiverFrame.onload = function() {
  37. receiverLoaded = true;
  38. clearTimeout(frameTimer);
  39. }
  40. document.body.appendChild(receiverFrame);
  41. function finish(message, name, data) {
  42. function waitForReceiverLoad() {
  43. if (receiverLoaded) {
  44. receiverFrame.contentWindow.postMessage({webretro: {timestamp: parseInt(queries.timestamp), message: message, name: name, data: data}}, "*");
  45. } else {
  46. setTimeout(waitForReceiverLoad, 500);
  47. }
  48. }
  49. waitForReceiverLoad();
  50. }
  51. window.addEventListener("unload", function() {
  52. if (queries.type) finish("cancelled");
  53. }, false);
  54. window.addEventListener("message", function(e) {
  55. if (e.data == "acknowledged") window.close(); // with coop, this window has "ownership" over itself
  56. }, false);
  57. function xhr(loc, success, error) {
  58. var xhr = new XMLHttpRequest();
  59. xhr.open("GET", loc, true);
  60. xhr.responseType = "arraybuffer";
  61. xhr.onload = function() {
  62. success(this.response);
  63. }
  64. xhr.onerror = function(e) {
  65. if (error) error(e);
  66. }
  67. xhr.send();
  68. }
  69. // Pass on data from Google Drive picker
  70. window.addEventListener("message", function(e) {
  71. if (e.origin == window.location.origin && e.data.webretro) finish(e.data.webretro.message, e.data.webretro.name, e.data.webretro.data);
  72. }, false);
  73. if (queries.type == "drive") {
  74. // Google Drive
  75. var dwidth = window.screen.width - 320;
  76. var dheight = window.screen.height - 240;
  77. var dleft = 160;
  78. var dtop = 120;
  79. window.open("drive.html?exts=" + fileTypes.join(","), "Choose a File", "left=" + dleft + ",top=" + dtop + ",width=" + dwidth + ",height=" + dheight);
  80. } else if (queries.type == "dropbox") {
  81. // Dropbox
  82. Dropbox.appKey = dropboxAppKey;
  83. Dropbox.choose({
  84. success: function(files) {
  85. var file = files[0];
  86. xhr(file.link, function(data) {
  87. finish("success", file.name, data);
  88. }, function() {
  89. finish("error");
  90. });
  91. },
  92. cancel: function() {
  93. finish("cancelled");
  94. },
  95. linkType: "direct",
  96. multiselect: false,
  97. folderselect: false
  98. });
  99. } else if (queries.type == "onedrive") {
  100. // OneDrive
  101. OneDrive.open({
  102. clientId: onedriveClientId,
  103. action: "download",
  104. multiSelect: false,
  105. advanced: {
  106. filter: fileTypes.join(",")
  107. },
  108. success: function(response) {
  109. var name = response.value[0].name;
  110. var link = response.value[0]["@microsoft.graph.downloadUrl"];
  111. xhr(link, function(data) {
  112. finish("success", name, data);
  113. }, function() {
  114. finish("error");
  115. });
  116. },
  117. cancel: function() {
  118. finish("cancelled");
  119. },
  120. error: function(error) {
  121. finish("error");
  122. }
  123. });
  124. }
  125. </script>
  126. </body>
  127. </html>