User:Mdale/wikitrust.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Mdale/wikitrust. |
/* 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(/>/g,'>').replace(/</g,'<').replace(/"/g,'"').replace(/&/g,'&').replace(/'/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);
}
}