I created an HTMLcomponent using the example code provided by Wix.
I can send a message from the page code to the HTMLcomponent and that works.
When the windows.parent.postMessage() within the HTMLcomponent executes, this does not fire the onMessage() event in the page code.
Otherwise, the HTMLcomponent code works and is able to connect to the 3rd party API and renders a PDF.
I have narrowed down the issue to the url string within the postMessage(). I tried both the https:// and http:// versions of my website url (both with and without the www.) but that did not work. The funny thing is when I use the “*” as the url, then it does work.
But according to Wix recommendations as well as the Monzilla docs page regarding postMessage(), they both say never use “*”, in order to avoid exposure of data to a malicious site.
My page contains 2 objects, a button (buttonHTML) and the HTMLcomponent (html1)
What am I missing?
page code
$w.onReady(function () {
});
export function html1_message(event) {
if (event.data) {
console.log("Request came from: "+ event.origin);
console.log(`Message received by page code: ${event.data}`);
}
}
export function buttonHTML_click(event) {
$w("#html1").postMessage("Message from page code");
}
HTMLcomponent code
<!doctype html>
<html>
<head>
<script type="text/javascript">
function init() {
// when a message is received from the page code
window.onmessage = (event) => {
if (event.data) {
console.log("HTML Code Element received a message!");
insertMessage(event.data);
}
}
}
// display received message
function insertMessage(msg) {
document.getElementById('demo').innerHTML = msg;
sendReturnMessage("Message from the HTML Component!");
}
// send message to the page code
function sendReturnMessage(msg) {
try {
console.log("We are going to try and run postMessage().");
window.parent.postMessage(msg, "https://mywebsite.org/");
console.log("We just completed running postMessage().");
} catch ( error ) {
console.log(error.message);
}
}
</script>
</head>
<body onload="init();" >
<h1>HTML Component Test</h1>
<h3>(PDF Viewer Test)</h3>
<p id="demo">Message will go here</p>
<div id="adobe-dc-view"></div>
<script src="https://documentcloud.adobe.com/view-sdk/main.js"></script>
<script type="text/javascript">
document.addEventListener("adobe_dc_view_sdk.ready", function(){
var adobeDCView = new AdobeDC.View({clientId: "my api key here", divId: "adobe-dc-view"});
adobeDCView.previewFile({
content:{location: {url: "https://documentcloud.adobe.com/view-sdk-demo/PDFs/Bodea Brochure.pdf"}},
metaData:{fileName: "Bodea Brochure.pdf"}
}, {embedMode: "SIZED_CONTAINER"});
});
</script>
</body>
</html>