let create= (x)=> document.createElement(x),
select= (x,y=document)=> y.querySelector(x),
selectAll= (x,y=document)=> y.querySelectorAll(x);



let titles= selectAll("a[href*=documenten] h4");
let links= [...titles].map(i=> i.closest("a").href);

let amt= 0;
let errors= {};
let success= {};

links.forEach((i,x)=>{
let failed= false;
fetch(i)
.then(r=> r.text())
.catch(f=> failed= true)
.then(d=>{
if(failed){
errors[i]= "RESPONSE NOT OK";
amt++;
return;
}

let doc= new DOMParser().parseFromString(d,"text/html");
let dl= select(".pf-body a",doc);
if(dl){
success[titles[x].innerHTML]= dl.href;
}
else{
errors[i]= "ELEMENT NOT FOUND";
failed= true;
}
amt++;
})
.catch(err=>{
errors[i]= "RESPONSE OK BUT STILL ERROR";
});

});




let fetchFinished= setInterval(()=>{
if(amt>=links.length){
clearInterval(fetchFinished);
showInNewTab(JSON.stringify(success)+"\n\n======\n\n"+JSON.stringify(errors) );
}	
},1000);


function showInNewTab(text){
let ta= create("textarea");
ta.value= text;
ta.style.cssText= `width:98vw; height: 150vw;
border: 2px solid black; margin: 0;
padding: 10px; font: 14px Arial`;
window.open().document.body.append(ta);
}



