// ==UserScript==
// @name           twittertitlechanger
// @namespace      http://oquno.com/
// @description    add username to twitter favorites' title
// @include        http://twitter.com/*/favorites*
// ==/UserScript==
(function(){

	// get username
	var username = location.href.match(/.com\/([^\/]+)/);

	// rewrite page title
	document.title = "Twitter / " + username[1] + "'s favorites";

	// get user's icon and set as favicon
	function loaded(xreq)
	{
		var rdoc = document.createElement("div");
		rdoc.innerHTML = xreq.responseText;
		rdoc.style.display = 'none';
		document.body.appendChild(rdoc);
		var icon = document.evaluate("//h2//img[@id='profile-image']", 
			document, null, 7, null);
		if(icon.snapshotLength == 0) 
			return;
		icon = icon.snapshotItem(0);
		var links = document.getElementsByTagName('link');
		for(i=0; i<links.length; i++){
			var link = links[i];
			if (link.rel == "shortcut icon"){
				/* thanks to os0x! http://oquno.com/log/eid1947.html#comments */ 
			       var f = link.cloneNode(false);
			       f.href = icon.src;
			       var head = link.parentNode;
			       head.removeChild(link);
			       head.appendChild(f);
			       break;
			}
		}
	}
	
	var xreq = new XMLHttpRequest();
	xreq.open('GET', 'http://twitter.com/' + username[1] , true);
	xreq.onreadystatechange = function()
	{
		if(xreq.readyState == 4)
			loaded(xreq);
	}
	xreq.send(null);

})();
