-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·249 lines (190 loc) · 5.64 KB
/
server.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// load the things we need
var express = require('express');
var app = express();
var path = require('path');
var http = require('http');
var AxeBuilder = require('axe-webdriverjs');
var WebDriver = require('selenium-webdriver');
const puppeteer = require('puppeteer');
const firefox = require('selenium-webdriver/firefox');
const bodyParser = require('body-parser');
var urlExists = require('url-exists');
var axe = require('axe-core');
var events = require('events');
var fs = require('fs');
var score;
var scoreColor;
var errorForm = "";
var error = "Voer een url in om je website te testen";
var urlencoderParser = bodyParser.urlencoded({ extended: true });
// set the view engine to ejs
app.set('view engine', 'ejs');
// Set the port number
let port = 8080;
app.use(express.static(__dirname + '/public'));
// Accessibility check page
app.get('/', function (req, res) {
res.render('pages/accessibilityTester', {
errorForm: errorForm,
error: error
});
});
// app.get('/accessibilityCheck', urlencoderParser, function (req, res) {
// score = 500;
// url = req.body.url;
// var urlExist = false;
// var finalUrl;
// if (req.body.url == "") {
// console.log("empty");
// }
// if (url.indexOf("http://") == 0 || url.indexOf("https://") == 0) {
// finalUrl = url;
// } else {
// finalUrl = "https://" + url;
// }
// try {
// urlExists(finalUrl, function (err, exists) {
// if (exists) {
// urlExist = true;
// } else {
// res.writeHead(200, {
// 'Content-Type': 'text/html',
// 'Access-Control-Allow-Origin': '*'
// });
// // res.redirect('/');
// errorForm = "errorForm";
// error = "Voer een correcte url in";
// }
// });
// } catch (e) {
// console.error(e);
// }
// res.send(urlExist);
// console.log(finalUrl);
// });
app.post('/accessibilityCheck', urlencoderParser, function (req, res) {
score = 500;
url = req.body.url;
var finalUrl;
if (req.body.url == "") {
console.log("empty");
}
var result;
var driver = new WebDriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().headless(), new firefox.Options().proxy = null)
.build();
var colorContrastArray = [];
var HTMLstructureArray = [];
var missingHTMLArray = []
var scalingArray = [];
var impact;
var category;
var scorePercentage;
if (url.indexOf("http://") == 0 || url.indexOf("https://") == 0) {
finalUrl = url;
} else {
finalUrl = "https://" + url;
}
try {
urlExists(finalUrl, function (err, exists) {
if (exists) {
urlExist = true;
errorForm = "errorForm-correct";
(async () => {
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox'] });
const page = await browser.newPage();
await page.setViewport({
width: 800,
height: 600
});
await page.goto(finalUrl, { waitUntil: 'networkidle2' });
await page.screenshot({ path: 'public/img/out1.png', fullPage: true });
await browser.close();
})();
driver
.get(finalUrl)
.then(function () {
AxeBuilder(driver)
.analyze(function (results) {
result = results['violations'];
for (let i = 0; i < result.length; i++) {
impact = results['violations'][i]['impact']
category = results['violations'][i]['tags'][0];
if (category == 'cat.color') {
colorContrastArray.push(results['violations'][i]);
}
if (category == 'cat.sensory-and-visual-cues' || category == 'cat.time-and-media') {
scalingArray.push(results['violations'][i]);
}
if (category == 'cat.text-alternatives' || category == 'cat.aria' || category == 'cat.name-role-value' || category == 'cat.forms') {
missingHTMLArray.push(results['violations'][i]);
}
if (category == 'cat.language' || category == 'cat.tables' || category == 'cat.structure' || category == 'cat.semantics' || category == 'cat.keyboard' || category == 'cat.parsing') {
HTMLstructureArray.push(results['violations'][i]);
}
switch (impact) {
case 'critical':
score -= 50;
break;
case 'serious':
score -= 40;
break;
case 'moderate':
score -= 10;
break;
case 'minor':
score -= 5;
break;
default:
break;
}
}
scorePercentage = score / 500 * 100;
if (scorePercentage >= 95) {
scoreColor = 'green';
} else if (scorePercentage < 95 && scorePercentage > 70) {
scoreColor = 'yellow';
} else if (scorePercentage <= 70 && scorePercentage > 50) {
scoreColor = 'orange';
} else {
scoreColor = 'red';
}
res.render('pages/results', {
data: result,
score: score,
url: url,
scoreColor: scoreColor,
scalingArray: scalingArray,
scorePercentage: scorePercentage,
colorContrastArray: colorContrastArray,
missingHTMLArray: missingHTMLArray,
HTMLstructureArray: HTMLstructureArray
});
driver.quit();
});
});
} else {
// res.writeHead(200, {
// 'Content-Type': 'text/html',
// 'Access-Control-Allow-Origin': '*'
// });
// res.send(urlExist);
res.redirect('/');
errorForm = "errorForm";
error = "Voer een correcte url in";
}
});
} catch (e) {
console.error(e);
driver.quit();
}
console.log(finalUrl);
});
app.get('*', function (req, res) {
res.render('pages/404', {
});
});
const server = http.createServer(app).listen(port, function (req, res) {
console.log('LISTENING ON PORT ' + port);
});