I am trying to loop through DOM elements of a webpage in a content script from a Google chrome extension. The problem is the list of results turns out to be sometimes different and I suspect this is because of scripts tagged as async, but as the process should start with window.onload this doesn't make any sense to me. Does anyone see why this would happen? EDIT :: I started to think that could be because I mark the window.onload function async. Would that be the problem?
The javascript code is as the following. It adds '2' for scripts and '1' for iframes and '-1' for others, ( '-1' is ignored later).
async function exam(element){
if (element.nodeName == 'SCRIPT'){
return '2';
}else if (element.nodeName == 'IFRAME'){
return '1';
}else{
return '-1';
}
}
async function domExam(element, list) {
let res = await exam(element);
if(res != '-1'){
list.push(res);
}
let chNodes = element.children;
if (chNodes.length > 0 ){
for (let item of chNodes){
list = await domExam(item, list);
}
return list;
}else{
return list;
}
}
window.onload = async function(){
list = await domExam(document, []);
console.log("List::", list);
}