Parent page and iframe are on the same domain. How can I set ifErme MutationObserver in documentElement?

This is not how it works:

var observer = new WebKitMutationObserver ( function observe(mutations) { mutations.map(function(mutation){console.log(mutation.addedNodes);}); } ); observer.observe(document.getElementsByTagName("iframe")[0].contentDocument.documentElement, {attributes: true, childList: true, characterData: true, subtree: true}); 
  • Try to listen to events in the iframe itself, and send the event to the parent using window.postMessage - vihtor
  • vihtor, for some reason I cannot change the iframe (the domain is not mine, I am writing an extension). - Ilya Koldunov

1 answer 1

It turned out that it is impossible to just take and add an event (and MutationObserver is one) into an unloaded document. Here it will work:

 var observer = new WebKitMutationObserver ( function observe(mutations) { mutations.map(function(mutation){console.log(mutation.addedNodes);}); } ); document.getElementsByTagName("iframe")[0].onload = function() { observer.observe(document.getElementsByTagName("iframe")[0].contentDocument.documentElement, {attributes: true, childList: true, characterData: true, subtree: true}); };