Source for "Digg User Topics Cloud"

By pwlin
Has 8 other scripts.


// ==UserScript==
// @name           Digg User Topics Cloud
// @namespace      http://userscripts.org/scripts/show/23843
// @description    Draw A Topics Cloud From User's Recent Dugg Stories
// @include        http://digg.com/users/*
// @include        http://www.digg.com/users/*
// ==/UserScript==

/*
--------------------------------------------
Start user configuration, change as you wish:
--------------------------------------------
*/
// do you want to see the topics count? (true/false):
var cloud_show_counts = true ;
// minimum color in hex (ex. '#114488') :
var cloud_min_color = '#114488' ;
// maximum color in hex(ex. '#83B46A') :
var cloud_max_color = '#83B46A' ;
// minimum font size in pixel (15) :
var cloud_min_font = 15 ;
// maximum font size in pixel (35) :
var cloud_max_font= 35 ;
// how to fetch the topics (can be either 'RSS' or 'API', default to RSS. Fetching RSS is much faster but not as accurate as API and sometimes returns no result, so change this according to your taste) :  
var cloud_fetch_type = 'RSS' ;
/*
---------------------------------------------
End user configuration.
---------------------------------------------
*/
	
var $cloud = {
	draw_cloud: function(doc){
		if ($cloud.fetch_type == 'RSS') {
			if (!$xpath($cloud.rss_root+'//item',doc).snapshotLength){
				$xpath($cloud.html_root+'//div[@class="sidebar"]//div[@id="cloud_container"]//div[@id="topics_cloud"]').snapshotItem(0).innerHTML = 'No data :-(' ;	
				return ; 
			}
		}
		else if ($cloud.fetch_type == 'API'){
			if (!$xpath($cloud.xml_root+'//story',doc).snapshotLength){
				$xpath($cloud.html_root+'//div[@class="sidebar"]//div[@id="cloud_container"]//div[@id="topics_cloud"]').snapshotItem(0).innerHTML = 'No data :-(' ;	
				return ; 
			}
		}
		var topics = [];
		if ($cloud.fetch_type == 'RSS') {
			for (var i = 0, k = $xpath($cloud.rss_root+'//item',doc).snapshotLength; i < k ; i++){
				if ($xpath($cloud.rss_root+'//item',doc).snapshotItem(i).getElementsByTagName('title')[0].textContent){
					var link_content = $xpath($cloud.rss_root+'//item',doc).snapshotItem(i).getElementsByTagName('link')[0].textContent ; 
					if (!link_content.match(/^\/users\//)){
						topics[i] = link_content.match(/^http:\/\/digg.com\/(.*)\//)[1] ;
						topics[i] = topics[i].replace(/\/(.*)/,'');
					}
				}	
			}
		}
		else if ($cloud.fetch_type == 'API'){
			for (var i = 0, k = $xpath($cloud.xml_root+'//story',doc).snapshotLength; i < k ; i++){
				topics[i] = ($xpath($cloud.xml_root+'//story',doc).snapshotItem(i).getElementsByTagName('topic')[0]).getAttribute('short_name') ;
			}
		}
			
		var topics_obj = $cloud.count_topics(topics), tail = $cloud.tail_walker(topics_obj), tags_start = parseInt((''+tail.valueOf()+'').match(/\d/))-1 , tags_end = tail.length-1 , color_start = $cloud.hex2RGB($cloud.min_color), color_end = $cloud.hex2RGB($cloud.max_color), colors = [] , txt_tmp = '' ;
		for(var topic in topics_obj){
			for (var i=0; i<3; i++) { colors[i] = $cloud.spread(color_start[i],color_end[i],topics_obj[topic]-tags_start,tags_end) ; }
			var font_size = $cloud.spread($cloud.min_font,$cloud.max_font,topics_obj[topic]-tags_start,tags_end) ;
			txt_tmp += $cloud.draw_single_topic(font_size,topic,topics_obj[topic],colors[0],colors[1],colors[2]);
		}
		if ($cloud.trim(txt_tmp) == '') { txt_tmp = 'No data :-(' ; }
		$xpath($cloud.html_root+'//div[@class="sidebar"]//div[@id="cloud_container"]//div[@id="topics_cloud"]').snapshotItem(0).innerHTML = txt_tmp ; 
	},
	
	spread : function(min_font,max_font,start,end){
		var m,n ; 
		if(min_font>max_font) { 
			m = (min_font-max_font)/Math.log(end); 
			n = min_font-Math.floor(Math.log(start)*m); 
		} 
		else { 
			m =(max_font-min_font)/Math.log(end); 
			n = Math.floor(Math.log(start)*m+min_font) ; 
		}
		return n ;
	},
	
	tail_walker : function(topics_obj){
		var tail = [];
		for (topic in topics_obj){
			if (!tail[topics_obj[topic]]) { tail[topics_obj[topic]] = [topics_obj[topic]] ; } 
		}
		return tail ;
	},
	
	draw_single_topic : function(font_size,topic,count,color1,color2,color3){
		if (topic == 'undefined') { return '' ; }
		var txt_tmp ='<span style="font-size:'+font_size+'px;"><a style="color:rgb('+color1+','+color2+','+color3+');text-decoration:none;" href="/'+topic+'">'+topic+'</a></span>';
		txt_tmp += ($cloud.show_counts == true) ? '<b style="font-size:9px;margin-left:2px;">('+count+')</b> ' : ' ' ;
		return txt_tmp ;
	},
	
	username : function(){
		return $xpath(this.html_root+'//div[@class="user-profile"]//div[@class="profile-header"]//input[@id="username"]').snapshotItem(0).getAttribute('value');	
	},
	
	count_topics: function(topics){
		var topics_obj = {}, i = topics.length, j;
		while( i-- ) {
			j = topics_obj[topics[i]];
			topics_obj[topics[i]] = j ? j+1 : 1;
		}
		return topics_obj; 
	},
	
	init : function(){
		var username = this.username();
		if ($xpath(this.html_root+'//div[@class="sidebar"]//ul[@class="stats"]').snapshotLength > 0 ){
			var cloud_container = document.createElement("div");
			cloud_container.setAttribute('id','cloud_container');
			cloud_container.innerHTML = '<div class="tab"><h3>Topics Cloud</h3></div><div id="topics_cloud" style="background:#F6F4EC none repeat scroll 0%;padding:6px;"><strong>Drawing topics cloud...</strong></div>';
			var fav_topics = $xpath(this.html_root+'//div[@class="sidebar"]//ol[@class="favorite-topics"]').snapshotItem(0) ; 
			(fav_topics.parentNode).insertBefore(cloud_container, fav_topics.nextSibling);
			if (this.fetch_type == 'RSS') {
				$ajax.get('http://digg.com/users/'+username+'/history.rss',$cloud.draw_cloud) ;
			}
			else if (this.fetch_type == 'API') {
				$ajax.get('http://services.digg.com/user/'+username+'/dugg?appkey='+encodeURIComponent('http://userscripts.org/scripts/show/23843')+'&count=50',$cloud.draw_cloud) ; 
			}
		}		
	},
	
	fetch_type : cloud_fetch_type == 'API' || cloud_fetch_type == 'RSS' ? cloud_fetch_type : 'RSS', 
	show_counts : cloud_show_counts == true || cloud_show_counts == false ? cloud_show_counts : true  ,
	min_color : cloud_min_color ? cloud_min_color : '#114488',
	max_color : cloud_max_color ? cloud_max_color : '#C76867',
	min_font : cloud_min_font ? cloud_min_font : 15 ,
	max_font: cloud_max_font ? cloud_max_font : 35,
	html_root : '//html//body//div[@id="container"]//div[@id="contents"]//div[@id="wrapper"]',
	rss_root : '//rss//channel', // used with RSS fetch type
	xml_root : '//stories', // used with API fetch type
	trim : function(str) { return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); },
	cutHex: function(hex){ return (hex.charAt(0)=="#") ? hex.substring(1,7) : hex},
	hex2RGB: function(strHex){ return  [(parseInt(($cloud.cutHex(strHex)).substring(0,2),16)),(parseInt(($cloud.cutHex(strHex)).substring(2,4),16)),parseInt(($cloud.cutHex(strHex)).substring(4,6),16)]; }
}

var $ajax = {
	get : function(url,callback){		
		GM_xmlhttpRequest({
			method: 'GET',
			url: url,
			headers:{
			 'User-Agent': navigator.userAgent 
			},
			onload: function(response) {
				var parser = new DOMParser();
				var doc = parser.parseFromString(response.responseText, 'application/xml');
				callback(doc);
			}
		});
	}
}

function $xpath(q,doc) { if (!doc || doc == '') {doc = document ; } return doc.evaluate(q, doc,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null); }

(function(){
	try{
		$cloud.init();
	}
	catch(e){}

})();