Skip to main content
1 of 8
rene
  • 2.8k
  • 2
  • 17
  • 35

Show a warning message for a deleted answer or comment

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.

// ==UserScript==
// @name         Check if comment and answer exist
// @namespace    http://meta.stackexchange.com/users/158100/rene
// @version      0.1
// @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 ($, window) {

    var hash = $(window.location).attr('hash'),
        msg = $('<div></div>')
                    .css('background-color','#CCFFCC')
                    .css('align','center')
                    .css('padding', '10px')
                    .css('text-align', 'center')
                    .hide();

    function showMessage(jqsel, text) {
        if (jqsel.length === 0) {
            $('div#header').append(
                msg
                .text('The ' + text + ' is not found. It may have been deleted')
                .fadeIn(2000)
                .fadeOut(5000));
        }
    }

    if (hash.length > 0) {
        cleanHash = hash.substring(1);
        if (cleanHash.indexOf('comment') === 0) {
            ids = cleanHash.substring(7).split('_');
            showMessage($('div#comments-' + ids[1]).find('tr#comment-'+ ids[0]), 'comment');
        } else {
            if (Number.parseInt(cleanHash, 10) !== NaN) {
                showMessage($('div#answer-' + cleanHash), 'answer');
            }
        }
    }
}($ || unsafeWindow.$, window || unsafeWindow ));

You can find it ready to be consumed by TamperMonkey or GreaseMonkey on the Github repository.

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.

Here is an Animated GIF to show the script in action

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

rene
  • 2.8k
  • 2
  • 17
  • 35