forked from matuzo/DevToolsSnippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugimages.js
79 lines (58 loc) · 1.72 KB
/
debugimages.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
// Debug images
{
console.clear();
const images = document.querySelectorAll('img');
const missingAlt = []
const emptyAlt = []
const suspiciousAlt = []
const missingDimensions = []
const suspicious = ['alt', 'image', 'img', 'logo']
if (images.length) {
for (let i = 0; i < images.length; i++) {
const image = images[i];
if (image.getAttribute('alt')) {
const attr = image.getAttribute('alt').trim();
if (attr === '') {
emptyAlt.push(image)
}
if (suspicious.indexOf(attr) > -1) {
suspiciousAlt.push(image)
}
} else {
missingAlt.push(image)
}
if (!image.attributes.width || !image.attributes.height) {
missingDimensions.push(image)
}
}
if (suspiciousAlt.length) {
console.warn('%cImages with suspicious alt, please check!', 'font-size: 13px');
for(image of suspiciousAlt) {
console.log(image)
}
}
if (missingDimensions.length) {
console.warn('%cImages without width and/or height attribute, please check!', 'font-size: 13px');
for(image of missingDimensions) {
console.log(image)
}
}
if (emptyAlt.length) {
console.info('%cImages with empty alt', 'font-size: 13px;');
for(image of emptyAlt) {
console.log(image)
}
}
if (missingAlt.length) {
console.error('%cImages with missing alt', 'font-size: 13px;');
for(image of missingAlt) {
console.log(image)
}
}
}
if (![...suspiciousAlt, ...missingDimensions, ...emptyAlt, ...missingAlt].length) {
console.info('All images look OK!')
}
var done = 'Finished running “Debug images”'
done;
}