User:Lupo/monobook.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
(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 && mw.config.get(variable) || !mw && window[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 makeActiveLink (name, script, tooltip) {
	return makeRawLink (name, 'javascript:' + script, null, tooltip);
}

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

function titleFromHref (href)
{
	if (!href) return "";
	var match = /[&?]title=([^&]*)/.exec (href);
	if (match && match.length > 1)
		return decodeURIComponent (match[1]).replace (/ /g, '_');
	var prefix = cfg('wgArticlePath').replace ('$1', "");
	if (href.indexOf (prefix) !== 0) prefix = cfg('wgServer') + prefix; // Fully expanded URL?
	if (href.indexOf (prefix) !== 0 && prefix.indexOf ('//') === 0) prefix = document.location.protocol + prefix; // Protocol-relative wgServer?
	if (href.indexOf (prefix) === 0)
		return decodeURIComponent (href.substring (prefix.length)).replace (/ /g, '_');
	return "";
}
 
function titleFromLink (node) {
	if (!node || typeof (node.getAttribute) != 'function') return "";
	return titleFromHref (node.getAttribute ('href', 2));
	// The ", 2" is for IE. IE sometimes has problems with encoded UTF-8 characters: using any other
	// way to get the href, it insists to decode the value, but does do so wrongly. For more info,
	// see commons.wikimedia.org/wiki/MediaWiki_talk:Gadget-HotCat.js/Archive01#Questions
}

if (cfg('wgUserName') === 'Lupo') {
	// importScript ('User:Lupo/c2.js'); // HotCat dev. version
	importScript ('User:Lupo/autocorrect.js');    // Auto-correction of common errors

	window.wgNotice=""; // Let's get rid of buggy centralnotices. I don't want them anyway.
	window.oldEdittools = true; // goddamn.
	window.ImageAnnotator_zoom_threshold = 3.0;
	window.UploadForm_own_language_first = true;
	window.UploadForm_page_preview_in_tooltip = true;
	window.AutoLoadLogsFilter = true; 
}

/*** 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 */ ";
	}
}

function makeImageLinks() {
	if (cfg('wgNamespaceNumber') !== 6) return [];
	
	var l = [];
	var title = cfg('wgTitle');
	if (title) {
		title = title.substr(title.indexOf(':') + 1).replace (/ /g, '_');
		var digits = title.match (/^(\d{6,10})[ _].*\.jpe?g$/i);
		if (digits) {
			l[l.length] = makeRawLink ('Flickr check...', 'http://www.flickr.com/photo.gne?id=' + digits[1], '_blank');
			l[l.length] = makeRawLink ('Pbase check...', 'http://www.pbase.com/image/' + digits[1], '_blank');			
		}
		var imgs = jQuery('div#file img');
		if (!imgs || !imgs.length) return l;
		for (var i = 0; i < imgs.length; i++) {
			var href = decodeURIComponent(imgs[i].getAttribute('src', 2)).replace ('%26', '&');
			if (href.indexOf('/' + title) >= 0) {
				var a = imgs[i].parentNode;
				if (a.nodeName.toLowerCase() === 'a') {
					href = encodeURIComponent(a.href);
					l[l.length] = makeRawLink ('Google search...', 'https://www.google.com/searchbyimage?image_url=' + href, '_blank');
					l[l.length] = makeRawLink ('Tineye search...', 'http://tineye.com/search?url=' + href, '_blank');
					break;
				}
			}
		}			
	}
	return l;
}

function stopMsg(evt) {
	var textbox = document.getElementById('wpTextbox1');
	if (textbox) {
		if (textbox.value && textbox.value.length > 0) textbox.value += '\n';
		textbox.value += 'Stop uploading images taken from other websites. If you continue to upload copyright violations, you will be blocked. ~~' + '~~';
	}
	var summary = document.getElementById('wpSummary');
	if (summary) summary.value = "Stop";
	evt.preventDefault();
}

/*** 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 ('DEL', 'COM:DEL'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('L', 'COM:L'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('AN', 'COM:AN'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('VP', 'COM:VP'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild (makeWikiLink ('Upload', 'Special:Upload?uselang=experienced'));
	content.appendChild (document.createElement('br'));
  
	if (cfg('wgUserName') === 'Lupo') {
		content.appendChild (makeWikiLink ('simple', 'User:Lupo/Simple Photographs'));
		content.appendChild (document.createTextNode('\xa0|\xa0'));
		content.appendChild (makeWikiLink ('hairy', 'User:Lupo/Hairy copyright'));
		content.appendChild (document.createTextNode('\xa0|\xa0'));
		content.appendChild (makeWikiLink ('bio', 'User:Lupo/resources'));
		content.appendChild (document.createTextNode('\xa0|\xa0'));
	}
	content.appendChild
		(makeRawLink ('css', cfg('wgScript') + '?title=User:' 
			+ encodeURIComponent (cfg('wgUserName'))
			+ '/monobook.css&action=edit'));
	content.appendChild (document.createTextNode('\xa0|\xa0'));
	content.appendChild
		(makeRawLink ('js', cfg('wgScript') + '?title=User:'
			+ encodeURIComponent (cfg('wgUserName'))
			+ '/monobook.js&action=edit'));

	var imgLinks = makeImageLinks();
	if (imgLinks.length) {
		content.appendChild(document.createElement('hr'));
		for (var i = 0; i < imgLinks.length; i++) {
			content.appendChild(imgLinks[i]);
			if (i+1 < imgLinks.length) content.appendChild (document.createElement('br'));
		}
	}
	if (cfg('wgUserName') === 'Lupo') {
		var action = cfg('wgAction');
		if (action === 'edit' || action === 'submit') {
			content.appendChild (document.createElement('br'));
			content.appendChild (makeClickLink ('stop', stopMsg));
		}
		content.appendChild(document.createElement('hr'));
		var span = document.createElement('span');
		span.id = 't-AjaxQuickDeleteOnDemand';
		span.appendChild (makeRawLink ('vc', 'javascript:importScript("MediaWiki:VisualFileChange.js");'));
		content.appendChild(span);
	}
	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: bottom tabs */

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');
	if (logo) logo.parentNode.removeChild (logo);
}

function fixDiffPage () {
	var oldtitle = document.getElementById ('mw-diff-otitle1');
	var newtitle = document.getElementById ('mw-diff-ntitle1');
	if (!oldtitle || !newtitle) return;
	oldtitle = oldtitle.getElementsByTagName('a');
	newtitle = newtitle.getElementsByTagName('a');
	if (!oldtitle || oldtitle.length == 0|| !newtitle || newtitle.length == 0) return;
	oldtitle = oldtitle[0];
	newtitle = newtitle[0];
	if (!oldtitle || !newtitle) return;
	var oT = titleFromLink (oldtitle);
	var nT = titleFromLink (newtitle);
	if (oT != nT) {
		oldtitle.insertBefore (document.createTextNode (oT + ': '), oldtitle.firstChild);
		newtitle.insertBefore (document.createTextNode (nT + ': '), newtitle.firstChild);
	}
}

function reformatMyPage() {
	rmLogo();
	addLeadSectionTab();
	setLeadSectionEditSummary();
	myPortlet();
	bottomTabs();
	var content = document.getElementById ('bodyContent');
	if (/[&?](diff|oldid|newid)=/.test (document.URL)) {
		// Diff page: check if we have different files
		fixDiffPage ();
	}
	//  topTabsToRightPlace();  /* First the top "tabs"... */ // Used only at en-WP, not here
}

jQuery(document).ready(reformatMyPage);

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