Skip to content

Instantly share code, notes, and snippets.

@bainco
Created November 13, 2018 22:01
Show Gist options
  • Save bainco/141871200525426037b8d54f1f6cf54c to your computer and use it in GitHub Desktop.
Save bainco/141871200525426037b8d54f1f6cf54c to your computer and use it in GitHub Desktop.
NetTango iframe Cross Domain Messaging
//-------------------------------------------------------------
// MESSAGE POST HANDLER START
//-------------------------------------------------------------
/**************************************************************
Set the origin is the parent window
**************************************************************/
var origin = null;
var id = null;
//-------------------------------------------------------------
// A function to process messages received by the window.
//-------------------------------------------------------------
function receiveMessage(e) {
//set the origin on message receipt
//can also filter origin here
//the payload is in e.data
if (origin == null && (e.origin.indexOf('s3-us-west-1') >= 0 || e.origin.indexOf('localhost') >= 0 || e.origin.indexOf('ctstem-stage') >= 0 || e.origin.indexOf('ct-stem') >= 0)) {
origin = e.origin;
}
if (e.data && (e.origin.indexOf('s3-us-west-1') >= 0 || e.origin.indexOf('localhost') >= 0 || e.origin.indexOf('ctstem-stage') >= 0 || e.origin.indexOf('ct-stem') >= 0)) {
//verify data is valid and update the model
//console.log(e.data);
//console.log(typeof e.data);
var payload = JSON.parse(e.data);
id = payload.id;
var data = JSON.parse(payload.data);
if(data) {
//-----------------------------------------------------------
// resize canvases to their original state --
// NetTango will enlarge on higher pixel density displays
//-----------------------------------------------------------
var canvas = document.getElementById("nt-wolf-code");
if (canvas) {
canvas.getAttributeNode("width").value = "470";
canvas.getAttributeNode("height").value = "500";
}
canvas = document.getElementById("nt-moose-code");
if (canvas) {
canvas.getAttributeNode("width").value = "470";
canvas.getAttributeNode("height").value = "500";
}
//-----------------------------------------------------------
// restore code
//-----------------------------------------------------------
NetTango.restoreAll(data);
}
}
}
//-------------------------------------------------------------
// A function to send message to the parent window
//-------------------------------------------------------------
function sendMessage() {
//get the current state of the model in JSON format
var data = NetTango.saveAll();
if (origin) {
var payload = {'id': id, 'data': data};
window.parent.postMessage(JSON.stringify(payload), origin);
}
}
// Setup an event listener that calls receiveMessage() when the window
// receives a new MessageEvent.
window.addEventListener('message', receiveMessage);
$('button#recompile-button').on('click', function() {
recompile();
});
}
/**************************************************************
// MESSAGE POST HANDLER END
**************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment