Userscripts.org - Inline Fans List

By pwlin Last update Jun 25, 2009 — Installed 318 times. Daily Installs: 0, 0, 0, 0, 2, 0, 0, 2, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 2, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 2, 0, 0

There are 24 previous versions of this script.

// ==UserScript==
// @name        Userscripts.org - Inline Fans List
// @version 	0.5.9
// @namespace	http://userscripts.org/scripts/show/35370
// @description	See the list of fans in the same script page
// @include		http://userscripts.org/scripts/show/*
// @include 	http://userscripts.org/topics/*
// @include		http://userscripts.org/scripts/discuss/*
// @include		http://userscripts.org/scripts/reviews/*
// @include		http://userscripts.org/scripts/review/*
// @include		http://userscripts.org/scripts/issues/*
// @include		http://userscripts.org/scripts/admin/*
// @include		http://userscripts.org/scripts/edit/*
// @include		http://userscripts.org/scripts/edit_src/*
// @include		http://userscripts.org/scripts/upload/*
// @include		http://userscripts.org/scripts/images/*
// ==/UserScript==

var $uso = {
	pre_load_fans : false , 
	_html_root : '//html//body//div[@id="root"]//div[@id="content"]',  
	_fans_anchor_obj : null, 
	_fans_link : null, 
	_fans_count : null, 
	_fans_content_obj : null , 
	
	
	init: function(){
		try { 
			this._fans_anchor_obj = $xpath(this._html_root+'//ul[@id="script-nav"]//li[contains(*/text(),"Fans")]//a').snapshotItem(0) ;  
			this._fans_anchor_obj.innerHTML = '<img src="'+this.images.arrow+'"/> '+this._fans_anchor_obj.innerHTML;
			this._fans_link = this._fans_anchor_obj.href ; 
			this._fans_count = $xpath(this._html_root+'//ul[@id="script-nav"]//li[contains(*/text(),"Fans")]//span[1]').snapshotItem(0).innerHTML ;
			this._fans_anchor_obj.setAttribute('onclick','return false;');
			this._fans_anchor_obj.setAttribute('onfocus','this.blur();');
			this._fans_anchor_obj.addEventListener('click', $uso.toggle_fans_button , false ) ;  
			this.create_fans_content_obj();
			if(this.pre_load_fans){
				this.xmlhttp(this._fans_link, this.parse_response) ;
			}
		} catch(e) { /* console.log(e)  ; */  } 
	} , 
	
	create_fans_content_obj : function(){
		var div_scriptinfo_obj = $xpath(this._html_root+'//ul[@id="script-nav"]').snapshotItem(0) ;		
		var div_fans_content_obj = document.createElement("div");
		div_fans_content_obj.setAttribute('style','padding:0 5px 5px 5px; margin-bottom:0.9em; display:none; font-size:13px; border-bottom:1px dotted #999999;');
		div_fans_content_obj.setAttribute('id','fans_content') ; 
		div_fans_content_obj.innerHTML = '<b>Loading fans....</b>';
		div_scriptinfo_obj.parentNode.insertBefore(div_fans_content_obj, div_scriptinfo_obj.nextSibling);
		this._fans_content_obj = $xpath(this._html_root+'//div[@id="fans_content"]').snapshotItem(0) ; 
	} , 
	
	toggle_fans_button : function(){
		$uso._fans_content_obj.style.display == 'none'  ? $uso.show_fans_content() : $uso.hide_fans_content() ; 
	} , 
	
	show_fans_content : function(){
		$xpath(this._html_root+'//ul[@id="script-nav"]//li[contains(*/text(),"Fans")]').snapshotItem(0).setAttribute('style','border-bottom: 1px dotted #999999;');
		$uso._fans_anchor_obj.innerHTML = (''+$uso._fans_anchor_obj.innerHTML+'').replace($uso.images.arrow, $uso.images.arrow_down) ; 
		$uso._fans_content_obj.style.display = '' ; 
		if ($uso._fans_content_obj.innerHTML == '<b>Loading fans....</b>') {
			if (($uso._fans_count).match(/^0$/)) { 
				$uso._fans_content_obj.innerHTML = '<b>No fans for this script</b>'; 
			}
			else {
				$uso.xmlhttp($uso._fans_link, $uso.parse_response) ;
			}
		}		
	},
	
	hide_fans_content : function() {
		$xpath(this._html_root+'//ul[@id="script-nav"]//li[contains(*/text(),"Fans")]').snapshotItem(0).removeAttribute('style');
		$uso._fans_anchor_obj.innerHTML = (''+$uso._fans_anchor_obj.innerHTML+'').replace($uso.images.arrow_down, $uso.images.arrow) ;  
		$uso._fans_content_obj.style.display = 'none';
	},
	
	parse_response: function(response) {
		var fav_container = '' ; 
		try{
			fav_container = response.match(/<p class="favorite">(.*)[\s\S]+this script<\/a><\/p>/ig)[0];
		}
		catch(e) { }
		var pagination = response.match(/<div class="pagination"(.*?)[\s\S]+<ul/) ;
		response = response.match(/<ul style="-moz-column-count: 4; list-style: none(.*?)[\s\S]+<\/ul>/);
		$uso._fans_content_obj.innerHTML = '<div id="fans">'+fav_container+'</div><b>Fans for this script:</b>'+response[0]; 
		if (pagination){
			pagination = (''+pagination[0]+'').replace(/&laquo;|&raquo;|<ul$/g,'') ;
			var parser = new DOMParser();
			var dom = parser.parseFromString(pagination, "text/xml");
			if(dom){
				dom = dom.documentElement ;
				var anchors = dom.getElementsByTagName('a') ; 
				for(a in anchors){
					var url = anchors[a].getAttribute('href') ; 
					if(anchors[a].getAttribute('class') != 'next_page' && url){
						$uso.xmlhttp('http://userscripts.org'+url+'', $uso.parse_pagination) ;
					}
				}
			}
			dom = null ; 
			parser = null ; 
		}
	} ,
	
	parse_pagination: function(response){
		response = response.match(/<ul style="-moz-column-count: 4; list-style: none(.*?)[\s\S]+<\/ul>/);
		$uso._fans_content_obj.innerHTML += '<hr/>'+response[0]; 
	},
	
	images : {
		arrow : 'data:image/gif;base64,R0lGODlhCQAJAJEAAP///5mZmQAAAAAAACH5BAUUAAAALAAAAAAJAAkAAAIRDI4nlroNhGRIzmSp04offxQAOw==',
		arrow_down : 'data:image/gif;base64,R0lGODlhCQAJAJEAAP///5mZmQAAAAAAACH5BAUUAAAALAAAAAAJAAkAAAIQhI+pGuIvAmhQGhpTs3qpAgA7'
	},
	
	xmlhttp : function(myurl,func) { GM_xmlhttpRequest({ method:"GET", url:myurl, headers:{ "User-Agent": navigator.userAgent }, onload: function(response) { func(response.responseText) ; } }) ; }	
}

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