Skip to content

Commit

Permalink
Update link-checker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblogan authored Aug 31, 2023
1 parent 4264c8a commit 0044f65
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions tasks/link-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,38 @@ const retrieveLinks = async (siteMapUrls, visitedLinks) => {
for (let i = 0; i < siteMapUrls.length; i++) {
let url = siteMapUrls[i];

let response = await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 2 });
await page.waitForNetworkIdle();
if (response && response.status() && response.status() === 200) {
console.log(`successfully visited ${url} to retrieve links`);
visitedLinks[url] = true;

const urlList = await page.evaluate(async (url) => {
let urls = [];
let elements = document.getElementsByTagName('a');
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
if (element.href) {
const link = {
url: element.href,
parentUrl: url,
linkText: element.textContent
};
urls.push(link);
try{
let response = await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 2 });
await page.waitForNetworkIdle();
if (response && response.status() && response.status() === 200) {
console.log(`successfully visited ${url} to retrieve links`);
visitedLinks[url] = true;

const urlList = await page.evaluate(async (url) => {
let urls = [];
let elements = document.getElementsByTagName('a');
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
if (element.href) {
const link = {
url: element.href,
parentUrl: url,
linkText: element.textContent
};
urls.push(link);
}
}
}
return urls;
}, url);

urlList.forEach((link) => {
if (!CRAWLER_EXCEPTIONS.includes(link.url)) {
urlsToVisit.push(link);
}
});
return urls;
}, url);

urlList.forEach((link) => {
if (!CRAWLER_EXCEPTIONS.includes(link.url)) {
urlsToVisit.push(link);
}
});
}
} catch (e) {
console.log(`attempt to retrieve ${url} has failed: ${e}`);
}
}

Expand Down

0 comments on commit 0044f65

Please sign in to comment.