Skip to content

Instantly share code, notes, and snippets.

@sajjadmd
Created September 11, 2019 15:04
Show Gist options
  • Select an option

  • Save sajjadmd/e00dc28a8dcdc99bbe8b9df81bf9b8f9 to your computer and use it in GitHub Desktop.

Select an option

Save sajjadmd/e00dc28a8dcdc99bbe8b9df81bf9b8f9 to your computer and use it in GitHub Desktop.
Checks for status of all linked internal pages on the current page. SEO Scripts.
(async function() {
const getStatus = async (u) => {
let response;
try {
response = await fetch(u, {method: 'HEAD'})
} catch (err) {
console.log("%cProbably OK, but forbidden, do a manual check", "font-size:11px;color:red;")
return 0;
}
return response.status;
}
async function check404() {
clear();
const links = document.querySelectorAll("body a");
const base = `${window.location.protocol}//${window.location.hostname}`;
let valid = [];
links.forEach((l) => {
let href = l.getAttribute("href");
if (!href) { return };
if (href.startsWith(base)) {
valid.push(href.trim());
} else if (
href.startsWith("/")
|| (href.match(/^[a-zA-Z0-9]/gi) !== null && !href.includes(":"))
) {
href = new URL(href, base);
valid.push((href.toString()).trim());
} else {
console.log(`%cProbably not a link to this site/subdomain/domain: ${href}`, "font-size:12px;color:red;padding-top:10px");
return;
}
});
function uniq(value, index, self) {
return self.indexOf(value) === index;
}
if (valid.length > 0) {
valid = valid.filter(uniq);
}
console.log(`%cWill check for ${valid.length} pages`, "font-size:16px;font-style:bold;");
for (const u of valid) {
const status = await getStatus(u);
if (status === 200 || status === 304) {
console.log(`%cOK (${status}): ${u}`, "font-size:12px;color:blue;border-top:1px solid red;padding-top:10px");
} else {
console.log(`%cBAD ${status}: ${u}`, "font-size:12px;color:red;border-top:1px solid red;padding-top:10px");
}
}
};
check404();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment