I have an iframe in my index file and I want fetch all of the html from the iframe and store it in a variable. The content loaded inside of the iframe is from another domain. So I am trying to achieve this using javascript and postMessage and an eventListener. This is my code so far. I have a button which calls the click method.
index.vue
click() {
let iframe = document.getElementById('the_iframe');
iframe.contentWindow.postMessage('Dont really know what to send here', '*');
this.test();
},
test() {
window.addEventListener('message', function(event) {
//Just a test, as the it wont go in the else
console.log(event.currentTarget.document);
if (event.origin !== 'http://testdomain') {
console.log('ERROR');
} else {
console.log(event.currentTarget.document);
const html = event.currentTarget.document);
//This however doesnt work. The value is not stored in the const
console.log(event.currentTarget.document);
}
});
}
When I console log event.currentTarget.document it prints out the entire html structure of the site, including the parent page (index). My question is: How do i fetch all of the html of just the iframe inside of my index file? and how do I store it in a variable?