84 lines
2.9 KiB
JavaScript
84 lines
2.9 KiB
JavaScript
|
const express = require('express');
|
||
|
const puppeteer = require('puppeteer');
|
||
|
const aChecker = require("accessibility-checker");
|
||
|
const uuid = require("uuid");
|
||
|
const { vnu } = require("vnu");
|
||
|
const { exception } = require('console');
|
||
|
|
||
|
async function main() {
|
||
|
delete process.env._JAVA_OPTIONS;
|
||
|
const app = express();
|
||
|
const browser = await puppeteer.launch({ headless: true });
|
||
|
app.get('/', async (req, res) => {
|
||
|
const url = String(req.query.url || '');
|
||
|
console.log(`Preparing report for ${url}`);
|
||
|
const page = await browser.newPage();
|
||
|
let code = 200;
|
||
|
let resp = null;
|
||
|
try {
|
||
|
const vnu_promise = vnu(url, {
|
||
|
"also-csheck-css": true,
|
||
|
"also-check-svg": true,
|
||
|
});
|
||
|
let links = null;
|
||
|
let element_count = null;
|
||
|
let results = null;
|
||
|
try {
|
||
|
await page.goto(url, { 'timeout': 30 * 1000, 'waitUntil': 'load' });
|
||
|
const all_hrefs = await page.evaluate(() => Array.from(document.querySelectorAll('a[href]')).map(x => x.href));
|
||
|
const links_mess = all_hrefs.filter(x => x.startsWith('https://') || x.startsWith('http://')).map(x => x.split('#')[0]);
|
||
|
links = Array.from(new Set(links_mess)).sort();
|
||
|
element_count = await page.evaluate(() => document.querySelectorAll('*').length);
|
||
|
const uid = uuid.v4();
|
||
|
const compliance = await aChecker.getCompliance(page, uid);
|
||
|
results = compliance.report.results;
|
||
|
} catch (e) {
|
||
|
code = 500;
|
||
|
if (resp?.type !== 'errors') {
|
||
|
resp = {
|
||
|
type: 'errors',
|
||
|
errors: []
|
||
|
};
|
||
|
}
|
||
|
resp.errors.push({
|
||
|
name: e.name,
|
||
|
message: e.message,
|
||
|
stack: e.stack
|
||
|
});
|
||
|
}
|
||
|
const vnu_report = await vnu_promise;
|
||
|
if (resp === null) {
|
||
|
resp = {
|
||
|
type: 'success',
|
||
|
element_count,
|
||
|
links,
|
||
|
results,
|
||
|
vnu_report,
|
||
|
};
|
||
|
}
|
||
|
} catch (e) {
|
||
|
code = 500;
|
||
|
if (resp?.type !== 'errors') {
|
||
|
resp = {
|
||
|
type: 'errors',
|
||
|
errors: []
|
||
|
};
|
||
|
}
|
||
|
resp.errors.push({
|
||
|
name: e.name,
|
||
|
message: e.message,
|
||
|
stack: e.stack
|
||
|
});
|
||
|
}
|
||
|
await page.close();
|
||
|
console.log(`Returning ${code} for ${url}`);
|
||
|
res.statusCode = code;
|
||
|
res.json(resp);
|
||
|
res.end();
|
||
|
});
|
||
|
app.listen(64161, () => console.log('Listening on http://localhost:64161'));
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
main
|
||
|
};
|