User:Evad37/Thanky.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Evad37/Thanky. |
/***************************************************************************************************
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'> | </span>").append($thank)
);
});
});
});
});
/* </nowiki> */