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 08:04, 21 September 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://mammoth.dnip.net/mvWiki/extensions/MetavidWiki/skins/wikitrust.js.php?'+wgPageName+'">');
*/

var trustServer = 'http://enwiki-trust.cse.ucsc.edu/index.php';
var wikitrust_api_loc = 'http://enwiki-trust.cse.ucsc.edu/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);
}
function wiki_trust_callback(result){
	js_log(result);
	//@@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("got: " +  );
			do_sajax_request(result['query']['pages'][i]['revisions'][j]['*']);
			break;
		}
	}
	
	//do_sajax_request( [0]['revisions'][0]);
}
//uses the wikitrust_page global for content: 
function do_sajax_request(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 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);
}
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);          
   }
}