10

About:

When you follow a link to a comment or answer you might end-up being confused if the answer or comment is removed.

This little userscript shows a warning on top of the page when the link you just used contains a fragment to an answer or comment that no longer exists on the page.

This is a repost of my original answer on Meta Stack Exchange

Screenshot:

Here is an Animated GIF to show the script in action
GIF kindly provided by user 41686d6564

Download / Install:

You can find it ready to be consumed by TamperMonkey or GreaseMonkey on the Github repository. Here is the direct install aand here it the source.

Platform:

The script will run on any browser that support userscripts with the general available userscript managers like Tampermonkey or GreaseMonkey.

Contact:

If you find a bug or have a feature request either create an issue or leave a comment here.

Allow for 6 to 8 weeks before fixing.

License:

The code is released under the MIT License

Code:

The code is written in JavaScript and has a dependency on the jQuery loaded on the page by Stack Exchange.

For interested parties here is the full code:

 // ==UserScript==
// @name         Check if comment and answer exist
// @namespace    http://meta.stackexchange.com/users/158100/rene
// @version      1.2
// @description  Show a message if the answer or comment in the hash of the url doesn't exist
// @author       rene
// @match        *://*.stackexchange.com/questions/*
// @match        *://stackoverflow.com/questions/*
// @match        *://serverfault.com/questions/*
// @match        *://askubuntu.com/questions/*
// @match        *://mathoverflow.net/questions/*
// @match        *://stackapps.com/questions/*
// @match        *://superuser.com/questions/*
// @grant        none
// ==/UserScript==

(function (document, window, StackExchange) {

    var hash = (window.location && window.location.hash)?window.location.hash:'';

    function showMessage(jqsel, text) {
        if (jqsel === null) {
            // wait for SE to be ready
            StackExchange.initialized.done(function() {
                var classid = 'missing-post-or-comment';
                // put a notify on top
                StackExchange.notify.show('The ' + text + ' is not found. It may have been deleted', classid);
                // remove it automagically after 5 seconds
                setTimeout(function () { StackExchange.notify.close(classid);}, 5000);
            });
        }
    }

    StackExchange.ready( function() {
        if (hash.length > 0) {
            cleanHash = hash.substring(1);
            if (cleanHash.indexOf('comment') === 0) {
                ids = cleanHash.substring(7).split('_');
                var lnk = document.getElementById('comments-link-' + ids[1]);
                if (lnk !== null) {
                    StackExchange.comments.loadAll(lnk).done(function() {
                        showMessage(document.getElementById('comment-'+ ids[0]), 'comment');
                    });
                } else {
                    showMessage(document.getElementById('comment-'+ ids[0]), 'comment');
                }
            // Make sure the hash is not the ID of the question.
            } else if (window.location.pathname.indexOf('/questions/' + cleanHash) !== 0) {
                if (!isNaN(Number.parseInt(cleanHash, 10))) {
                    showMessage(document.getElementById('answer-' + cleanHash), 'answer');
                }
            }
        }
    });

}(document || unsafeWindow.document, window || unsafeWindow, StackExchange || unsafeWindow.StackExchange ));

Or find it in the GitHub repo where you're invited to submit pull requests.

6
  • 1
    That thumbnail tho. Commented May 21, 2017 at 14:33
  • 2
    @BhargavRao I'm not known for my artistic skills .. :(
    – rene
    Commented May 21, 2017 at 14:34
  • 1
    we all know that flowers can't draw, @rene :/ Commented May 21, 2017 at 14:36
  • Just used this recently and it's useful when fighting Smokey's posts! Just a small request, is it possible to show the message using StackExchange.notify.show("message") instead? Any opinion/reason?
    – Andrew T.
    Commented Oct 21, 2017 at 11:57
  • 1
    @AndrewT. I just figured out how I can use that without sacrificing functionality. With you in the next 6 to 8 hours.
    – rene
    Commented Oct 21, 2017 at 12:23
  • 1
    @AndrewT. the version which uses notify is now live in the github repo.
    – rene
    Commented Oct 21, 2017 at 20:39

0

You must log in to answer this question.

Browse other questions tagged .