User:User unavailable826/global.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Joeytje50/JWB.js/load.js&action=raw&ctype=text/javascript');

/* This script will add items to the toolbar for quick access to commonly used things */

/* $( document ).ready( function() {
  mw.util.addPortletLink(
    'p-personal',
    mw.util.getUrl( 'Wikipedia:Dashboard' ),
    'Dashboard',
    'pt-dashboard',
    'View the Wikipedia Dashboard ',
    null,
    '#pt-preferences'
  );
}); */
mw.loader.using( 'mediawiki.util' ).done( function() {
	mw.util.addPortletLink('p-navigation', mw.config.get( 'wgServer' ) + '/wiki/User:Anoop Rao/Sandbox', "sandbox");
	} );

			
/* 
* Rollback
* @description Perform rollbacks without needing to be in the usergroup
* @author Ozank
*/

mw.loader.using('mediawiki.api', function() {

	//Exit if normal rollback links are present
	if ($('.mw-rollback-link').length) return;
	
	var conf = mw.config.get([
		'wgAction',
		'wgCanonicalSpecialPageName',
		'wgContentLanguage',
		'wgPageName',
		'wgUserLanguage',
		'skin'
	]),
	i18n = {
		en: {
			rollback: "rollback",
			description: ""Rollback" reverts edit(s) to this page of the last contributor in one click",
			summary: "Reverted edits by [[Special:Contributions/$1|$1]] to last version by [[User:$2|$2]] ([[wikia:c:dev:Rollback|script]])",
			summaryShort: "Reverted edits by $1 ([[wikia:c:dev:Rollback|script]])",
			editConflict: "Unable to rollback (edit conflict)",
			contentFail: "Unable to rollback (failed to get page content)",
			singleEditor: "Unable to rollback (no different editor found)",
			revisionFail: "Unable to rollback (failed to get revisions)",
			editFail: "Unable to rollback (failed to publish edit)",
			success: "Rollback successful"
		}
	};
	i18n = i18n[conf.wgUserLanguage] || i18n[conf.wgUserLanguage.split('-')[0]] || i18n[conf.wgContentLanguage] || i18n[conf.wgContentLanguage.split('-')[0]] || i18n.en;
	
	//History
	if (conf.wgAction == "history" && $('#pagehistory li').length > 1) 
		$('#pagehistory li:first .mw-history-undo a').before('<span class="mw-custom-rollback-link"><a title="' + i18n.description + '" data-id="' + conf.wgPageName + '" data-user="' + $('.mw-userlink:first').text() + '">' + i18n.rollback + '</a></span> | ');
	
	//Contributions
	else if (conf.wgCanonicalSpecialPageName == "Contributions") {
		$('#mw-content-text ul').find('li').each(function() {
			if ($(this).find('.mw-uctop').length)
				$(this).append(' <span class="mw-custom-rollback-link">[<a title="' + i18n.description + '" data-id="' + $(this).find('a:first').attr('title') + '" data-user="' + conf.wgPageName.split('/')[1] + '">' + i18n.rollback + '</a>]</span>');
		});
	}
	
	//Diffs
	else if (($.getUrlVar('diff') || $.getUrlVar('oldid')) && $('#differences-nextlink').length === 0)
		$('.mw-usertoollinks:last').after('&nbsp;&nbsp;&nbsp;&nbsp;<span class="mw-custom-rollback-link">[<a title="' + i18n.description + '" data-id="' + conf.wgPageName + '" data-user="' + $('#mw-diff-ntitle2 .mw-userlink').text() + '">' + i18n.rollback + '</a>]</span>');
	else
		return;

	conf.API = new mw.Api();
	conf.token = mw.user.tokens.values.editToken;	

	mw.util.addCSS('.mw-custom-rollback-link a { cursor: pointer; }');
		
	$('.mw-custom-rollback-link a').click(function() {
		getRevisionIdAndContent($(this).data('id'),$(this).data('user').replace(/_/g, " "));
		$(this).parent().remove();
	});
	
	function getRevisionIdAndContent(title,target) {
		conf.API.get({
		action: 'query',
		prop: 'revisions',
		titles: title,
		rvprop: 'user|ids',
		rvlimit: 500,
		cb: new Date().getTime()
		})
		.done(function(d) {
			if (!d.error) {
				var revisions;
				
				for (var i in d.query.pages) {
					revisions = d.query.pages[i].revisions;
				}
				
				//Don't rollback if the page has been edited by somebody else
				if (target != revisions[0].user) {
					outputError(i18n.editConflict);
					return;
				}
				
				var lastUser,
				revId;
				
				for (var i in revisions) {
					if (revisions[i].user != target) {
						lastUser = revisions[i].user; //remember last author
						revId = revisions[i].revid; //get revision to revert to
						break;
					}
				}
				
				if (lastUser) {
					conf.API.get({
					action: 'query',
					prop: 'revisions',
					rvprop: 'content',
					revids: revId,
					cb: new Date().getTime()
					})
					.done(function(d) {
						if (!d.error) {
							var content = ""; //can be no content on page
							for (var i in d.query.pages) {
								if (d.query.pages[i].revisions)
									content = d.query.pages[i].revisions[0]["*"];
							}
							performRollback(title,content,target,lastUser);
						}
						else
							outputError(i18n.contentFail + ': ' + d.error.code);
					})
					.fail(function() {
						outputError(i18n.contentFail);			
					});
				}
				else
					outputError(i18n.singleEditor);
			}
			else
				outputError(i18n.revisionFail + ': ' + d.error.code);
		})
		.fail(function() {
			outputError(i18n.revisionFail);
		});			
	}
	
	function performRollback(page,text,user,user2) {
		var summary = i18n.summary.replace(/\$1/g,user).replace(/\$2/g,user2);

		if (summary.length > 150)
			summary = i18n.summaryShort.replace('$1',user);
		
		conf.API.post({
		action: 'edit',
		title: page,
		text: text,
		summary: summary,
		minor: true,
		token: conf.token
		})
		.done(function(d) {
			if (!d.error) {
				new BannerNotification(i18n.success,'confirm').show();				
			}
			else
				outputError(i18n.editFail + ': ' + d.error.code);
		})
		.fail(function() {
			outputError(i18n.editFail);
		});	
	}
	
	function outputError(text) {
		switch (conf.skin) {
			case 'oasis': 
			case 'wikia':
				new BannerNotification(text,'error').show();				
			break;
 
			default:
				alert(text);
			break;
		}	
	}
});