-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck.js
More file actions
122 lines (109 loc) · 3.79 KB
/
check.js
File metadata and controls
122 lines (109 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const argv = require('minimist')(process.argv.slice(2));
const { URL } = require('url');
//Convert to an absolute URL by default (for example, this will add a trailing slash after the domain if it wasn't provided)
const url = new URL(argv._[0]).href;
const util = require('util');
const puppeteer = require('puppeteer');
const axe = require('axe-core');
const fs = require('fs');
let path = __dirname + '/config/axe-options.inc.json';
if (!fs.existsSync(path)) {
path = __dirname + '/config/axe-options.sample.json';
}
const axeOptions = JSON.parse(fs.readFileSync(path, 'utf8'));
let chromeTmpDataDir = null;
const getViolations = async (url, darkMode = false) => {
try {
let options = {headless: "new",
args: [
'--aggressive-cache-discard',
'--disable-cache',
'--disable-application-cache',
'--disable-offline-load-stale-cache',
'--disable-gpu-shader-disk-cache',
'--media-cache-size=0',
'--disk-cache-size=0',
'--disable-extensions',
'--disable-component-extensions-with-background-pages',
'--disable-default-apps',
'--mute-audio',
'--no-default-browser-check',
'--autoplay-policy=user-gesture-required',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-notifications',
'--disable-background-networking',
'--disable-breakpad',
'--disable-component-update',
'--disable-domain-reliability',
'--disable-sync',
]
};
if (argv.sandbox === 'false') {
options.args = ['--no-sandbox', '--disable-setuid-sandbox'];
}
const prefersColorScheme = darkMode === true ? 'dark' : 'light';
const browser = await puppeteer.launch(options);
const page = await browser.newPage();
await page.setCacheEnabled(false);
if (argv.ua) {
await page.setUserAgent(argv.ua);
}
let chromeSpawnArgs = browser.process().spawnargs;
for (let i = 0; i < chromeSpawnArgs.length; i++) {
if (chromeSpawnArgs[i].indexOf("--user-data-dir=") === 0) {
chromeTmpDataDir = chromeSpawnArgs[i].replace("--user-data-dir=", "");
}
}
await page.on('dialog', async dialog => {
//Auto dismiss dialogs so that the process does not hang waiting on user input.
await dialog.dismiss();
});
await page.emulateMediaFeatures([{name: 'prefers-color-scheme', value: prefersColorScheme}]);
await page.goto(url);
await page.addScriptTag({
path: require.resolve('axe-core')
});
// run axe on the page
const axeResults = await page.evaluate(async (axeOptions) => {
return await axe.run(axeOptions);
}, axeOptions);
await browser.close();
return axeResults.violations;
} catch (e) {
//fail early
process.exit(1);
}
};
const deleteFolderRecursive = function(path) {
var files = [];
if( fs.existsSync(path) ) {
files = fs.readdirSync(path);
files.forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
(async () => {
const violations = await getViolations(url, false);
const darkModeContrastViolations = argv.dark_mode === 'true' ? await getViolations(url, true) : [];
if (chromeTmpDataDir != null) {
deleteFolderRecursive(chromeTmpDataDir);
}
console.log(
JSON.stringify(
violations.concat(
darkModeContrastViolations.filter(violation => violation.id === 'color-contrast')
.map((item) => {
item.id += '-darkmode';
item.help += ' (Dark Mode)';
return item;
})
)));
})();