Jump to content

User:Mdale/wikitrust.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mdale (talk | contribs) at 22:03, 17 December 2007. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* to install put in User:UserName/monobook.js 
 * document.write('<script type="text/javascript" src="http://en.wikipedia.org/w/index.php?title=User:Mdale/wikitrust.js&action=raw&ctype=text/javascript">');
*/

var trustServer = 'http://enwiki-trust.cse.ucsc.edu/index.php';
var wikitrust_api_loc = 'http://gaston.cse.ucsc.edu:81/api.php';
var TdoLoad = true;

//only activate on "view" pages: 
if(wgAction=="view"){
	addOnloadHook(do_disp_trust_link);	
}
//do color trust()
function do_color_trust(){   
	   bodyContent =  document.getElementById('bodyContent')
       bodyContent.innerHTML = 'loading<blink>...</blink>';
       if(typeof sajax_init_object == 'undefined'){
       		if(TdoLoad)loadExternalJs('/skins-1.5/common/ajax.js');
       		TdoLoad=false;
       		//js_log('still not set loading');
       		setTimeout('do_color_trust()', 20);
       }else{
       		js_log('do sjax');
       		TdoLoad=true;
	       	do_get_trustpage();
       }          
}
/*
 * do the api call: 
 */
function do_get_trustpage(){
	url = wikitrust_api_loc + '?format=json&action=query' +
			'&callback=wiki_trust_callback'+ 
			'&titles='+wgPageName+'&prop=revisions&rvprop=content';
	js_log('loading: ' + url);
	loadExternalJs(url);
	//wiki_trust_callback(sample_returned);
}
function wiki_trust_callback(result){
	js_log('wiki_trust_callback');
	//@@todo check if its an error or bad query
	for(i in result['query']['pages']){
		for(j in result['query']['pages'][i]['revisions']){	
			js_log('path: result[\'query\'][\'pages\']['+i+'][\'revisions\']['+j+'][\'*\']');		
			get_preivew_html(result['query']['pages'][i]['revisions'][j]['*']);
			break;
		}
	}
	//do_sajax_request( [0]['revisions'][0]);
}
//uses the wikitrust_page global for content: 
function get_preivew_html(wikiturst_text){	
	   wikiturst_text = swap_trust_template_for_span_color(wikiturst_text);
	   a = sajax_init_object();
	   a.open("POST", '/w/index.php?title='+wgPageName+'&action=submit&live', true);
	   var Boundary = '--------p1415';
	   a.setRequestHeader('Content-Type', 'multipart/form-data; boundary='+Boundary);
	   var PostData = '--' + Boundary 
			+ '\nContent-Disposition: form-data; name="wpTextbox1"\n\n'
			+ wikiturst_text;
	   if (a.overrideMimeType) a.overrideMimeType('text/html');
	   js_log('send post data: ' + PostData);
	   a.send(PostData);
	   a.onreadystatechange = function(){
			if (a.readyState != 4) return;
			var html = a.responseText;
			html = html.replace(/&gt;/g,'>').replace(/&lt;/g,'<').replace(/&quot;/g,'"').replace(/&amp;/g,'&').replace(/&apos;/g,"'");			
			//get rid of the preview note: first <h2> to close </dvi> 
			var sp = html.indexOf('<h2>');
			var ep = html.indexOf('</div>');			
			html = html.substr(0,sp) + html.substr(ep);
			bodyContent.innerHTML = html;										
	};
}
function swap_trust_template_for_span_color(text){
	/*text = text.replace('/{{#trust:1|begin}}/g', '<span style="background-color:#FFD085;">');
	text = text.replace('/{{#trust:2|begin}}/g', '<span style="background-color:#FFD085;">');
	text = text.replace('/{{#trust:3|begin}}/g', '<span style="background-color:#FFD085;">');
	text = text.replace('/{{#trust:4|begin}}/g', '<span style="background-color:#FFD085;">');
	text = text.replace('/{{#trust:5|begin}}/g', '<span style="background-color:#FFD085;">');*/
	
	text = text.replace(/\{\{#trust:([0-9])\|begin\}\}/g, replace_callback);
	return text;
}
function replace_callback(trust_string){
	num = trust_string.substring(9,trust_string.indexOf('|',9));
	var c='';
	switch(num){
		case '0':c='FF8888';break;
		case '1':c='FF9999';break;
		case '2':c='FF9898';break;
		case '3':c='FFAAAA';break;
		case '3':c='FFA8A8';break;
		case '4':c='FFBBBB';break;
		case '5':c='FFB8B8';break;
		case '6':c='FFCCCC';break;
		case '7':c='FFC8C8';break;
		case '8':c='FFEEEE';break;
		case '9':c='FFE8E8';break;
		case '10':c='FFFFFF';break;
	}
	return '<span style="background-color:#'+c+';">';
}
// replace callback support for safari.
(function(){
  var default_replace = String.prototype.replace;
  String.prototype.replace = function(search,replace){
        // replace is not function
        if(typeof replace != "function"){
                return default_replace.apply(this,arguments)
        }
        var str = "" + this;
        var callback = replace;
        // search string is not RegExp
        if(!(search instanceof RegExp)){
                var idx = str.indexOf(search);
                return (
                        idx == -1 ? str :
                        default_replace.apply(str,[search,callback(search, idx, str)])
                )
        }
        var reg = search;
        var result = [];
        var lastidx = reg.lastIndex;
        var re;
        while((re = reg.exec(str)) != null){
                var idx  = re.index;
                var args = re.concat(idx, str);
                result.push(
                        str.slice(lastidx,idx),
                        callback.apply(null,args).toString()
                );
                if(!reg.global){
                        lastidx += RegExp.lastMatch.length;
                        break
                }else{
                        lastidx = reg.lastIndex;
                }
        }
        result.push(str.slice(lastidx));
        return result.join("")
  }
})();

function do_disp_trust_link(){		
	newLi = document.createElement('li');
	newLi.id = 'ca-trust';
var titlePos = document.URL.indexOf('/wiki/');
    if( titlePos !=-1 ){
       var page_title = document.URL.substr(titlePos+6); 
}
	newLi.innerHTML = '<a href="javascript:do_color_trust();" title="View Trust analysis">View Trust</a>';
	//newLi.innerHTML = '<a href="'+trustServer+page_title+'" onclick="do_color_trust();" title="View Trust analysis">View Trust</a>';

	lastLi = document.getElementById('ca-watch');
	lastLi.parentNode.appendChild(newLi);
}
/* 
 * this won't work in some versions of Safari
 * you have to do your document.writes before the page is done rendering..
 * ....so @@todo for safari: 
 * have the view_trust issue a page refresh with flag in get request:
 * and check window.location.search paramaters to do the remote request 
 * with a document.write insted of DOM injection. 
 */
function loadExternalJs(url){
   //js_log('load js: ' + url);
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);  
}
function js_log(string){
   if( window.console ){
        console.log(string); 
   }else{
   	 //disable alerts
     //alert(string);          
   }
}