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

Closed
xplshn opened this issue Mar 10, 2024 · 7 comments
Closed

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

xplshn opened this issue Mar 10, 2024 · 7 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
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 Jul 10, 2024
@xplshn
Copy link
Author

xplshn commented Jul 10, 2024

:(

@github-actions github-actions bot removed the stale Requests that have not had recent interaction (Out-of-Date) label Jul 11, 2024
Copy link

github-actions bot commented Sep 9, 2024

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 Sep 9, 2024
@xplshn
Copy link
Author

xplshn commented Sep 9, 2024

:(

@github-actions github-actions bot removed the stale Requests that have not had recent interaction (Out-of-Date) label Sep 10, 2024
@eshellman
Copy link
Collaborator

note that we have a linkchecker in our github actions that gets invoked on every PR. We would welcome refinements to the link checker. https://github.com/EbookFoundation/free-programming-books/blob/main/.github/workflows/check-urls.yml That checker uses awesome-bot.

We're not really in a position to maintain link checking code in this repo.

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

2 participants