Jump to content

User:Lupo/monobook.js

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Lupo (talk | contribs) at 20:35, 16 January 2015. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
if (window.mediaWiki && window.mediaWiki.loader) {
  // Kill some scripts that I find annoying.
  window.mediaWiki.loader.load = (function (original_loader) {
    var ignoreScripts = /^ext\.(UserBuckets|articleFeedback)/;
    return function (modules, type) {
      if (typeof (modules) === 'string') {
        if (ignoreScripts.test (modules)) return false;
      } else if (modules.length) {
        for (var i=0; i < modules.length; i++) {
          if (ignoreScripts.test (modules[i])) {
            modules = modules.splice(i, 1); i--;
          }
        }
        if (modules.length === 0) return false;
      }
      return original_loader.call (this, modules, type);
    };
  })(window.mediaWiki.loader.load);
}

(function(jQuery, mw){

// No "analytics" tracking, please.
mw.track = mw.trackSubscribe = function() {};
if (mw.eventLog) mw.eventLog.sendBeacon = function(url) {};

function cfg(variable) { return mw.config.get(variable); }

if (cfg('wgUserName') !== 'Lupo') return;

if ((/\.(js|css)$/.test(cfg('wgTitle')) || (cfg('wgNamespaceNumber') === 828)) && (cfg('wgAction') === 'edit' || cfg('wgAction') === 'submit') && cfg('wgNamespaceNumber') % 2 === 0) {
	mw.loader.load('ext.codeEditor');
}

var today  = new Date ();
var months = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function formatDate (d, with_timezone) {
	if (!with_timezone) {
		return months[d.getMonth()] + ' ' + d.getDate() + ', ' + d.getFullYear();
	} else {
		var tzRaw = d.getTimezoneOffset();
		var tzHour = Math.floor( Math.abs(tzRaw) / 60);
		var tzMin = Math.abs(tzRaw) % 60;
		var tzString = ((tzRaw >= 0) ? "-" : "+")
				+ ((tzHour < 10) ? "0" : "")
				+ tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
		return formatDate (d, false) + ' (' + tzString + ')';
	}
}

function makeRawLink(text, href, target, tooltip) {
	var link = document.createElement('a');
	link.href = href;
	if (target) link.target = target;
	if (tooltip) link.title = tooltip;
	link.appendChild(document.createTextNode(text));
	return link;
}

function makeWikiLink(text, page, target, tooltip) {
	return makeRawLink(text, cfg('wgArticlePath').replace('$1', page), target, tooltip);
}

function makeClickLink (name, handler) {
	var link = makeRawLink (name, '#');
	jQuery(link).on('click', handler);
        return link;
}

if ((/\.(js|css)$/.test(wgTitle) || cfg('wgNamespaceNumber') === 828) && (cfg('wgAction') === 'edit' || cfg('wgAction') === 'submit')) {
	window.mediaWiki.loader.load('ext.codeEditor');
}

importScript("MediaWiki:Gadget-ImageAnnotator.js");

window.hotcat_use_category_links = true;

importScript("User:Ilmari Karonen/edittoolstest.js");
window.testJsEdittools = true;

/*** General functions: setting the edit summary */

function editSummary (text)
{
	var wpSummary = document.getElementById('wpSummary');
	var summary = wpSummary.value;
	if (!summary) {
		wpSummary.value = text;
	} else {
		summary = summary.substr(summary.lastIndexOf('*/') + 2).replace(/^\s+|\s+$/g, "");
		wpSummary.value += (summary && summary.length > 0 ? (" & " + text.substr(0, 1).toLowerCase () + text.substr(1)) : text) ;
	}
}

/*------------------------------------------------------------------------------------------
  Edit lead section link
  ------------------------------------------------------------------------------------------*/

/*** Edit section 0 link */
function addLeadSectionTab () {
	/* Only add new tab if the page is editable and has section editing links. */
	if (!jQuery('.mw-editsection').length) return;
	var edit_tab = document.getElementById ('ca-edit');
	if (!edit_tab) return;
	var href_for_lead = edit_tab.firstChild.getAttribute ('href') + "&section=0";
	var first_title   = jQuery('h1.firstHeading');
	if (!first_title || !first_title.length) return;
	// We have a "firstHeading": put "[edit]" to its right
	var edit_span = document.createElement ('span');
	edit_span.setAttribute ('class', 'mw-editsection');
	edit_span.appendChild (document.createTextNode (' ['));
	edit_span.appendChild (makeRawLink ("edit", href_for_lead));
	edit_span.appendChild (document.createTextNode (']'));
	first_title[0].appendChild (edit_span);
}

function setLeadSectionEditSummary () {
	// The edit summary is set to "Lead section" if we're editing only the lead section of
	// an article. This is the case iff the URL of the document ends in "&section=0".
	if ((document.location.href.indexOf ("&section=0") + 10 == document.location.href.length) &&
		document.editform && document.editform.wpSummary !== null)
	{
		document.editform.wpSummary.value = "/* Lead section */ ";
	}
}

/*------------------------------------------------------------------------------------------
  Personal toolbox
  ------------------------------------------------------------------------------------------*/

function myPortlet() {
	var leftcolumn = document.getElementById ('column-one');
	if (!leftcolumn) return;
	/* Put together a new portlet and insert it at the top of the left column. */
	/* Note: \xa0 is &nbsp; and \xa9 is &copy; */
	var newportlet = document.createElement('div');
	var content    = document.createElement('div');

	newportlet.className = 'portlet';
	newportlet.id = 'p-lupo-links';
	newportlet.style.direction = 'ltr';
	content.className = 'pBody';
	content.style.direction = 'ltr';

	content.appendChild (document.createTextNode('ML: '));
	var mailLink = 'http://lists.wikimedia.org/pipermail';
	var mailSuffix = '' + today.getFullYear () + '-' + months[today.getMonth()] + '/thread.html#end';
	content.appendChild (makeRawLink('(en)', mailLink + '/wikien-l/' + mailSuffix, '_blank'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeRawLink('(c)', mailLink + '/commons-l/' + mailSuffix, '_blank'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeRawLink('(a)', mailLink + '/wikitech-ambassadors/' + mailSuffix, '_blank'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeRawLink('(t)', mailLink + '/wikitech-l/' + mailSuffix, '_blank'));
	content.appendChild (document.createElement('br'));

	content.appendChild (document.createTextNode('Logs: '));
	content.appendChild (makeWikiLink ('B', 'Special:Log/block'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('D', 'Special:Log/delete'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('I', 'Special:Log/upload'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('M', 'Special:Log/move'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('U', 'Special:Log/newusers'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('N', 'Special:Contributions/newbies'));
	content.appendChild (document.createElement('br'));

	content.appendChild (makeWikiLink ('AN', 'WP:AN'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('CPM', 'Wikipedia:How_to_fix_cut_and_paste_moves'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('PUI', 'WP:PUI'));
	content.appendChild (document.createElement('br'));

	content.appendChild (makeWikiLink ('news', 'Wikipedia:Wikipedia_Signpost'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('FA', 'WP:FAC'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('FP', 'WP:FPC'));
	content.appendChild (document.createElement('br'));

	content.appendChild (makeWikiLink ('\xa9vios', 'WP:CP'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('\xa9tags', 'Wikipedia:Image_copyright_tags'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('PD', 'WP:PD'));
	content.appendChild (document.createElement('hr'));

	content.appendChild (makeClickLink ('v', function (evt) {
		var textbox = document.getElementById('wpTextbox1');
		if (textbox) {
			textbox.value += '\n\nDo not [[Wikipedia:Vandalism|vandalize]] or you will be prevented from editing. ~~' + '~~';
			editSummary('stop vandalizing');
		}
		evt.preventDefault();
    }));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeClickLink ('#r', function (evt) {
		var textbox = document.getElementById('wpTextbox1');
		if (textbox) {
			textbox.value += '#REDIRECT [[]]';
			editSummary('redir');
			textbox.focus();
		}
		evt.preventDefault();
    }));
	content.appendChild (document.createTextNode('\xa0'));
	content.appendChild (makeClickLink ('hi', function (evt) {
		var textbox = document.getElementById('wpTextbox1');
		if (textbox) {
			textbox.value += '\n\n{' + '{subst:User:Lupo/Welcome}' + '} ~~' + '~~';
			editSummary('Welcome');
			textbox.focus();
		}
		evt.preventDefault();
    }));
	content.appendChild (document.createElement('hr'));

	content.appendChild (makeWikiLink ('rechts', 'User:Lupo/rechts'));
	content.appendChild (document.createTextNode('\xa0'));
	content.appendChild (makeRawLink ('(edit)', '/enwiki/w/index.php?title=User:Lupo/rechts&action=edit'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeRawLink ('css', '/enwiki/w/index.php?title=User:Lupo/monobook.css&action=edit'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeRawLink ('js', '/enwiki/w/index.php?title=User:Lupo/monobook.js&action=edit'));

	var title=document.createElement('h3');
	title.style.textTransform = 'none';
	title.appendChild (document.createTextNode (formatDate (today, false)));
	newportlet.appendChild(title);
	newportlet.appendChild(content);
	
	// Insert the newly created portlet in the left column.
	leftcolumn.insertBefore (newportlet, leftcolumn.firstChild);
}

/*------------------------------------------------------------------------------------------
  Other customizations: interwikis to top, bottom tabs
  ------------------------------------------------------------------------------------------*/

function languageAtTop () {
  /* Take the #p-lang (if there is one) out of the .portlet sidebar and put it
     physically into the main column. */
  var contents  = document.getElementById('column-content');
  var langlinks = document.getElementById('p-lang');

  if (langlinks) {
    langlinks.parentNode.removeChild (langlinks);
    contents.insertBefore(langlinks, contents.firstChild);
  }
}

function bottomTabs() {
  /* Duplicate the top "tabs" at the end of the content area. */
  var tabs = document.getElementById('p-cactions').cloneNode(true);
  /* Give all the named items new ids to avoid id clashes with the existing top "tabs". */
  tabs.id = 'mytabs';
  var listitems = tabs.getElementsByTagName('LI');
  for (i=0;i<listitems.length;i++) {
    if(listitems[i].id) listitems[i].id = 'mytabs-' + listitems[i].id;
  }
  document.getElementById('column-content').appendChild(tabs);
}

function topTabsToRightPlace() {
  /* Remove the top "tabs" from the .portlet side column (why were they ever put there?) and
     put them at the top of the content area, where they belong! (This allows me to use simple
     relative positioning to get a proper layout. I don't have to mess around with absolute
     positioning. This is necessary for my language links at the top to work properly, but it
     is cleaner anyway. */
  var contents  = document.getElementById('column-content');
  var tabs      = document.getElementById('p-cactions');

  tabs.parentNode.removeChild (tabs);
  contents.insertBefore(tabs, contents.firstChild);
}

function rmLogo() {
  /* Physically remove the logo. My CSS doesn't display it anyway, but I don't know whether
     that is sufficient to prevent the browser loading it. */
  var logo = document.getElementById('p-logo');

  logo.parentNode.removeChild (logo);
}

/*------------------------------------------------------------------------------------------
  Bug fixes
  ------------------------------------------------------------------------------------------*/

/* http://bugzilla.wikimedia.org/show_bug.cgi?id=2831 */

function hash_name_exists (hash_name) {
  if (!hash_name) return false;
  /* Check if there is an element with that id */
  if (!document.getElementById (hash_name)) return true;

  /* If the hash starts with a non-alphanumeric character, check if there is an anchor with a
     matching name (the "name" attribute is not an ID...) */
  if (/^\W.*/.test (hash_name)) {
    var anchors = document.getElementsByTagName ('a');
    for (i = 0; i < anchors.length; i++) {
      var name = anchors[i].getAttribute ('name');
      if (name === hash_name) return true;
    }
  }
  return false;
}

function bug2831Workaround () {
  /* Hash has a '#' at the beginning */
  if (!window.location.hash) return;
  var hash_name = window.location.hash.substring (1);
  var has_5B = /\.5[BD]/.test (hash_name);
  if (!has_5B &&
      (hash_name.indexOf ('.7C') <= 0))
    return;                                     // None of the critical characters
  if (hash_name_exists (hash_name)) return;     // Anchor exists

  var new_hash = null;
  if (!has_5B) {
    /* Only try replacing .7C (pipe) */
    new_hash = hash_name.replace (/^.*\.7C(.*)/, '$1');
    if (!hash_name_exists (new_hash)) {
      new_hash = null;
    }
  } else {
    /* First try replacing all */
    new_hash = hash_name.replace(/\.5[BD]/g, '');
    if (!hash_name_exists (new_hash)) {
      /* Next, try replacing more selectively */
      new_hash = hash_name.replace (/\.5B\.5B(.*)\.5D\.5D/g, '$1');
      if (new_hash == hash_name)
        new_hash = hash_name.replace (/\.5B\.5B(.*?)(\.5D\.5D)?/g, '$1');
      if (!hash_name_exists (new_hash)) {
        /* Try removing interwikis etc. */
        if (new_hash.charAt (0) == ':') {
          new_hash = new_hash.substring (1);
          if (!hash_name_exists (new_hash)) {
            if (/^[a-z]+:/.test (new_hash)) {
              new_hash = new_hash.substring(new_hash.indexOf (':')+1);
              if (!hash_name_exists (new_hash)) new_hash = null;
            } else
              new_hash = null;
          }
        } else
          new_hash = null;
        if (new_hash) {
          /* Try to account for piped links */
          new_hash = hash_name.replace (/\.5B\.5B.*\.7C(.*)\.5D\.5D/g, '$1');
          if (!hash_name_exists (new_hash)) {
            /* http links with display text */
            new_hash = hash_name.replace(/\.5B(?:http|ftp):.*?_(.+)\.5D/g, '$1');
            if (!hash_name_exists (new_hash)) {
              /* Try the same with a greedy match (i.e., last underscore) */
              new_hash = hash_name.replace(/\.5B(?:http|ftp):.*_(.+)\.5D/g, '$1');
              if (!hash_name_exists (new_hash)) {
                new_hash = null;
              }
            }
          }
        }
      }
      /* Yes, I know the above regular expressions will fail if there is more than one link
         in the section title. Any idea to correct that? Note that anyway, there are still
         a few cases that aren't caught. Some cannot be caught at all (with reasonable effort);
         try a section heading like "[http://some.example.org]"! */
    }
  }
  if (new_hash != null) window.location.hash = '#' + new_hash;
}

function reformatMyPage() {
  rmLogo();
  addLeadSectionTab();
  setLeadSectionEditSummary();
  myPortlet();
  bottomTabs();
  topTabsToRightPlace();  /* First the top "tabs"... */
  languageAtTop();        /* ... and then the language links */
  /* The order is important here: first move the tabs, then the language links, otherwise they
     will end up in the wrong order (tabs above language links). */
  bug2831Workaround();
}

jQuery(document).ready(reformatMyPage);

})(window.jQuery, window.mediaWiki);