Sort Tumblr Followings by Last Update

By cxx Last update Nov 4, 2008 — Installed 140 times. Daily Installs: 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

There are 3 previous versions of this script.

// ==UserScript==
// @name           Sort Tumblr Followings by Last Update
// @namespace      http://cxx.tumblr.com/
// @include        http://www.tumblr.com/following
// @version        0.1.3
// ==/UserScript==

function xpath(xpathText) {
	return document.evaluate(
		xpathText, document, null,
		XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null
	);
}

var nodes = xpath('id("following")/li');
var users = [];
var node;
var mul = {
	second: 1,
	minute: 60,
	hour:   60 * 60,
	day:    60 * 60 * 24,
	week:   60 * 60 * 24 * 7,
	month:  60 * 60 * 24 * 30
};

while (node = nodes.iterateNext()) {
	var html = '<li>' + node.innerHTML + '</li>';
	var last;
	if (html.match(/<strong>(\d+) (\w+?)s? ago<\/strong>/m))
		last = RegExp.$1 * mul[RegExp.$2];
	else
		last = 60 * 60 * 24 * 365;
	users.push({ html: html, last: last });
}

users.sort(function(a,b){ return a.last - b.last; });
users.forEach(function(val,idx){ users[idx] = val.html; });
xpath('id("following")').iterateNext().innerHTML = users.join('\n');