User:Woganhemlock/library/js

''This page documents various JavaScript modifications I have found. I sometimes may use these in creating custom wiki designs and skin modifications, as well as adding in functionality to the wiki. Be aware several of these are violations of Wikia's Terms of Use. Got something I might like? Tell me!''

Bot contribs
Personal use only

Used to go to special:Contributions/username as a bot. This is useful for reverting edits so they don't appear in RecentChanges unless bots is set to yes. Good for reverting profanity, and other obscenities, or if there is a large amount to revert. function hiderollback { if(document.title.indexOf('User contributions')==-1) return; var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0]; var botlink = document.location.href; if(botlink.indexOf('?')==-1) { botlink += '?bot=1'; } else { botlink += '&bot=1'; } addlilink(tabs, botlink, '&bot=1', 'ca-bot'); } addOnloadHook(hiderollback)

function addlilink(tabs, url, name, id) { var na = document.createElement('a'); na.href = url; na.id = id; na.appendChild(document.createTextNode(name)); var li = document.createElement('li'); li.appendChild(na); tabs.appendChild(li); return li; }

Countdown timer
Creates a countdown timer. An example of this script in action can be seen at The Sims Wiki:Battles. Here's the js to work it (goes in common.js): // ************************************************** // Experimental javascript countdown timer (Splarka) // Version 0.0.3 // ************************************************** // // Usage example: // //  Only January 01 2007 00:00:00 PST until New years. // //  Javascript disabled.

function updatetimer(i) { var now = new Date; var then = timers[i].eventdate; var diff = count=Math.floor((then.getTime-now.getTime)/1000);

// catch bad date strings if(isNaN(diff)) { timers[i].firstChild.nodeValue = '** ' + timers[i].eventdate + ' **' ; return; }

// determine plus/minus if(diff<0) { diff = -diff; var tpm = 'T plus '; } else { var tpm = 'T minus '; }

// calcuate the diff var left = (diff%60) + ' seconds'; diff=Math.floor(diff/60); if(diff > 0) left = (diff%60) + ' minutes ' + left; diff=Math.floor(diff/60); if(diff > 0) left = (diff%24) + ' hours ' + left; diff=Math.floor(diff/24); if(diff > 0) left = diff + ' days ' + left timers[i].firstChild.nodeValue = tpm + left;

// a setInterval is more efficient, but calling setTimeout // makes errors break the script rather than infinitely recurse timeouts[i] = setTimeout('updatetimer(' + i + ')',1000); }

function checktimers { //hide 'nocountdown' and show 'countdown' var nocountdowns = getElementsByClassName(document, 'span', 'nocountdown'); for(var i in nocountdowns) nocountdowns[i].style.display = 'none' var countdowns = getElementsByClassName(document, 'span', 'countdown'); for(var i in countdowns) countdowns[i].style.display = 'inline'

//set up global objects timers and timeouts. timers = getElementsByClassName(document, 'span', 'countdowndate'); //global timeouts = new Array; // generic holder for the timeouts, global if(timers.length == 0) return; for(var i in timers) { timers[i].eventdate = new Date(timers[i].firstChild.nodeValue); updatetimer(i); //start it up  } } addOnloadHook(checktimers);

// ************************************************** // - end -  Experimental javascript countdown timer // ************************************************** And to activate it use something like this: New years is in January 01 2007 00:00:00 PST. Counter unavailable.

Collapsing Portlets - Monobook Script
This one uses quite a bit of code to work. Some in Common.js and some in Monobook.css. I haven't tried this for personal use but I assume you could make it work by placing the code to go in common.js in a monobook.js file since common files don't work the same way for user subpages. Anyway, here's the .js: // ================================================== // Collapsible Portlets (experimental) // ==================================================

function foldingPortlets { var portlets = getElementsByClassName(document.getElementById('column-one'),'div','portlet'); var portskip = ['p-personal', 'p-cactions', 'p-logo', 'ads-top-left']; var first=true;

for(var i=0;i<portlets.length;i++) { if(portskip.join(' ').indexOf(portlets[i].id)==-1) { var pd = portlets[i].getElementsByTagName('div')[0]; var ph = portlets[i].getElementsByTagName('h5')[0]; ph.className = 'portletCollapsible' pd.setAttribute('id','pbody-'+i); pd.style.display='none';

var link = document.createElement('a'); var head = getAllText(ph); while(ph.firstChild) ph.removeChild(ph.firstChild); link.appendChild(document.createTextNode(head)); link.setAttribute('href','javascript:showPortlet(\'' + i + '\');'); link.setAttribute('id','plink-'+i); link.className = 'portletClosed'; ph.appendChild(link);

if(first==true) { first=false; showPortlet(i); }   }  } } if(skin=='monobook') addOnloadHook(foldingPortlets)

function getAllText(thing) { if (thing.nodeType == 3) return thing.nodeValue; var text = new Array; var i=0; while(thing.childNodes[i]) { text[text.length] = getAllText(thing.childNodes[i]); i++; } return text.join(''); }

function showPortlet(id) { var pd = document.getElementById('pbody-'+id); var pl = document.getElementById('plink-'+id);

if(pd.style.display=='none') { pd.style.display='block'; pl.className = 'portletOpened'; } else { pd.style.display='none'; pl.className = 'portletClosed'; } }

// ================================================== // End Collapsible Portlets // ================================================== ...and here is the .css /* Classes for collapsible portlets, customize as needed */ h5.portletCollapsible { display:block; padding:0; height:auto; border:1px outset grey; } .portletCollapsible a { padding:1px 1px .2em 15px; display:block; text-decoration:none; color:black; } .portletClosed { background:center left url("http://images.wikia.com/common/skins/common/images/Arr_d.png") no-repeat; } .portletOpened { background:center left url("http://images.wikia.com/common/skins/common/images/Arr_u.png") no-repeat; }

Skin swapper
Possible ToU violation Adds a button for swapping between monobook and wikia, and, if it becomes supported, vector. /* skin change buttons */ function CreateSkinChangeButtons { //Oasis buttons $('div.buttons a:first-child').before('MonobookVector'); //Monobook buttons $('#p-cactions .pBody ul li:nth-last-child(1)').after('OasisVector'); } addOnloadHook(CreateSkinChangeButtons);

Kill Side Rail
Personal use only Remove the darned side rail and expand content area by adding a button. /* add a button that increases the content size and hides the rail */ function CreateContentResizeButton { var contentWidth = $('#WikiaMainContent').width; var catlinksWidth = $('#catlinks').width; var html = ' Expand <--> '; if(contentWidth < 1000) { if($('#WikiHeader .wikia-menu-button').length > 0) {$('#WikiHeader .wikia-menu-button').after(html);} if($('#WikiHeader .wikia-button').length > 0) {$('#WikiHeader .wikia-button').after(html);} if($('#WikiHeader .view-source').length > 0) {$('#WikiHeader .view-source').after(html);} if(wgCanonicalNamespace == 'User_blog') {$('#WikiaUserPagesHeader .wikia-button').after(html);} } } addOnloadHook(CreateContentResizeButton); function ExpandContent(contentWidth, catlinksWidth) { document.getElementById('WikiaMainContent').style.width = '1000px'; document.getElementById('catlinks').style.width = '1000px'; document.getElementById('WikiaRail').style.display = 'none'; $('#resizeButton a').replaceWith('<a onclick="CompressContent(' + contentWidth + ', ' + catlinksWidth + ');" data-id="resizeButton" style="color:#fff;" title="Compress the content area back to its original width, and restore the side rail."> Compress >--< </a>'); }

function CompressContent(contentWidth, catlinksWidth) { document.getElementById('WikiaMainContent').style.width = contentWidth; document.getElementById('catlinks').style.width = catlinksWidth; document.getElementById('WikiaRail').style.display = 'block'; $('#resizeButton a').replaceWith('<a onclick="ExpandContent(' + contentWidth + ', ' + catlinksWidth + ');" data-id="resizeButton" style="color:#fff;" title="Expands the content area. Note that this will hide the side rail."> Expand <--> </a>'); }

WikiActivity - RecentChanges
Personal use only

Swap out the WikiActivity button for one to RecentChanges. I'm not sure what exactly this will do on wikis with the new nav enabled, but it very much works on ones without it. /* change wiki activity to recent changes */ function WikiActivity2RecentChanges { $('.wikia-button[data-id$="wikiactivity"]').replaceWith('<a data-id="recentchanges" class="wikia-button secondary" accesskey="g" title="Special:RecentChanges" href="/wiki/Special:RecentChanges"><img height="0" width="0" class="sprite activity" src="http://images1.wikia.nocookie.net/common/skins/common/blank.gif">Recent Changes</a>'); } addOnloadHook(WikiActivity2RecentChanges);

AutoRefresh
Used as default on this wiki. Adds a check box to auto refresh recentchanges. Handy if you are a stalker. importScriptPage('AjaxRC/code.js', 'dev');

Hide stuff
Collapses and hides stuff. importScriptPage('ShowHide/code.js', 'dev'); See w:c:dev:ShowHide for more info.