Skip to content

Instantly share code, notes, and snippets.

@divinity76
Forked from mountainpenguin/sh-ignore.js
Created July 21, 2012 11:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save divinity76/3155471 to your computer and use it in GitHub Desktop.
sh-ignore
/*
forked from Mr. Pengi's, and added fallback to cookies if localStorage is not available,
also fixed a few exceptions that would rise if localStorage is not available
*/
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();
return;
}
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);
if(supports_html5_storage())
localStorage.setItem("sh_ignores", JSON.stringify(ignores));
}
}
function ignore(un) {
ignores.push(un);
if(supports_html5_storage())
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() {
if(localStorage in window && window['localStorage'] !== null)
{return true;}
//no localStorage here, emulating it with cookies
/*code from https://developer.mozilla.org/en/DOM/document.cookie (slightly modified) */
try {
window.localStorage = {
getItem: function (sKey) {
if (!sKey || !this.hasItem(sKey)) {
return null;
}
return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
vEnd = typeof (vEnd) === 'undefined' ? new Date(2027, 2, 3) : vEnd; //default to 2027
sPath = typeof (sPath) === 'undefined' ? '/includes2/includes/forum.php' : sPath; //default to slavehack forum
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/.test(sKey)) {
return;
}
var sExpires = "";
if (vEnd) {
switch (typeof vEnd) {
case "number":
sExpires = "; max-age=" + vEnd;
break;
case "string":
sExpires = "; expires=" + vEnd;
break;
case "object":
if (vEnd.hasOwnProperty("toGMTString")) {
sExpires = "; expires=" + vEnd.toGMTString();
}
break;
}
}
document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
},
removeItem: function (sKey) {
if (!sKey || !this.hasItem(sKey)) {
return;
}
var oExpDate = new Date();
oExpDate.setDate(oExpDate.getDate() - 1);
document.cookie = escape(sKey) + "=; expires=" + oExpDate.toGMTString() + "; path=/";
},
hasItem: function (sKey) {
return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
}
};
} catch (ee) { //error emulating localStorage...
return false;
}
return true; //emulated
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment