Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Small JS snippet to easily spot dead links #11094

Open
xplshn opened this issue Mar 10, 2024 · 2 comments
Open

[Enhancement] Small JS snippet to easily spot dead links #11094

xplshn opened this issue Mar 10, 2024 · 2 comments

Comments

@xplshn
Copy link

xplshn commented Mar 10, 2024

Could we add this snippet to easily spot dead links? It will send a HEAD to every link in the page and if it is not 200, 403, 405, it will append this to the link: "[status: statusCode]", it is all client side and isn't noticeable, not even in web pages with lots of links, its what I have on my website too. :)

        // Function to check the status of a URL
        function checkLinkStatus(link, callback) {
            var proxyUrl = 'https://corsproxy.org/?';
            var targetUrl = encodeURIComponent(link.href);
            var xhr = new XMLHttpRequest();
            xhr.open('HEAD', proxyUrl + targetUrl, true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState ===  4) {
                    callback(xhr.status);
                }
            };
            xhr.send();
        }

        // Function to update link text with status code
        function updateLinkText(link, statusCode) {
            // Check if a status span already exists for this link
            var existingStatusSpan = link.nextElementSibling;
            if (existingStatusSpan && existingStatusSpan.classList.contains('status_code')) {
                // Update the existing status span
                existingStatusSpan.textContent = ' [status: ' + statusCode + ']';
            } else {
                // Create a new span element with the class 'status_code'
                var statusSpan = document.createElement('span');
                statusSpan.className = 'status_code';

                // Check the status code and set the text content of the span
                if (statusCode !==  200) {
                    if (statusCode !==  0) {
                        if (statusCode !==  403) {
                            if (statusCode !==  405) {
                                statusSpan.textContent = ' [status: ' + statusCode + ']';
                            }
                        }
                    }
                }

                // Insert the status span after the link
                link.parentNode.insertBefore(statusSpan, link.nextSibling);
            }
        }

        // Function to check all links on page load
        function checkAllLinksOnPageLoad() {
            var links = document.querySelectorAll('a');
            links.forEach(function(link) {
                checkLinkStatus(link, function(statusCode) {
                    updateLinkText(link, statusCode);
                });
            });
        }

        // Run the function when the page loads
        window.onload = checkAllLinksOnPageLoad;

Copy link

This issue has been automatically marked as stale because it has not had recent activity during last 60 days 😴

It will be closed in 30 days if no further activity occurs. To unstale this issue, remove stale label or add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest.

Thank you for your patience ❤️

@github-actions github-actions bot added the stale Requests that have not had recent interaction (Out-of-Date) label May 10, 2024
@xplshn
Copy link
Author

xplshn commented May 10, 2024

:(

@github-actions github-actions bot removed the stale Requests that have not had recent interaction (Out-of-Date) label May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant