Skip to content

Instantly share code, notes, and snippets.

@BlackPrincess
Created October 21, 2016 09:27
Show Gist options
  • Save BlackPrincess/66959fd8da155c340673efe0153a1168 to your computer and use it in GitHub Desktop.
Save BlackPrincess/66959fd8da155c340673efe0153a1168 to your computer and use it in GitHub Desktop.
function createNgElement(reason) {
const ng = document.createElement("span")
ng.style.position = "relative"
ng.style.top = "0"
ng.style.left = "0"
ng.style.zIndex = "9999"
ng.style.width = "20px"
ng.style.height = "20px"
ng.style.padding = "0 10px"
ng.style.backgroundColor = "#d42d2d"
ng.style.color = "#fff"
ng.style.fontSize = "12px"
ng.style.opacity = "0.7"
ng.className = "link-checker-ng"
ng.innerText = reason
return ng;
}
function linkCheck(list) {
if(list.length === 0) return;
const [a, ...xs] = list
const href = a.getAttribute("href")
if(href === "") {
const ng = createNgElement("href=''")
a.appendChild(ng)
linkCheck(xs)
return;
}
if(href === "#") {
const ng = createNgElement("href='#'")
a.appendChild(ng)
linkCheck(xs)
return;
}
fetch(href, {mode: 'no-cors'}).then((res) => {
linkCheck(xs)
return res
}).then((res) => {
if(res.body === null) {
const ng = createNgElement("unchecked")
a.appendChild(ng)
return;
}
if(!res.ok) {
const ng = createNgElement("ng")
a.appendChild(ng)
return
}
})
}
linkCheck(Array.from(document.querySelectorAll("a")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment