Explorar o código

Improve error message when `window.opener == null` (#5340)

Refs: https://github.com/transloadit/uppy/issues/5334#issuecomment-2228359806
Mikael Finstad hai 9 meses
pai
achega
efdc4d192b

+ 25 - 3
packages/@uppy/companion/src/server/controllers/send-token.js

@@ -14,11 +14,33 @@ const htmlContent = (token, origin) => {
     <head>
         <meta charset="utf-8" />
         <script>
-          window.opener.postMessage(${serialize({ token })}, ${serialize(origin)})
-          window.close()
+          (function() {
+            'use strict';
+
+            var data = ${serialize({ token })};
+            var origin = ${serialize(origin)};
+
+            if (window.opener != null) {
+              window.opener.postMessage(data, origin);
+              window.close();
+            } else {
+              // maybe this will work? (note that it's not possible to try/catch this to see whether it worked)
+              window.postMessage(data, origin);
+
+              console.warn('Unable to send the authentication token to the web app. This probably means that the web app was served from a HTTP server that includes the \`Cross-Origin-Opener-Policy: same-origin\` header. Make sure that the Uppy app is served from a server that does not send this header, or set to \`same-origin-allow-popups\`.');
+
+              addEventListener("DOMContentLoaded", function() {
+                document.body.appendChild(document.createTextNode('Something went wrong. Please contact the site administrator. You may now exit this page.'));
+              });
+            }
+          })();
         </script>
     </head>
-    <body></body>
+    <body>
+    <noscript>
+      JavaScript must be enabled for this to work.
+    </noscript>
+    </body>
     </html>`
 }
 

+ 2 - 13
packages/@uppy/companion/test/__tests__/callback.js

@@ -46,19 +46,8 @@ describe('test authentication callback', () => {
       .get(`/dropbox/send-token?uppyAuthToken=${token}`)
       .expect(200)
       .expect((res) => {
-        const body = `
-    <!DOCTYPE html>
-    <html>
-    <head>
-        <meta charset="utf-8" />
-        <script>
-          window.opener.postMessage({"token":"${token}"}, "http:\\u002F\\u002Flocalhost:3020")
-          window.close()
-        </script>
-    </head>
-    <body></body>
-    </html>`
-        expect(res.text).toBe(body)
+        expect(res.text).toMatch(`var data = {"token":"${token}"};`)
+        expect(res.text).toMatch(`var origin = "http:\\u002F\\u002Flocalhost:3020";`)
       })
   })
 })