Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find images with no alt text.
console.clear();
let out = '';
let images = [];
let problems = [];
$$('img').forEach(img => {
//document.querySelectorAll('img').forEach(img => {
let text = img.getAttribute('alt');
if (!text) {
img.style.outline = '3px solid orangered';
problems.push(img);
}
images.push(img);
out += `
${text||'No Link text'}
${img.alt}`;
});
if (out === '') {
console.warn('Sorry, no images found');
} else {
let firstImage = "";
//copy(out);
//console.info('done harvesting links, ready to paste');
console.info('Done looking for images.')
if (problems.length > 0) {
let s = problems.length === 1 ? "" : "s";
console.warn('%c[DEBUG] %d image%s with no alt text.', 'color: orangered', problems.length, s);
console.groupCollapsed('Images without alt text');
problems.forEach(img => {
if (!firstImage) {
firstImage = img;
}
console.dirxml(img)
});
console.groupEnd('Images without alt text');
}
if (images.length > 0) {
let s = images.length === 1 ? "" : "s";
console.warn('%c[DEBUG] %d image%s found.', 'color: limegreen', images.length, s);
console.groupCollapsed('All images');
images.forEach(img => {console.dirxml(img)});
console.groupEnd('All links');
}
firstImage.scrollIntoView();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment