Skip to content

Instantly share code, notes, and snippets.

@cho45
Created January 20, 2009 13:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cho45/49465 to your computer and use it in GitHub Desktop.
Save cho45/49465 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Hatena: Ignore Ids
// @description Remove the id from our world.
// @namespace http://lowreal.net/
// @include http://d.hatena.ne.jp/*
// @include http://h.hatena.ne.jp/*
// @include http://h1beta.hatena.ne.jp/*
// @include http://b.hatena.ne.jp/*
// @include http://q.hatena.ne.jp/*
// @require http://svn.coderepos.org/share/lang/javascript/jsdeferred/trunk/jsdeferred.userscript.js
// @require http://gist.github.com/3238.txt#$X
// @require http://gist.github.com/49453.txt#createHTMLDocument
// @require http://gist.github.com/3239.txt#createElementFromString
// ==/UserScript==
(function () { with (D()) {
//
$E = createElementFromString;
function getCachedResource (uri, expire, convertfun) {
var d = Deferred();
var key = uri;
var v = {};
try { v = eval(GM_getValue(key, "({})")) || {} } catch (e) { log("parse error: may be uneval bug"); v = {}; }
d.clear = function () {
GM_setValue(key, "");
return this;
};
if (v.time && v.time > (new Date).getTime() - expire) {
log("Cache Hitted: " + key);
next(function () { d.call(v.body); });
} else {
log("Cache expired; getting... " + key);
GM_xmlhttpRequest({
method : "GET",
url : uri,
headers : {
},
onload : function (req) { try {
var res = convertfun(req.responseText);
GM_setValue(key, uneval({time:(new Date).getTime(), body:res}));
log(key, uneval({time:(new Date).getTime(), body:res}));
log("Cached: " + key);
d.call(res);
} catch (e) { d.fail(e) } },
onerror : function (e) {
d.fail("HTTPError:"+e);
}
});
}
return d;
}
getCachedResource("http://b.hatena.ne.jp/my.name", duration("1 day"), function (text) {
return eval("(" + text + ")").ignores_regex.split("|");
}).
next(function (ignore_users) {
function isIgnored (id) {
return (" " + ignore_users.join(" ") + " ").indexOf(" " + id + " ") != -1;
}
if (location.hostname == 'h1beta.hatena.ne.jp' || location.hostname == 'h.hatena.ne.jp') {
var hide_entries = function () {
$X(".//div[@class='entry'][contains(' id:" + ignore_users.join(" id:") + " ', concat(' ', .//span[@class='username']/a/@title,' '))]", Array).forEach(function (e) {
e.parentNode.removeChild(e);
});
};
hide_entries();
unsafeWindow.Hatena.Haiku.Pager.addEventListener('loadedEntries', hide_entries);
}
// ダイアリを 404 に
if (location.hostname == "d.hatena.ne.jp") {
var id = location.pathname.split("/")[1];
if (isIgnored(id)) {
document.documentElement.innerHTML = "";
return xhttp.get("http://d.hatena.ne.jp/hatena404/").next(function (req) {
document.documentElement.innerHTML = req.responseText;
});
}
}
// 人力検索のコメントを非表示に
if (location.hostname == "q.hatena.ne.jp") {
$X(".//div[a[contains('/" + ignore_users.join("/") + "', @href)]] | .//div[a[contains('/" + ignore_users.join("/") + "', @href)]]/following-sibling::div[1]", document.body, Array).forEach(function (e) {
e.parentNode.removeChild(e);
});
}
// 該当ユーザがつけたはてなスターを非表示に
// var selector = "img[alt='" + ignore_users.join("'].hatena-star-star ,\nimg[alt='") + "'].hatena-star-star";
// $E(
// [
// "<style type='text/css'>",
// "#{selector} {",
// "display: none !important",
// "}",
// "</style>"
// ].join("\n"), {
// parent: document.getElementsByTagName('head')[0],
// data : {
// selector: selector
// }
// }
// );
return null;
}).
error(function (e) {
log(e);
});
} // end with
function createDocumentFromString (s) {
s = String(s).replace(/<script[ \t\r\n<>][\S\s]*?<\/script(?:[ \t\r\n]*>|[ \t\r\n]+)|<(?:i?frame|html|object|script)(?:[ \t\r\n][^<>]*(?:>|(?=<))|[ \t\r\n]*>)|<\/(?:i?frame|html|object|script)(?:[ \t\r\n]*>|[ \t\r\n]+)/gi, '');
var d = createHTMLDocument();
if (d) {
var child, root = d.documentElement;
while ((child = root.firstChild)) root.removeChild(child);
var r = d.createRange();
r.selectNodeContents(root);
root.appendChild(r.createContextualFragment(s));
return d;
} else {
throw "createHTMLDocument failed.";
}
}
function duration (str) {
var ret = 0, map = {
sec : 1, min : 60, hour : 3600, day : 86400, week : 604800, month : 2592000, year : 31536000
};
str.replace(/(\d+)\s*(msec|sec|min|hour|day|week|month|year)s?/g, function (_, num, unit) {
ret += +num * map[unit];
});
return ret * 1000;
}
function log () {
var o = Array.prototype.concat.apply([], arguments);
if (unsafeWindow.console) {
unsafeWindow.console.log(o.join(", "));
}
if (typeof(GM_log) != "undefined") {
GM_log(o);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment