Jump to content

User:Evad37/Thanky.js

From Wikipedia, the free encyclopedia
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.
/***************************************************************************************************
 Thanky: Adds thank links next to diff links on special pages (e.g. watchlist, recent changes, etc)
***************************************************************************************************/
/* jshint esversion: 5, laxbreak: true, undef: true, eqnull: true, maxerr: 3000 */
/* globals $, mw, OO, extraJs */
/* <nowiki> */

$.when(
	// Resource loader modules
	mw.loader.using([
		'mediawiki.util', 'mediawiki.api', 'mediawiki.Uri',
		'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows'
	]),
	// Page ready
	$.ready
).then(function() {
	var config = {
		scriptVersion: "0.0.1",
		mw: mw.config.get(['wgNamespaceNumber', 'wgUserName', 'wgCanonicalSpecialPageName'])
	};
	if( config.mw.wgNamespaceNumber != -1 ) {
		// only operate in Special: namespace
		return;
	}
	var API = new mw.Api( {
		ajax: {
			headers: { 
				'Api-User-Agent': 'Thanky/' + config.scriptVersion + 
					' ( https://en.wikipedia.org/wiki/User:Evad37/Thanky )'
			}
		}
	} );
	API.get( {
		action: 'query',
		meta: 'allmessages',
		ammessages: [ 'Thanks-confirmation2', 'Thanks-thank', 'Thanks-thanked', 'Error', 'Centralauth-rename-table-status-failed' ],
		amenableparser: 1,
		amargs: mw.config.get('wgUserName')
	} ).then(function(response) {
		var messages = {};
		response.query.allmessages.forEach(function(message) {
			messages[message.normalizedname] = message['*'];
		});
		
		var makeErrorMsg = function(code, jqxhr) {
			var details = '';
			if ( code === 'http' && jqxhr.textStatus === 'error' ) {
				details = 'HTTP ' + messages.error + ' ' + jqxhr.xhr.status;
			} else if ( code === 'http' ) {
				details = 'HTTP ' + messages.error + ': ' + jqxhr.textStatus;
			} else if ( code === 'ok-but-empty' ) {
				details = messages.error + ': Got an empty response from the server';
			} else {
				details = 'API ' + messages.error + ': ' + code;
			}
			return details;
		};
		
		// Hook on to changes in page content - initial load, 'New filters for edit review' changes, etc
		mw.hook( 'wikipage.content' ).add( function ( $content ) {
			$content.find("a.mw-changeslist-diff").not(".thanky-linkAdded").each(function() {
				var uri = new mw.Uri(this.attributes.href.value);
				var diffNumber = Number(uri.query.diff);
				var oldidNumber = Number(uri.query.oldid);
				if(isNaN(diffNumber) && isNaN(oldidNumber)) {
					// Can't work out revision, so can't send thanks
					return;
				}
				var revision = isNaN(diffNumber) ? oldidNumber : diffNumber;
				var $thank =  $("<a>").attr("href","#").text( messages["thanks-thank"] ).click(function(e){
					e.preventDefault();
					OO.ui.confirm(  messages["thanks-confirmation2"] ).then( function ( confirmed ) {
						if ( confirmed ) {
							API.postWithToken("csrf", {
								action: "thank",
								rev: revision,
								source: "Thanky/" + config.mw.wgCanonicalSpecialPageName
							}).then(
								function() {
									mw.notify("✓ " + messages["thanks-thanked"]);
									$thank.hide().after( messages["thanks-thanked"] );
								},
								function(code, err) {
									mw.notify( makeErrorMsg(code, err), {
										title: "✗ " + messages["thanks-thank"] + " " + messages['centralauth-rename-table-status-failed'].toUpperCase(),
										type: 'warn'
									} );
								}
							);
						}
					});
				});
				$(this).addClass("thanky-linkAdded").after(
					$("<span class='thanky-container'>&#32;|&nbsp;</span>").append($thank)
				);
			});
		});
	});
});
/* </nowiki> */