-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,42 @@ | ||
import pa11y from "pa11y"; | ||
import axios from "axios"; | ||
import { parseString } from 'xml2js'; | ||
import puppeteer from "puppeteer"; | ||
import core from "@actions/core"; | ||
|
||
process.on('warning', e => console.warn(e.stack)); | ||
const navigationTimeout = 120000; // Set the navigation timeout to 120 seconds (120,000 milliseconds) | ||
|
||
const sitemapUrl = 'https://developers.cloudflare.com/sitemap.xml'; | ||
const urlsToProcess = []; // Array to store URLs for processing | ||
async function checkLinks() { | ||
const browser = await puppeteer.launch({ | ||
headless: "new", | ||
}); | ||
const page = await browser.newPage(); | ||
|
||
async function processUrl(url) { | ||
try { | ||
const results = await pa11y(url); | ||
console.log(results); | ||
} catch (error) { | ||
console.log("error"); | ||
const sitemapUrl = "https://developers.cloudflare.com/sitemap.xml"; | ||
await page.goto(sitemapUrl, { timeout: navigationTimeout }); | ||
|
||
const sitemapLinks = await page.$$eval("url loc", (elements) => | ||
elements.map((el) => el.textContent) | ||
); | ||
|
||
const visitedLinks = []; | ||
const brokenLinks = []; | ||
|
||
for (const link of sitemapLinks) { | ||
if (!link) { | ||
continue; // Skip if the link is empty | ||
} | ||
} | ||
|
||
axios.get(sitemapUrl) | ||
.then(response => { | ||
if (response.status === 200) { | ||
parseString(response.data, (err, result) => { | ||
if (err) { | ||
console.error('Error parsing XML:', err); | ||
return; | ||
} | ||
|
||
const urls = result.urlset.url.map(urlObj => urlObj.loc[0]); | ||
urlsToProcess.push(...urls); // Add individual URLs to the array | ||
}); | ||
} else { | ||
console.error('Failed to fetch sitemap. Status code:', response.status); | ||
} | ||
}) | ||
.catch(error => { | ||
console.error('An error occurred:', error); | ||
|
||
const page2 = await browser.newPage() | ||
const result = await pa11y(link, { | ||
browser, | ||
page: page2 | ||
}) | ||
.finally(() => { | ||
// Process the URLs after the response has been handled | ||
urlsToProcess.forEach(processUrl); | ||
}); | ||
|
||
console.log(result); | ||
await page2.close(); | ||
} | ||
await page.close(); | ||
await browser.close(); | ||
} | ||
|
||
|
||
checkLinks(); |