IFrame:
(function() {
var actions = {
getContent: function() {
return document.documentElement.innerHTML;
}
};
window.addEventListener('message', function(e) {
if(e.origin === 'http://bistro-obed.ru') {
var data = JSON.parse(e.data);
if(data.action in actions) {
data.result = actions[data.action]();
parent.postMessage(JSON.stringify(data), '*');
}
}
});
})();
Основной сайт:
(function() {
var frame = document.getElementById('include_content');
var callbackList = {
handleContent: function(result) {
var contents = (new DOMParser).parseFromString(result, 'text/html');
console.log(contents);
}
};
window.addEventListener('message', function(e) {
if(e.origin === 'https://menu.food24h.ru') {
var data = JSON.parse(e.data);
if(data.callback in callbackList) {
callbackList[data.callback](data.result);
}
}
});
frame.addEventListener('load', function() {
var data = {
action: 'getContent',
callback: 'handleContent'
};
frame.contentWindow.postMessage(JSON.stringify(data), '*');
});
})();