Last active
July 1, 2024 17:04
-
-
Save sam2332/b3535a1e6514a64a7b6360e72a22795d to your computer and use it in GitHub Desktop.
User script to scan web links on pages and highlight broken pages.
This file contains 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
// ==UserScript== | |
// @name Highlight 404 links in red background | |
// @namespace http://tampermonkey.net/ | |
// @version 2024-07-01 | |
// @description try to take over the world! | |
// @author You | |
// @match https://*.ingham.org/* | |
// @match https://cms3.revize.com/revize/inghamcounty/* | |
// @exclude https://itsm.ingham.org/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=ingham.org | |
// @grant GM_xmlhttpRequest | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Function to check if the base URL contains 'revize.com' or 'ingham.com' | |
function checkBaseURL() { | |
const baseElement = document.querySelector('base'); | |
if (!baseElement) return false; | |
const hrefAttribute = baseElement.getAttribute('href'); | |
const regexMatchIngham = /\/\/[a-zA-Z0-9-]+\.ingham\.org\//i; | |
const regexMatchRevize = /\/\/[a-zA-Z0-9-]+\.revize\.org\//i; | |
// Return true if href matches either regex, otherwise return false | |
if (regexMatchIngham.test(hrefAttribute)) { | |
return true; | |
} else if (regexMatchRevize.test(hrefAttribute)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
// Execute the script only if the base URL check passes | |
if (!checkBaseURL()) { | |
console.log('Base URL does not contain revize.com or ingham.com, script will not run.'); | |
return; | |
} | |
const links = document.querySelectorAll('a'); | |
links.forEach(link => { | |
const url = link.href; | |
if (url.startsWith('mailto:') || url.startsWith('tel:')) { | |
console.log('Skipping mailto or tel link:', url); | |
return; // Skip this iteration of the loop | |
} | |
const parts = url.split('.'); | |
const lastSegment = parts[parts.length - 1].split(/#|\?/)[0]; | |
let useGetMethod = true; // Default to GET method | |
// Define extensions known to be static resources | |
const staticExtensions = ['jpg', 'jpeg', 'png', 'gif', 'css', 'js', 'ico', 'svg', 'mp4', 'mp3', 'pdf']; | |
// Check if the last segment of the URL contains one of the static extensions | |
if (parts.length > 1 && staticExtensions.includes(lastSegment)) { | |
useGetMethod = false; // Use HEAD for static resources | |
} | |
// Apply a loading color (e.g., light blue) | |
link.style.backgroundColor = 'lightblue'; | |
console.log('Checking link:', link.href); | |
GM_xmlhttpRequest({ | |
method: useGetMethod ? 'GET' : 'HEAD', | |
url: url, | |
onload: function(response) { | |
// Parse the response to check the title | |
if (useGetMethod) { | |
const parser = new DOMParser(); | |
const doc = parser.parseFromString(response.responseText, "text/html"); | |
const title = doc.querySelector('title') ? doc.querySelector('title').textContent : ""; | |
if (response.status === 404 || title.includes('404')) { | |
link.style.backgroundColor = 'red'; | |
console.error('404 Detected in title or status:', link.href); | |
} else { | |
link.style.backgroundColor = ''; | |
console.log('Link is valid:', link.href); | |
} | |
} else { | |
if (response.status === 404) { | |
link.style.backgroundColor = 'red'; | |
console.error('404 Detected by status:', link.href); | |
} else { | |
link.style.backgroundColor = ''; | |
console.log('Link is valid:', link.href); | |
} | |
} | |
}, | |
onerror: function() { | |
link.style.backgroundColor = 'orange'; | |
console.error('Error checking link:', link.href); | |
}, | |
ontimeout: function() { | |
link.style.backgroundColor = 'orange'; | |
console.warn('Timeout checking link:', link.href); | |
} | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment