Skip to content

Instantly share code, notes, and snippets.

@hotchpotch
Created January 16, 2009 11:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hotchpotch/47901 to your computer and use it in GitHub Desktop.
Save hotchpotch/47901 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name hatena diarylist otsune mode
// @namespace http://rails2u.com/
// @description 観測範囲が狭い人向け
// @include http://d.hatena.ne.jp/diarylist
// ==/UserScript==
(function() {
const CONTAINER_XPATH = '//div/ul[@class="list-plain"]';
const LIST_XPATH = '//div/ul[@class="list-plain"]/li';
const OTSUNES = 20;
var container = $X(CONTAINER_XPATH)[0];
var head = document.getElementsByTagName('h1')[0];
// head.getElementsByTagName('span')[0].style.display = 'inline';
var ary = [];
var merge = function (newList) {
if (ary.length)
allAddElements(ary);
var list = $X(CONTAINER_XPATH + '/li[not(preceding-sibling::*)]', container.wrappedJSObject);
var timestamp = listToTS(list[0]);
for (var i = 0; i < newList.length; i++) {
var li = newList[i];
if ( listToTS(li) > timestamp ) {
ary.push( li );
} else {
break;
}
}
if (ary.length) {
addElements(ary);
}
reloadOstunes(OTSUNES);
}
var otsune = function() {
var img = document.createElement('img');
img.style.paddingLeft = '3px';
img.src = 'http://www.hatena.ne.jp/users/ot/otsune/profile_s.gif';
return img;
}
var reloadOstunes = function(num) {
for (var i = 0; i < num; i++) {
head.appendChild(otsune());
}
}
var removeOtsune = function() {
var imgs = head.getElementsByTagName('img');
if (imgs.length) {
var i = imgs[0];
i.parentNode.removeChild(i);
return true;
} else {
return false;
}
}
var listToTS = function(li) {
return parseInt([li.firstChild.nodeValue].join('').replace(/[^\d]/g, ''));
}
var addElements = function (els) {
var wait = (Math.max(1, (OTSUNES - 8)) * 1000) / els.length;
var w = 0;
var funcs = [];
if (els.length) {
var func = function() {
if (els.length) {
unshiftChild(container, els.pop());
w += wait;
if (els.length)
setTimeout(func, w);
}
};
setTimeout(func, w);
}
}
var allAddElements = function (els) {
var el;
while((el = els.pop()))
unshiftChild(container, el);
}
var unshiftChild = function (elem, child) {
if (elem.firstChild) {
elem.insertBefore(child, elem.firstChild);
} else {
elem.appendChild(child);
}
}
var nowLoading = false;
var load = function() {
nowLoading = true;
GM_xmlhttpRequest({
method : 'GET',
url : location.href,
onload : function(res) {
var doc = document.createElement('div');
doc.innerHTML = res.responseText;
//var list = $X(LIST_XPATH, doc.wrappedJSObject ? doc.wrappedJSObject.document : doc);
var list = doc.getElementsByClassName('list-plain')[0].getElementsByTagName('li')
merge(list);
nowLoading = false;
}
});
}
reloadOstunes(OTSUNES);
var timer = function() {
if (!nowLoading && !removeOtsune()) {
load();
}
}
setInterval(timer, 1000);
// http://gist.github.com/3238
function $X (exp, context, type /* want type */) {
if (typeof context == "function") {
type = context;
context = null;
}
if (!context) context = document;
var exp = (context.ownerDocument || context).createExpression(exp, function (prefix) {
var o = document.createNSResolver(context)(prefix);
if (o) return o;
return (document.contentType == "application/xhtml+xml") ? "http://www.w3.org/1999/xhtml" : "";
});
switch (type) {
case String: return exp.evaluate(context, XPathResult.STRING_TYPE, null).stringValue;
case Number: return exp.evaluate(context, XPathResult.NUMBER_TYPE, null).numberValue;
case Boolean: return exp.evaluate(context, XPathResult.BOOLEAN_TYPE, null).booleanValue;
case Array:
var result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (var ret = [], i = 0, len = result.snapshotLength; i < len; i++) {
ret.push(result.snapshotItem(i));
}
return ret;
case undefined:
var result = exp.evaluate(context, XPathResult.ANY_TYPE, null);
switch (result.resultType) {
case XPathResult.STRING_TYPE : return result.stringValue;
case XPathResult.NUMBER_TYPE : return result.numberValue;
case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
// not ensure the order.
var ret = [], i = null;
while (i = result.iterateNext()) ret.push(i);
return ret;
}
return null;
default: throw(TypeError("$X: specified type is not valid type."));
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment