Utilizador:DimeCadmium/dcutil.js

A UESPWiki – Sua fonte de The Elder Scrolls desde 1995

Nota: Após gravar, terá de limpar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Internet Explorer: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
  • Opera: Ir para Menu → Configurações (Opera → Preferências no Mac) e, em seguida, Privacidade e segurança → Limpar dados de navegação → Imagens e ficheiros em cache.
//Yeah, its crappy, so what.

/* USAGE:
importScript('User:DimeCadmium/dcutil.js');
addOnloadHook(function(){
  //override dcSet items as desired here, f.e.:
  dcSet['showWL'] = true;
  dcsetup();
});
*/

/* SETTINGS */
if (typeof dcSet === "undefined") {
  dcSet = {
    'showdate': true,

    'lightboximg': true, // boolean, enable/disable lightboxing of images

    'showWL': false, // boolean, enable/disable watchlist under options
    'maxWL': 5, // int, maximum # of watches to show
    'timeoutWL': 60, // int, (seconds) duration between watchlist reloads

    'showRC': true, // boolean, enable/disable recent changes under options
    'maxRC': 10, // int, maximum # of RC to show
    'timeoutRC': 60, // int, (seconds) duration between RC reloads
  
    'hideLogo': true, // hide logo
    'hideGPlus': true, // hide #p-googleplus
    'hideGeneral': true, // hide general portlet
    'hideSections': false, // hide sections portlet
    'hideCommunity': true, // hide community portlet
    'hideToolbox': true, // hide toolbox portlet

    'fixfirsttab': false, // rewrite first tab from namespace -> "article", like it should be.

    'modSections': {
      'activate': true, // boolean, enable/disable the following navpane modification
      'items': [ // array of strings, link (INCLUDING <a href></a>) to be put in nav.
                 // if an item is array, the first element will be used WITHOUT <li></li>.
        '<a href="/wiki/Main_Page">Main</a>',
        '<a href="/wiki/Lore:Main_Page">Lore</a>',
        '<span id="mwtoggle" onclick="dctoggle(\'mw\');">&gt;</span> <a href="/wiki/Morrowind:Morrowind">Morrowind</a><ul id="mwexps" style="display:none">'+
	        '<li><a href="/wiki/Tribunal:Tribunal">Tribunal</a></li>'+
	        '<li><a href="/wiki/Bloodmoon:Bloodmoon">Bloodmoon</a></li></ul>',
        '<span id="esotoggle" onclick="dctoggle(\'eso\');">&gt;</span> <a href="/wiki/Online:Online">ESO</a><ul id="esoexps" style="display:none">'+
                '<li><a href="/wiki/Online:Aldmeri_Dominion">AD</a></li>'+
                '<li><a href="/wiki/Online:Daggerfall_Covenant">DC</a></li>'+
                '<li><a href="/wiki/Online:Ebonheart_Pact">EP</a></li></ul>',
        ['<hr />'],
        '<a href="/wiki/Special:SpecialPages">Special</a>',
        '<a href="/wiki/Special:PrefixIndex/' + wgPageName + '">Subpages</a>',
      ],
    },
  };
}

/* CODE */
var idWLT, idRCT;
//addOnloadHook(doOnLoad);

function dctoggle(eid) {
  exp = document.getElementById(eid+'exps');
  tog = document.getElementById(eid+'toggle');
  if (exp.style.display == "none") {
    exp.style.display = "block";
    tog.innerHTML = "v";
  } else {
    exp.style.display = "none";
    tog.innerHTML = "&gt;";
  }
}

function doRCTimer() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET", "/w/api.php?action=query&list=recentchanges&format=json&rclimit="+dcSet['maxRC']+"&rcprop=ids|user|parsedcomment|timestamp|title|flags&rcshow=!bot", false);
  xmlhttp.send();
  var json = xmlhttp.responseText;
  var rcOut = eval('(' + json + ')');

  var str = "";
  for (var k in rcOut['query']['recentchanges']) {
    var ary = rcOut['query']['recentchanges'][k];
    var action   = ary['type'];
    var user     = ary['user'];
    var article  = ary['title'];
    var time     = ary['timestamp'].substring(11,19);
    var comment  = ary['parsedcomment'];
    var minor    = 'minor' in ary ? '<span style="color:#ff4500;font-weight:bold;">m</span> ' : '';
    if (comment == "") comment = "<em>(no comment)</em>";
    if (action == "edit") action = '<a href="/w/index.php?title='+article+'&diff='+ary['revid']+'&oldid='+ary['old_revid']+'" title="diff">edit</a>';
    str += '<li class="watchli"><a href="/wiki/'+article+'">'+article+'</a><br /><small>'+minor+comment+'<br />'+user+'<br />'+action+' at '+time+'</small></li>';
  }
  var now = new Date();
  now = now.getUTCHours()+":"+now.getUTCMinutes()+":"+now.getUTCSeconds();

  (document.getElementById('rcdiv')).innerHTML = "<small>"+now+"</small><br /><ul>"+str+"</ul>";

  if (typeof autoEdit !== "undefined") { autoEdit(); }
}

function doWLTimer() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET", "/w/api.php?action=query&list=watchlist&format=json&wllimit="+dcSet['maxWL']+"&wlprop=title|timestamp", false);
  xmlhttp.send();
  var json = xmlhttp.responseText;
  var wlOut = eval('(' + json + ')');

  var str = "";
  for (var k in wlOut['query']['watchlist']) {
    var title = wlOut['query']['watchlist'][k]['title'];
    var time  = wlOut['query']['watchlist'][k]['timestamp'];
    str += '<li class="watchli"><strong>'+title+'</strong><br /><small>'+time+'</small></li>';
  }
  (document.getElementById('watchdiv')).innerHTML = "<ul>"+str+"</ul>";

  if (typeof autoEdit !== "undefined") { autoEdit(); }
}



function dcsetup() {
  dchidestuff();

  if (dcSet['showdate']) {
    ((document.getElementById('p-personal').getElementsByClassName('pBody')[0]).getElementsByTagName('ul')[0]).innerHTML = "<li>"+esodate()+"</li>"+((document.getElementById('p-personal').getElementsByClassName('pBody')[0]).getElementsByTagName('ul')[0]).innerHTML;
  }

  if (dcSet['fixfirsttab']) {
    document.getElementById('p-cactions').getElementsByClassName('pBody')[0].getElementsByTagName('li')[0].getElementsByTagName('a')[0].innerHTML = "article";
  }

  if (dcSet['modSections']['activate']) {
    dcmodsections();
  }

  if (dcSet['showWL']) {
    idWLT = setInterval(doWLTimer, (dcSet['timeoutWL']*1000));
    (document.getElementById('column-one')).innerHTML += '<div id="p-wlist" class="portlet generated-sidebar"><h5>watchlist</h5><div class="pBody" id="watchdiv"><ul></ul></div></div>';
    doWLTimer();
  }
  if (dcSet['showRC']) {
    idRCT = setInterval(doRCTimer, (dcSet['timeoutRC']*1000));
    (document.getElementById('column-one')).innerHTML += '<div id="p-rclist" class="portlet generated-sidebar"><h5><a href="/wiki/Special:RecentChanges">rc</a> &bull; <a href="javascript:doRCTimer();void(0);">reload</a></h5><div class="pBody" id="rcdiv"><ul></ul></div></div>';
    doRCTimer();
  }

  if (dcSet['lightboximg']) {
    lightboximg();
  }
}

function dchidestuff() {
  if (dcSet['hideLogo']) {
    document.getElementById('p-logo').style.display = "none";
	document.getElementById('column-one').style.paddingTop = 0;
  }
  if (dcSet['hideGPlus']) {
    (document.getElementsByTagName('head')[0]).innerHTML += "<style>#p-googleplus { display:none; }</style>";
	// this one doesn't exist when the script is run.
  }
  if (dcSet['hideGeneral']) {
    document.getElementById('p-general').style.display = "none";
  }
  if (dcSet['hideSections']) {
    document.getElementById('p-sections').style.display = "none";
  }
  if (dcSet['hideCommunity']) {
    document.getElementById('p-community').style.display = "none";
  }
  if (dcSet['hideToolbox']) {
    document.getElementById('p-tb').style.display = "none";
  }
}

function dcmodsections() {
  var navLine = '<h5>navigation</h5><div class="pBody"><ul>';
  for (var item in dcSet['modSections']['items']) {
    if (dcSet['modSections']['items'][item] instanceof Array) {
      navLine += dcSet['modSections']['items'][item][0];
    } else {
      navLine += '<li>'+dcSet['modSections']['items'][item]+'</li>';
    }
  }

  (document.getElementById('p-sections')).innerHTML = navLine+'</ul></div>';
}

// The following is removed until rewritten.
/*
function doEdit(newCont) {
  var xhr = new XMLHttpRequest;
  xhr.open("GET", '/api.php?format=json&action=query&prop=info|revisions&intoken=edit&titles='+wgPageName, false);
  xhr.send();
  var respS = eval('('+xhr.responseText+')');
  var pgs = respS['query']['pages'];
  var tok;
  for (var key in pgs) {
    tok = pgs[key]['edittoken'];
  }
  tok = tok.substring(0, tok.length-2);
  tok += "%2B%5C";

  xhr = new XMLHttpRequest;
  xhr.open("GET", "/api.php?action=query&titles="+wgPageName+"&prop=revisions&rvprop=user|comment|content&rvlimit=1&format=xml", false);
  xhr.send();
  var currX = xhr.responseXML;
  var curr = (currX.getElementsByTagName('rev')[0]).textContent;

  xhr = new XMLHttpRequest;
  xhr.open("POST", "/api.php", false);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.send("action=edit&title="+wgPageName+"&section=0&text="+newCont+"\n\n"+curr+"&token="+tok+"&summary=test&minor=1&nocreate=1&watchlist=nochange");
}
*/


// Images in Lightbox - via http://uesp.net/wiki/User:Dominus_Arbitrationis
function lightboximg() {
  $('body').append('<div id="lightbox" style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.6);text-align:center;z-index:9999"></div>');
  $('a.image').click(function(e) {
    e.preventDefault();
      if ($(this).find('img').attr('src').match('/thumb/')) {
      	var src = $(this).find('img').attr('src').replace('/thumb','').replace(/\/[\-_.%\w]*$/, '');
      } else {
        var src = $(this).find('img').attr('src');
      }
    $('#lightbox')
      .html('<img src="'+src+'" style="background:#fff;box-shadow: 0 0 25px #111;max-height:100%;max-width:100%;vertical-align:middle;cursor:pointer;" />')
      .css('line-height', $(window).height()+'px')
      .fadeIn('fast')
      .live('click', function() {
        $(this).fadeOut('fast');
    });
  });
}

function esodate(date) {
	if (date == null)
		d = new Date();
	else
		d = date;
	days = ["Sundas","Morndas","Tirdas","Middas","Turdas","Fredas","Loredas"];
	months = ["Morning Star", "Sun's Dawn", "First Seed", "Rain's Hand", "Second Seed", "Midyear", "Sun's Height", "Last Seed", "Hearthfire", "Frostfall", "Sun's Dusk", "Evening Star"];
	suffix = ["st", "nd", "rd", "th"];
	return "Today is "+days[d.getDay()]+", "+d.getDate()+"<sup>"+suffix[(d.getDate()-1 > 3 ? 3 : d.getDate()-1)]+"</sup> of "+months[d.getMonth()];
}