Created
June 25, 2012 22:44
-
-
Save mountainpenguin/2991923 to your computer and use it in GitHub Desktop.
sh-ignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ignores = null; | |
var replacementtext = "<td align='left' bgcolor='444444'>This post has been ignored</td>"; | |
$(document).ready( function () { | |
getIgnores(); | |
hideIgnores(); | |
bindUsernames(); | |
}); | |
function getIgnores() { | |
// use localStorage | |
if (!supports_html5_storage()) { | |
// looks like this won't work for us | |
ignores = new Array(); | |
} | |
ignores_ = localStorage.getItem("sh_ignores") | |
if (!ignores_) { | |
ignores = new Array(); | |
} else { | |
ignores = JSON.parse(ignores_); | |
if (!ignores) { | |
ignores = new Array(); | |
} | |
} | |
} | |
function bindUsernames() { | |
$("tr > td").find("b > font").each( function () { | |
addLinksTo($(this)); | |
}); | |
} | |
function addLinksTo(elem) { | |
var username = elem.html(); | |
var lnk = $("<a id='sh-ignore-" + username + "' href='#' />"); | |
if (ignores.indexOf(username) !== -1) { | |
// add unignore link | |
lnk.html("Unignore"); | |
} else { | |
// add ignore link | |
lnk.html("Ignore"); | |
} | |
lnk.on("click", function (evt) { | |
evt.preventDefault(); | |
// ignore or unignore | |
var ignoreornot = $(this).html(); | |
var un = $(this).attr("id").split("sh-ignore-")[1]; | |
if (ignoreornot == "Ignore") { | |
if (confirm("Ignore " + un + "?")) { | |
ignore(un); | |
refresh(); | |
} | |
} else { | |
if (confirm("Unignore " + un + "?")) { | |
unignore(un); | |
refresh(); | |
} | |
} | |
}); | |
lnk.appendTo(elem.closest("td")); | |
} | |
function refresh() { | |
window.location.replace(window.location); | |
} | |
function unignore(un) { | |
var idx = ignores.indexOf(un); | |
if (idx !== -1) { | |
ignores.splice(idx, 1); | |
localStorage.setItem("sh_ignores", JSON.stringify(ignores)); | |
} | |
} | |
function ignore(un) { | |
ignores.push(un); | |
localStorage.setItem("sh_ignores", JSON.stringify(ignores)); | |
} | |
function ignore_check(username, isStaff) { | |
if (!username) { | |
return false; | |
} | |
if (isStaff == true) { | |
return false; | |
} | |
if (ignores.indexOf(username) !== -1) { | |
return true; | |
} | |
return false; | |
} | |
function doIgnore(td2) { | |
var repl = $(replacementtext); | |
var showspan = $("<span />").html("show") | |
.css({ | |
"color": "white", | |
"cursor": "pointer", | |
"padding-left": "1em" | |
}) | |
.bind("mouseenter", function () { | |
$(this).css("text-decoration", "underline"); | |
}) | |
.bind("mouseleave", function () { | |
$(this).css("text-decoration",""); | |
}) | |
.click( function () { | |
if ($(this).hasClass("shown")) { | |
$(this).next().css({ | |
"display":"none", | |
"visibility":"hidden" | |
}); | |
$(this).removeClass("shown"); | |
$(this).html("show"); | |
} else { | |
$(this).next().css({ | |
"display":"block", | |
"visibility":"visible", | |
}); | |
$(this).addClass("shown"); | |
$(this).html("hide"); | |
} | |
}) | |
.appendTo(repl); | |
var hiddenspan = $("<span />").html(td2.html()).css({ | |
"display" : "none", | |
"visibility" : "hidden", | |
"color":"white", | |
"margin-top":"2px", | |
"border-top":"1px solid white", | |
"padding-top":"2px" | |
}).appendTo(repl); | |
repl.css({ | |
"color" : "red" | |
}); | |
td2.replaceWith(repl); | |
} | |
function hideIgnores() { | |
$("tr").each( function () { | |
var tds = $("td", $(this)); | |
var td1 = $(tds[0]); | |
var td2 = $(tds[1]); | |
// get username | |
var username = td1.find("b > font").html(); // username is the only element in bold | |
var isStaff = new Boolean(td1.find("u").html()); // staff position names are variable, but they're always underlined | |
if (ignore_check(username, isStaff)) { | |
// go ahead and ignore this one | |
doIgnore(td2); | |
} | |
}); | |
} | |
function supports_html5_storage() { | |
try { | |
return 'localStorage' in window && window['localStorage'] !== null; | |
} catch (e) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment