From what I have read, having a client-side code read the message from the iframe outside of the sandboxed one seems impossible. I can only have it sent if to the iframe outside the sandboxed. What would be the solutions, then?
Here is what I have tried so far:
document.addEventListener("DOMContentLoaded", function() {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const signupToken = urlParams.get('token');
//const iframe = document.getElementById('myiframe'); //Most outter iframe not reachable
//const iframe = document.getElementById('sandboxed'); //Outter iframe not reachable
const iframe = document.getElementById('userHtmlFrame'); //This would be the ideal, but not reachable
if (iframe && signupToken) {
console.log("WebApp iframe found and here is the token " + signupToken)
iframe.contentWindow.postMessage({
signupToken: signupToken
}, "*");
}
});
Then, to read the message, I have:
let urlToken = '';
document.addEventListener("DOMContentLoaded", () => {
window.addEventListener('message', (event) => {
console.log("Received message:", event);
if (event.origin === "https://www.kshkjfhkajhfd.com") {
console.log("Correct origin");
if (event.data && event.data.signupToken) {
urlToken = event.data.signupToken;
console.log("Token received from parent:", urlToken);
} else {
console.log("Message does not contain expected data:", event.data);
}
} else {
console.error("Message from unknown or incorrect origin:", event.origin);
}
}, false);
});