I'm currently working on a script that will check all the the links of a page and highlight any broken links. Synchronously, this wasn't a major task, but I only just started working with Asynch, so any help with my code would be much appreciated.
var size = 0;
var itemsProcessed = 0;
console.log("Size: " + size);
function linkCheck() {
var body = document.querySelectorAll("*");
var checker = Array.from($(body).filter("a"));
size = (Object.keys(checker).length);
console.log("Size: " + size);
var list;
checker.forEach(function (anchor) {
var link = $(anchor).attr("href");
itemsProcessed++;
console.log("Items: " + itemsProcessed);
if (itemsProcessed == size) {
colorUp(list);
}
var req = new XMLHttpRequest();
req.open('HEAD', $(checker).attr("href"), true);
itemsProcessed++;
try {
req.send(null);
} catch (e) {
console.log("ERROR");
return true;
}
console.log(req.status);
var data = (req.status !== 200);
if (data) {
//console.log("Added to List")
list += data;
}
console.log(link, data);
});
}
function colorUp(list) {
console.log("DONE");
$(list).css({
"border": "3px solid #ffb700",
"background": "repeating-linear-gradient(135deg, #FFE0B2, #FFE0B2 5px, #ffffff 5px, #ffffff 10px)"
});
}
linkCheck();
Currently, I'm having trouble waiting for the forEach to finish, but if you have any suggestions, or anything I can do to make the problem clearer, feel free to leave a post!
Thank you guys