mirror of
https://github.com/NoRedInk/noredink-ui.git
synced 2024-12-01 09:12:33 +03:00
64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
|
// this script is from the example axe docs at https://github.com/dequelabs/axe-core/blob/develop/doc/examples/puppeteer/axe-puppeteer.js
|
||
|
// it is licensed MPL 2.0: https://github.com/dequelabs/axe-core/blob/develop/LICENSE
|
||
|
|
||
|
const puppeteer = require('puppeteer');
|
||
|
const axeCore = require('axe-core');
|
||
|
const { parse: parseURL } = require('url');
|
||
|
const assert = require('assert');
|
||
|
|
||
|
// Cheap URL validation
|
||
|
const isValidURL = input => {
|
||
|
const u = parseURL(input);
|
||
|
return u.protocol && u.host;
|
||
|
};
|
||
|
|
||
|
// node axe-puppeteer.js <url>
|
||
|
const url = process.argv[2];
|
||
|
assert(isValidURL(url), 'Invalid URL');
|
||
|
|
||
|
const main = async url => {
|
||
|
let browser;
|
||
|
let results;
|
||
|
try {
|
||
|
// Setup Puppeteer
|
||
|
browser = await puppeteer.launch();
|
||
|
|
||
|
// Get new page
|
||
|
const page = await browser.newPage();
|
||
|
await page.goto(url);
|
||
|
|
||
|
// Inject and run axe-core
|
||
|
const handle = await page.evaluateHandle(`
|
||
|
// Inject axe source code
|
||
|
${axeCore.source}
|
||
|
// Run axe
|
||
|
axe.run()
|
||
|
`);
|
||
|
|
||
|
// Get the results from `axe.run()`.
|
||
|
results = await handle.jsonValue();
|
||
|
// Destroy the handle & return axe results.
|
||
|
await handle.dispose();
|
||
|
} catch (err) {
|
||
|
// Ensure we close the puppeteer connection when possible
|
||
|
if (browser) {
|
||
|
await browser.close();
|
||
|
}
|
||
|
|
||
|
// Re-throw
|
||
|
throw err;
|
||
|
}
|
||
|
|
||
|
await browser.close();
|
||
|
return results;
|
||
|
};
|
||
|
|
||
|
main(url)
|
||
|
.then(results => {
|
||
|
console.log(JSON.stringify(results));
|
||
|
})
|
||
|
.catch(err => {
|
||
|
console.error('Error running axe-core:', err.message);
|
||
|
process.exit(1);
|
||
|
});
|