Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when window.opener == null #5340

Merged
merged 7 commits into from
Jul 15, 2024
Merged

Conversation

mifi
Copy link
Contributor

@mifi mifi commented Jul 15, 2024

when window.opener == null
#5334 (comment)
Copy link
Contributor

github-actions bot commented Jul 15, 2024

Diff output files
diff --git a/packages/@uppy/companion/lib/server/controllers/send-token.js b/packages/@uppy/companion/lib/server/controllers/send-token.js
index 1f0738e..5769dc2 100644
--- a/packages/@uppy/companion/lib/server/controllers/send-token.js
+++ b/packages/@uppy/companion/lib/server/controllers/send-token.js
@@ -13,11 +13,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>`;
 };
 /**

window.close()
} else {
addEventListener("DOMContentLoaded", () => {
document.body.append(document.createTextNode('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\`.'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
document.body.append(document.createTextNode('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\`.'))
document.body.append('Unable to send the authentication token to the web app. Please contact your website administrator and/or try again later.');
console.warn('Failed to postMessage. 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", () => {
document.body.append(document.createTextNode('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\`.'))
});
}
</script>
</head>
<body></body>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<body></body>
<body><noscript>JavaScript is required for this app to work correctly.</noscript></body>

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", () => {
document.body.append(document.createTextNode('Something went wrong. Please contact the site administrator. You may now exit this page.'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
document.body.append(document.createTextNode('Something went wrong. Please contact the site administrator. You may now exit this page.'))
document.body.append('Something went wrong. Please contact the site administrator. You may now exit this page.')

Comment on lines 17 to 18
var data = ${serialize({ token })};
var origin = ${serialize(origin)};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var data = ${serialize({ token })};
var origin = ${serialize(origin)};
(function (data, origin) {
'use strict';

addEventListener("DOMContentLoaded", () => {
document.body.append(document.createTextNode('Something went wrong. Please contact the site administrator. You may now exit this page.'))
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
})(${serialize({ token })}, ${serialize(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", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
addEventListener("DOMContentLoaded", () => {
addEventListener("DOMContentLoaded", function () {

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", () => {
document.body.append(document.createTextNode('Something went wrong. Please contact the site administrator. You may now exit this page.'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
document.body.append(document.createTextNode('Something went wrong. Please contact the site administrator. You may now exit this page.'))
document.body.appendChild(document.createTextNode('Something went wrong. Please contact the site administrator. You may now exit this page.'))

Comment on lines 17 to 19
'use strict';

function sendToken() {
Copy link
Contributor

@aduh95 aduh95 Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'use strict';
function sendToken() {
(function (origin, origin) {
'use strict';

Comment on lines +20 to +22
var data = ${serialize({ token })};
var origin = ${serialize(origin)};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var data = ${serialize({ token })};
var origin = ${serialize(origin)};

Comment on lines 37 to 38

sendToken();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sendToken();

@aduh95 aduh95 merged commit efdc4d1 into main Jul 15, 2024
20 checks passed
@aduh95 aduh95 deleted the send-token-error branch July 15, 2024 14:23
github-actions bot added a commit that referenced this pull request Jul 15, 2024
| Package          | Version | Package          | Version |
| ---------------- | ------- | ---------------- | ------- |
| @uppy/companion  |   5.0.3 | @uppy/xhr-upload |   4.0.1 |
| @uppy/locales    |   4.0.2 | uppy             |   4.0.3 |

- @uppy/companion: Improve error message when `window.opener == null` (Mikael Finstad / #5340)
- @uppy/xhr-upload: correctly type xhrUpload meta (Merlijn Vos / #5344)
- meta: fix require path in `upload-to-cdn` (Antoine du Hamel / #5343)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants