Skip to content

Instantly share code, notes, and snippets.

@Shog9
Created February 21, 2018 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shog9/a2be02ea98db2365080c88d4ea40544d to your computer and use it in GitHub Desktop.
Save Shog9/a2be02ea98db2365080c88d4ea40544d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name ElectionComments
// @description Make election comments less annoying to moderate: show deleted, permalinks, undeletion, confirmation-free deletion
// @author animuson, Shog9
// @namespace https://gh.stackoverflow.com/CoGro/ElectionMonitor
// @version 1.0
// @include http*://stackoverflow.com/election*
// @include http*://*.stackoverflow.com/election*
// @include http*://dev.stackoverflow.com/election*
// @include http*://askubuntu.com/election*
// @include http*://superuser.com/election*
// @include http*://serverfault.com/election*
// @include http*://mathoverflow.net/election*
// @include http*://*.stackexchange.com/election*
// ==/UserScript==
function with_jquery(f)
{
var script = document.createElement("script");
script.type = "text/javascript";
script.textContent = "(" + f.toString() + ")(jQuery)" + "\n\n//# sourceURL=ElectionComments.userscript";
document.body.appendChild(script);
}
with_jquery(function()
{
StackExchange.initialized.then(function()
{
var nominations = $("#mainbar table .post-text").parent("[id^=post-]");
nominations
.addClass("nomination")
.attr("postId", function() { return this.id.match(/post-(\d+)/)[1] });
var nominationIds = nominations.map(function() { return +this.id.match(/post-(\d+)/)[1] }).toArray();
// add proper permalinks for nominations
function AddPermalinks(postId)
{
var base = postId ? $("#post-" + postId) : $("#mainbar");
base.find(".comment-date>span").wrap(function()
{
var commentId = +$(this).parents(".comment").attr("id").replace("comment-", '');
var postId = +$(this).parents(".nomination").attr("postId");
var link = $(this).parents(".nomination").find(".post-menu a:first").attr("href").replace(/#.+$/, '');
return "<a href='" + link + "?tab=nomination#comment" + commentId + "_" + postId + "'></a>";
});
}
AddPermalinks();
// ...and also add support for showing proper permalinks
var commentLinkData = location.hash.match(/#comment(\d+)_(\d+)/);
if ( commentLinkData )
{
var commentId = +commentLinkData[1];
var postId = +commentLinkData[2];
var commentLink = $("#comment-" + commentId);
if ( commentLink.length )
highlightComment(commentId);
else
StackExchange.comments.loadAll($("#post-" + postId), "?includeDeleted=true").then(function() { highlightComment(commentId) });
}
function highlightComment(commentId)
{
var el = $("#comment-" + commentId);
if ( !el.length ) return;
el[0].scrollIntoView(true);
el.animate({ backgroundColor: "#c88" }, 3000, 'linear', function () { $(this).css('background-color', ''); });
}
// intercept comment loading to add permalinks
$(document)
.ajaxSuccess(function(event, XMLHttpRequest, ajaxOptions)
{
if (!/\/posts\/\d+\/comments/.test(ajaxOptions.url))
return;
var postId = +ajaxOptions.url.match(/\/posts\/(\d+)\/comments/)[1];
var showDeleted = /\?includeDeleted=true/.test(ajaxOptions.url);
setTimeout(function()
{
AddPermalinks(postId);
EnableQuickDelete(postId);
if ( showDeleted )
$('#comments-link-' + postId).find('.js-show-link, .js-link-separator, .js-deleted-separator, .fetch-deleted-comments').addClass('dno');
}, 1);
});
// find out how many (if any) deleted comments exist for each nomination
$.get("/admin/posts/issues/" + nominationIds.join(";"))
.then(function(issues)
{
issues = $(issues);
for (let nom of nominationIds)
{
let deletedComments = issues.find("a[href='/admin/posts/" + nom + "/comments']");
$("#post-" + nom + " .fetch-deleted-comments").text("show " + deletedComments.text());
}
});
// add option to load deleted comments
nominations.each(function()
{
var $comments = $(this).find('[id^=comments-link-]');
var postId = $comments.attr('id').replace('comments-link-', '');
$comments.append('<span class="js-deleted-separator">&nbsp;|&nbsp;</span><a class="fetch-deleted-comments comments-link red-mod-link">show deleted</a>');
});
// wire up deleted comment loading, undeletion
nominations
.on("click", ".fetch-deleted-comments", function()
{
StackExchange.comments.loadAll($(this), "?includeDeleted=true");
})
.on("click", ".undelete-comment", function()
{
var e = $(this);
var comment = e.closest('.comment');
var commentId = comment.attr('id').replace('comment-', '');
var postId = e.closest(".nomination").attr("postId");
if (e.is(':working') || (e.working(!0).addSpinnerAfter({'padding':'0 3px'}))) {
$.ajax({
'type': 'POST',
'url': '/admin/posts/' + postId + '/comments/' + commentId + '/undelete',
'dataType': 'html',
'data': {
'fkey': StackExchange.options.user.fkey
}
}).done(function(newComment){
comment.replaceWith(newComment);
AddPermalinks(postId);
EnableQuickDelete(postId);
}).fail(function(t, error){
e.parent().showErrorMessage(error || "An error occurred while trying to undelete.");
}).always(function(){
StackExchange.helpers.removeSpinner(), e.working(!1);
});
}
});
// wire up quick deletion (override built-in)
function EnableQuickDelete(postId)
{
var base = postId ? $("#post-" + postId) : $("#mainbar");
base.find(".comments .comment-delete:not(.quick-delete)")
.addClass("quick-delete")
.click(function(ev)
{
ev.preventDefault();
ev.stopImmediatePropagation();
var e = $(this);
var comment = e.closest('.comment');
var commentId = comment.attr('id').replace('comment-', '');
var postId = e.closest(".nomination").attr("postId");
$.post("/posts/comments/" + commentId + "/vote/10", {sendCommentBackInMessage: true, fkey:StackExchange.options.user.fkey}).then(function(results)
{
if ( !results.Success ) return;
comment.replaceWith(results.Message);
AddPermalinks(postId);
});
});
}
EnableQuickDelete();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment