https://developers.google.com/web/tools/chrome-devtools/javascript/snippets
Last active
March 16, 2021 14:53
-
-
Save chavesm/46c6f6db7f1aab0e2dbbfad04b7630cf to your computer and use it in GitHub Desktop.
Find Links and Forms Using JavaScript and Chrome Dev Tools (snippets and console)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
let allForms = []; | |
let problems = []; | |
$$("form").forEach((f) => { | |
allForms.push(f); | |
if (!f.hasAttribute("id")) { | |
f.style.border = "3px solid orangered"; | |
problems.push(f); | |
} else { | |
f.style.border = "3px solid limegreen"; | |
} | |
}); | |
if (allForms.length > 0) { | |
console.info('There are %d forms.', allForms.length); | |
console.groupCollapsed('All forms'); | |
allForms.forEach(f => {console.dirxml(f)}); | |
console.groupEnd('All forms'); | |
} | |
if (problems.length > 0) { | |
console.warn('%cThere were %d forms with no ID.', 'color: orangered', problems.length); | |
console.groupCollapsed('Forms without ID'); | |
problems.forEach(f => {console.dirxml(f)}); | |
console.groupEnd('Forms without ID'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
let problems = []; | |
$$("form").forEach((f) => { | |
if (!f.hasAttribute("id")) { | |
f.style.border = "3px solid orangered"; | |
problems.push(f); | |
} | |
}); | |
if (problems.length > 0) { | |
console.warn('%cThere were %d forms with no ID.', 'color: orangered', problems.length); | |
console.groupCollapsed('Forms without ID'); | |
problems.forEach(f => {console.dirxml(f)}); | |
console.groupEnd('Forms without ID'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
let out = ''; | |
let problems = []; | |
$$('a').forEach(a => { | |
let isImg = false; | |
let text = a.innerText ? a.innerText.trim() : a.getAttribute('title'); | |
let prefix = ''; | |
if (!text) { | |
if (a.querySelector('img')){ | |
isImg = true; | |
text = a.querySelector('img').alt; | |
prefix = 'Image: '; | |
} | |
if (a.getAttribute('aria-label')) { | |
text = a.getAttribute('aria-label'); | |
prefix = 'Aria Label: '; | |
} | |
if (a.getAttribute('aria-labelledby')) { | |
text = $('#' + a.getAttribute('aria-labelledby')).innerText; | |
prefix = 'Aria Labelled By: '; | |
} | |
} | |
if (text) { | |
text = prefix + text | |
} else { | |
if (isImg) a.parentNode.style.border = '3px solid orangered'; | |
else a.style.border = '3px solid orangered'; | |
problems.push(a); | |
} | |
out += ` | |
${text||'No Link text'} | |
${a.href}`; | |
}); | |
if (out === '') { | |
console.warn('Sorry, no links found'); | |
} else { | |
copy(out); | |
console.info('done harvesting links, ready to paste'); | |
if (problems.length > 0) { | |
console.warn('%cThere were %d links with no text.', 'color: orangered', problems.length); | |
console.groupCollapsed('Links without text'); | |
problems.forEach(a => {console.dirxml(a)}); | |
console.groupEnd('Links without text'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment