User:Falzar FZ/monobook.js

From Yugipedia
Jump to: navigation, search

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: hold down Ctrl and click the Refresh or Reload button. Firefox: hold down ⇧ Shift while clicking Reload (or press Ctrl+⇧ Shift+R). Google Chrome and Safari users can just click the Reload button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/* ========================================================================================================================== */


// First section contains code written by me, Falzar FZ, to make things more efficient for me and to reduce misclicks.



// Make each revision in 'History' pages go on a single line, regardless of length. So all the rollback buttons remain on the right. I usually misclick them if they wrapped around to the left.
if (wgAction == 'history')
  appendCSS ("#bodyContent ul   { white-space:nowrap; }");

// Same as above but for 'Contributions' pages. Also, shift the rollback links slightly right so they are on their own.
if (wgPageName == "Special:Contributions") {
  appendCSS ("#bodyContent ul   { white-space:nowrap; }");
  appendCSS (".mw-rollback-link { position:relative; left:50px; }");
}



// Add a few links to the Toolbox
addOnloadHook(
  function() {
    addPortletLink('p-tb', wgServer + '/wiki/User:Falzar_FZ/Sandbox', 'Sandbox');
    addPortletLink('p-tb', 'http://www.google.com/search?q=site%3Ayugioh.wikia.com%20Ruling%20Queries', 'Google search wikia');
    addPortletLink('p-tb', 'http://translate.google.com/#ja|en|', 'Google Translate');
  }
);



// There's probably an easier way, but since I already had the regular expressions on FoxReplace (Firefox browser add-on) (and that I didn't know it can be done on Javascript before), it's just a matter of copy/pasting and making the syntax compatible with Javascript (/ → \/).

// Add (hist | edit | delete | move) links to the list of pages at Special:WhatLinksHere.
if (wgPageName == "Special:WhatLinksHere") {
  addOnloadHook(
    function() {
      var findLinksWLH = /\<a href="\/wiki\/(.*?)" title=".*?"\>(.*?)\<\/a\>(.*)links\<\/a\>\)\<\/span\>/g;
      document.getElementById('mw-whatlinkshere-list').innerHTML = document.getElementById('mw-whatlinkshere-list').innerHTML.replace(findLinksWLH,'<a href="/wiki/$1" title=" ">$2</a> (<a href="/index.php?title=$1&amp;action=history" title=" ">hist</a> | <a href="/index.php?title=$1&amp;action=edit" title=" ">edit</a> | <a href="/index.php?title=$1&amp;action=delete" title=" ">delete</a> | <a href="/wiki/Special:MovePage/$1" title=" ">move</a>)$3links</a>)</span>');

      var rearrangeLinksWLH = /\<li\>\<a(.*?)a\>.\((.*?)\|(.*?)\|(.*?)\|(.*?)\)/g;
      document.getElementById('mw-whatlinkshere-list').innerHTML = document.getElementById('mw-whatlinkshere-list').innerHTML.replace(rearrangeLinksWLH,'<li>($2|$3|$4|$5) <a$1a>');
    }
  );
}

/* Breaks in some Categories for some reason.
// Add (edit) links to the list of pages in Categories.
if (wgCanonicalNamespace == "Category") {
  addOnloadHook(
    function() {
      var findLinksC  = /\<li\>\<a href="\/wiki\/(.*?)".*\>(.*?)\<\/a\>\<\/li\>/g;
      document.getElementById('mw-pages').innerHTML = document.getElementById('mw-pages').innerHTML.replace(findLinksC,'<li>(<a href="\/index.php?title=$1&action=edit" title=" ">edit<\/a>) <a href="\/wiki\/$1" title=" ">$2<\/a><\/li>');
    }
  );
}
*/

// Swap the rollback buttons with undo button.
if (wgAction == 'history') {
  addOnloadHook(
    function() {
      var swapRollUndo  = /\(\<span(.*?) \| \<span class="mw-history-undo"\>(.*?)\>undo\<\/a\>\<\/span\>\)/g;
      document.getElementById('pagehistory').innerHTML = document.getElementById('pagehistory').innerHTML.replace(swapRollUndo,'(<span class="mw-history-undo">$2>------------undo</a></span> | <span$1)');
    }
  );
}



// See hidden rows
  function displayHidden() {
    var rows = document.getElementsByTagName('tr');
    for (var i = 0, row; row = rows[i]; i++) {
      row.style.display = 'block';
    }
  }

// Show only diffs.
  function diffsOnly() {
      appendCSS (".diff-lineno, .diff-context { display: none !important; }");
  }
// After using this, when you copy highlighted content, only additions will be copied, markers and deleted lines will not be copied.
  function addsOnly() {
      appendCSS (".diff-marker, .diff-deletedline { -moz-user-select: none; }");
  }

// Key Press Handler
  onkeydown = keyHandler;
  function keyHandler(e) {
    var key = e.keyCode;
    // alert("Key code is: " + key);
    
    if (key == '65') { // a
      if (wgPageName == "Special:RecentChanges") {
        displayHidden();
      }
    } else if (key == '68') { // d
        diffsOnly();
    } else if (key == '70') { // f
        addsOnly();
    }

  }

// Because I'm lazy, automatically link files in delete reasons.
if (mw.util.getParamValue('action') == "delete") {
	$('#wpReason').bind("paste", function() {
		var vReason = $(this);
		setTimeout(function() {
			vReason.val(vReason.val().replace(/File:(.*?)\.(jpg|png)/gi, "[[:File:$1.$2]]"));
		}, 100);
	});
}

// Unlike deleting pages, the reason isn't automatically added when deleting files. Hack it so it does add it.
if (mw.config.get('wgCanonicalNamespace') == "File") {
	if (mw.util.getParamValue('action') == "delete") {
		addOnloadHook(
			function() {
				$('#wpReason').val(unescape(document.URL).replace(/File:(.*?)\.(jpg|png)/gi, "[[:File:$1.$2]]").match(/Reason for deletion.*?\(\)/));
			}
		);
	} else {
		$('#ca-delete').html($('#ca-delete').html().replace("action=delete","action=delete&reason=" + $('#delete').text().match(/Reason for deletion.*?\(\)/)));
	}
}

// Change "File - " back to "File:" (how it used to be).
if (mw.config.get('wgCanonicalNamespace') == "File") {
	$('#firstHeading').html($('#firstHeading').html().replace("File - ","File:"));
}

/* ========================================================================================================================== */

// <source lang="JavaScript">

// From Monchoman45

function CustomRollbacks() {
        var Buttons = [{
                text: 'vandalism',
                title: 'Revert vandalism',
                summary: 'Revert [[w:c:help:Help:Vandalism|vandalism]] by [[Special:Contributions/$2|$2]]',
                loadtalk: false
        }, {
                text: 'vb',
                title: 'Revert vandalism B',
                summary: 'Revert [[w:c:help:Help:Vandalism|vandalism]] by [[Special:Contributions/$2|$2]]&bot=1',
                loadtalk: false
        }, {
                text: 'b',
                title: 'Revert B',
                summary: 'Revert [[Special:Contributions/$2|$2]]&bot=1',
                loadtalk: false
        }, {
                text: 'unofficial',
                title: 'Revert unofficial',
                summary: 'Revert [[Special:Contributions/$2|$2]]. [[Forum:Page Format Reference%23CardRulings|Unofficial rulings are NOT allowed.]]',
                loadtalk: false
        }, {
                text: 'ex',
                title: 'EXACT quote',
                summary: 'Revert [[Special:Contributions/$2|$2]]. Must be an EXACT quote for it to be official.',
                loadtalk: false
        }];
 
        var list = document.getElementsByTagName('span');
        for(i in list) {
                if(list[i].className == 'mw-rollback-link') {
                        var link = list[i];
                        var rollback = link.innerHTML;
                        for(j in Buttons) {
                                var customlink = document.createElement('span');
                                customlink.innerHTML = rollback;
                                customlink.className = 'customrollback'; //we do this to avoid an infinite loop, because when we insert a <span> into the DOM, the list variable includes it
                                customlink.getElementsByTagName('a')[0].title = Buttons[j].title;
                                customlink.getElementsByTagName('a')[0].href += '&summary=' + Buttons[j].summary;
                                customlink.getElementsByTagName('a')[0].innerHTML = Buttons[j].text;
                                if(Buttons[j].loadtalk == true) {customlink.getElementsByTagName('a')[0].className = 'loadtalk';}
                                if(urlQuery('action') == 'history') {
                                        link.parentNode.insertBefore(customlink, link.nextSibling);
                                        customlink.outerHTML = ' | ' + customlink.outerHTML;
                                }
                                else {
                                        link.parentNode.appendChild(customlink);
                                        customlink.outerHTML = ' ' + customlink.outerHTML;
                                }
                        }
                }
        }
        for(i in list) { //why another one of these? we're fixing the class that we set to avoid an infinite loop
                if(list[i].className == 'customrollback') {
                        list[i].className = 'mw-rollback-link';
                }
        }

        /* Grunny's function for opening up a user's talkpage when rollbacking (http://community.wikia.com/wiki/User:Grunny) */
        $( '.mw-rollback-link .loadtalk' ).click( function (e) {
                e.preventDefault();
                var $rblink = $( this );
                $.ajax( {
                        url: $rblink.attr( 'href' ),
                        success: function () {
                                var     user = $rblink.attr( 'href' ).replace( /.*[&?]from=([^&]*).*/, '$1' ).replace( /\+/g, '_' ),
                                        newurl = wgServer + wgArticlePath.replace( '$1', 'User_talk:' + user + '?action=edit&section=new' );
                                location.href = newurl;
                        },
                        error: function () {
                                $rblink.text( function ( i, val ) {
                                        return val + ' [failed]';
                                } );           
                        }
                } );
        } );
}

addOnloadHook(CustomRollbacks);

/* Monchoman's function for fixing error that was occurring in Monobook */
function urlQuery(quer) {
	for(i in location.href.split('?')) {
		for(j in location.href.split('?')[i].split('&')) {
			if(location.href.split('?')[i].split('&')[j].split('=')[0] == quer) {
				return location.href.split('?')[i].split('&')[j].split('=')[1];
			}
		}
	}
	return undefined;
}

// </source>

/* ========================================================================================================================== */

// From http://dev.wikia.com/wiki/AjaxRC
importScriptPage('AjaxRC/code.js', 'dev');
var ajaxRefresh = 20000;

/* ========================================================================================================================== */

// Modified from http://community.wikia.com/wiki/Help:Custom_edit_buttons

if (mwCustomEditButtons) {
   mwCustomEditButtons[mwCustomEditButtons.length] = {
     "imageFile": "http://images.wikia.com/central/images/c/cb/Button_wikipedia.png",
     "speedTip": "wikipedia",
     "tagOpen": "[[wikipedia:",
     "tagClose": "]]",
     "sampleText": "link|link"};
 
   mwCustomEditButtons[mwCustomEditButtons.length] = {
     "imageFile": "http://images.wikia.com/central/images/2/23/Button_code.png",
     "speedTip": "Code",
     "tagOpen": "<code>",
     "tagClose": "</code>",
     "sampleText": "code"};
 
   mwCustomEditButtons[mwCustomEditButtons.length] = {
     "imageFile": "http://images.wikia.com/central/images/7/73/Button_code_nowiki.png",
     "speedTip": "Code + Nowiki",
     "tagOpen": "<code><nowiki>",
     "tagClose": "</nowiki></code>",
     "sampleText": "stuff"};
 
   mwCustomEditButtons[mwCustomEditButtons.length] = {
     "imageFile": "http://images.wikia.com/central/images/c/c9/Button_strike.png",
     "speedTip": "Grey Strike-through",
     "tagOpen": "<span style=\"color:#CCC;\"><s>",
     "tagClose": "</s></span>",
     "sampleText": "striked"};

   mwCustomEditButtons[mwCustomEditButtons.length] = {
     "imageFile": "http://images.wikia.com/central/images/7/74/Button_comment.png",
     "speedTip": "Comment visible only for editors",
     "tagOpen": "<!-- ",
     "tagClose": " -->",
     "sampleText": "Insert comment here"}
 }

/* ========================================================================================================================== */