User:Galil/gwiki-tools.js

/* == GuildWiki tools == This script is used together with Greasemonkey to enhance GuildWiki's  (gw.gamewikis.org) functionality. For more information, please see: http://gw.gamewikis.org/wiki/User:Galil/GWiki-tools Original author: Johan Sköld (aka User:Galil) johan.skold@gmail.com */ var IMG_HeaderLoading = "data:image/gif;base64,R0lGODlhDAAMAPcaAHl5d66urMXFw3l5dpSUk5WVlKOjoq+vrsbGw6Sko7u7uaWlpbm5t3h4doiIhtLSz4aGhJaWlsbGxNHRzrCwr5SUkqKiobq6uNHRz4eHhf///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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Zpg40MJBgRAuahx44MBGDQAABAjwccCAjxoVaHww8ILLgxhiDmQgUIAAgRM22rwYEAA7"; var xmlhttp = new XMLHttpRequest; var loc = location.href; // Helper functions function addEvent(obj, evType, fn, useCapture){ if (obj.addEventListener){ obj.addEventListener(evType, fn, useCapture); return true; } else if (obj.attachEvent){ var r = obj.attachEvent("on"+evType, fn); return r; } } function html_specialchar_decode(inp) { var outp = inp.replace(/&quot;/i, '"'); outp = outp.replace(/&lt;/i, '<');  outp = outp.replace(/&gt;/i, '>');  outp = outp.replace(/&amp;/i, '&');  return outp; } function popup (name, addr, style) {  window.open(addr, name,style); } // Full Size Image functions function fullSizeImage {  var fsImage = document.evaluate("//div[@class='fullImageLink' and @id='file']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;  if (fsImage) {    if (fsImage.childNodes[0].nodeName.toUpperCase == 'A') {      return fsImage;    } else {      return null;    }  } else {    return null;    } } function fsImage_Click {  var fsImage = document.getElementById('fsImage_gwikitools');  if (fsImage) {    if (fsImage.getAttribute('zoomed') == 'false') {      fsImage.childNodes[0].setAttribute('src', fsImage.getAttribute('image_addr'));      fsImage.childNodes[0].setAttribute('width', fsImage.getAttribute('image_width')); fsImage.childNodes[0].setAttribute('height', fsImage.getAttribute('image_height')); fsImage.setAttribute('zoomed', 'true'); } } } // Faster Watchpage adding functions function ToggleWatch(url) { xmlhttp.open('HEAD', url, true); xmlhttp.onreadystatechange = function { var watchLink = document.getElementById('watchLink_gwikitools'); if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { if (watchLink.getAttribute('action') == 'add') { var watchUrl = watchLink.getAttribute('url').replace(/&action=watch$/i, '&action=unwatch'); watchLink.childNodes[1].nodeValue = 'Unwatch'; watchLink.parentNode.setAttribute('id', 'ca-unwatch'); watchLink.setAttribute('action', 'remove'); watchLink.setAttribute('title', 'Remove this page from your watchlist [alt-w]'); } else { var watchUrl = watchLink.getAttribute('url').replace(/&action=unwatch$/i, '&action=watch'); watchLink.childNodes[1].nodeValue = 'Watch'; watchLink.parentNode.setAttribute('id', 'ca-watch'); watchLink.setAttribute('action', 'add'); watchLink.setAttribute('title', 'Add this page to your watchlist [alt-w]'); }       watchLink.setAttribute('backup-caption', watchLink.childNodes[1].nodeValue); watchLink.setAttribute('url', html_specialchar_decode(watchUrl)); document.getElementById('loadingImg_gwikitools').style.display = 'none'; } else { if (error) { if (watchLink.getAttribute('action') == 'add') var watchAction = 'adding'; else var watchAction = 'removing'; if (confirm('An error occured while '+watchAction+' this page to your watchlist.\n\nWould you like to visit the page manually instead?')) { location.href = watchLink.getAttribute('url'); } else { watchLink.childNodes[1].nodeValue = watchLink.getAttribute('backup-caption'); document.getElementById('loadingImg_gwikitools').style.display = 'none'; }       }      }    }  }  xmlhttp.send(null); } function watchLink_Click { var watchLink = document.getElementById('watchLink_gwikitools'); if (watchLink.getAttribute('url').length > 0) { watchLink.setAttribute('backup-caption', watchLink.childNodes[1].nodeValue); if (watchLink.getAttribute('action') == 'add') watchLink.childNodes[1].nodeValue = ' Adding'; else if (watchLink.getAttribute('action') == 'remove') watchLink.childNodes[1].nodeValue = ' Removing'; document.getElementById('loadingImg_gwikitools').style.display = ''; ToggleWatch(watchLink.getAttribute('url')); } } // Del.icio.us functions function deliciousLink_Click { popup('delicious', 'http://del.icio.us/post?v=4&noui&jump=close&url='+encodeURIComponent(loc)+'&title='+encodeURIComponent(document.title), 'toolbar=no,width=700,height=400'); return false; } //=================================================// // Main work //=================================================// //Image Scaling if (loc.match(/Image:.+?\..{3,4}$/i)) { var fsImage = fullSizeImage; if (fsImage) { if (fsImage.childNodes.length == 4) { addEvent(fsImage.childNodes[0],'click',fsImage_Click,true); addEvent(fsImage.childNodes[3],'click',fsImage_Click,true); var imagesize = fsImage.childNodes[3].childNodes[0].nodeValue.match(/([0-9]+)x([0-9]+)/); fsImage.childNodes[0].setAttribute('id', 'fsImage_gwikitools'); fsImage.childNodes[0].setAttribute('zoomed', 'false'); fsImage.childNodes[0].setAttribute('image_addr', fsImage.childNodes[0].href); fsImage.childNodes[0].setAttribute('image_width', imagesize[1]); fsImage.childNodes[0].setAttribute('image_height', imagesize[2]); fsImage.childNodes[0].href='javascript:void'; fsImage.childNodes[3].href='javascript:void'; fsImage.childNodes[3].childNodes[0].nodeValue = fsImage.childNodes[3].childNodes[0].nodeValue.replace(/Download high resolution version/, 'Click to display this image in fullsize'); } } } // Faster Watchpage adding var watchLink = document.evaluate("//li[@id='ca-watch' or @id='ca-unwatch']/a", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; if (watchLink) { if (watchLink.parentNode.getAttribute('id') == 'ca-watch') watchLink.setAttribute('action', 'add'); else watchLink.setAttribute('action', 'remove'); watchLink.setAttribute('url', watchLink.href); watchLink.setAttribute('id', 'watchLink_gwikitools') var loadingImg = document.createElement('img'); loadingImg.setAttribute('id', 'loadingImg_gwikitools'); loadingImg.setAttribute('src', IMG_HeaderLoading); loadingImg.style.display = 'none'; watchLink.insertBefore(loadingImg, watchLink.childNodes[0]); addEvent(watchLink,'click',watchLink_Click,true); watchLink.href = 'javascript:void'; } // External links in new window var eLinks = document.evaluate("//a[@class='external text']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); for (var i = 0; i < eLinks.snapshotLength; i++) eLinks.snapshotItem(i).target = '_blank'; // Del.icio.us var ulItems = document.evaluate("//div[@id='p-cactions' and @class='portlet']/ul", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; if (ulItems) { var deliciousListItem = document.createElement('li'); deliciousListItem.setAttribute('id', 'ca-delicious'); var deliciousLink = document.createElement('a'); deliciousLink.setAttribute('href', 'javascript:void'); deliciousLink.style.cursor = 'pointer'; deliciousLink.innerHTML = 'del.icio.us'; deliciousListItem.appendChild(deliciousLink); ulItems.appendChild(deliciousListItem); addEvent(deliciousLink,'click',deliciousLink_Click,true); }