There is an iframe to which there is access, because it is on the same domain. My task is to find the first link and put a handler on it. My code works, but there is one thing that I do not understand, and I would like to do it more competently. My actions: I put the MutationObserver on the document and wait for the iframe to be added. This moment works, iframe is taken. But then, in order to get access to his children, I have to set timeOut, otherwise the code does not work (A = null). I tried various features, like the contentWindow.onload event, document.DOMContentLoaded, tried to create another MutatonObserver, nothing except timeOut works. Help please, who understands what is the matter and how to fix it. My code is:
var observers = []; if(window.top == window.self){ var observer = new (window.MutationObserver || window.WebKitMutationObserver)(function(mutations) { for (var i = 0; i < mutations.length; i++) { if (mutations[i].type === 'childList') { parse(mutations[i].addedNodes); } } }); observer.observe( document.querySelector('body'), { childList : true, subtree : true, attributes : false }); observers.push(observer); } else{ run(document); } function parse(nodes){ [].forEach.call(nodes, function(node){ if(!/http(s?):/i.test(node.src)){ if(node.tagName == "IFRAME"){ setTimeout(function(){ run(node.contentWindow.document); }, 300); } } }); } function run(doc){ var a = doc.querySelector("a"); //<...> }