User:MusikAnimal/userRightsManager.js

// (function { if (!/Wikipedia:Requests for permissions\//.test(document.title)) {    return;  }

var permissionNames = { 'Account creator': 'accountcreator', 'Autopatrolled': 'autoreviewer', 'Confirmed': 'confirmed', 'File mover': 'filemover', 'Mass message sender': 'massmessage-sender', 'Pending changes reviewer': 'reviewer', 'Rollback': 'rollbacker', 'Template editor': 'templateeditor' };

var templates = { 'Account creator': 'Account creator granted', 'Autopatrolled': 'Autopatrolledgiven', 'AutoWikiBrowser': '', 'Confirmed': '', 'File mover': 'Filemovergiven', 'Mass message sender': 'Mass message sender granted', 'Pending changes reviewer': 'Pending changes reviewer granted', 'Rollback': 'Rollbackgiven3', 'Template editor': 'Template editor granted' };

$('.sysop-show a').on('click', getToken);

var api = new mw.Api, permission = mw.config.get('wgTitle').split('/').slice(-1)[0], revisionId = mw.config.get('wgRevisionId'), tagLine = ' (using userRightsManager)', permaLink, userName, userRightsToken, editToken;

function getToken(e) { if (permission === 'AutoWikiBrowser') return true; e.preventDefault; userName = $(this).parents('.plainlinks').find('a').eq(0).text; if (!confirm('Assign User:' + userName + ' ' + permissionNames[permission] + ' rights?')) return; $(this).replaceWith('Processing...'); $.when(api.getToken('userrights'), api.getToken('edit')).done(function(a, b) {     userRightsToken = a;      editToken = b;      assignPermission;    }).fail(function(response) {      alert('Failed to fetch tokens! Error: ' + response);   }); }

function assignPermission { permaLink = 'Special:PermaLink/' + revisionId + ''; api.post({     action: 'userrights',      user: userName,      add: permissionNames[permission],      reason: 'Requested at WP:PERM; ' + permaLink + tagLine,      token: userRightsToken    }).done(markAsDone).fail(function(response) {      alert('Failed to assign permission! Error: ' + response);   }); }

function markAsDone { var sectionNode = document.getElementById('User:' + userName); var sectionNumber = $(sectionNode).siblings('.mw-editsection').find('a').prop('href').match(/section=(\d)/)[1]; api.post({     action: 'edit',      title: mw.config.get('wgPageName'),      section: sectionNumber,      summary: 'done' + tagLine,      appendtext: '\n::✅ ~',      token: editToken    }).done(issueTemplate).fail(function(response) {      alert('Permission was assigned, but failed to mark request as done and issue template! Error: ' + response);   }); }

function issueTemplate { if (!templates[permission]) return location.reload(true); api.post({     action: 'edit',      title: 'User talk:' + userName,      section: 'new',      summary: permission + ' granted per ' + permaLink + tagLine,      text: '',      sectiontitle: permission + ' granted',      token: editToken    }).done(function {      location.reload(true).fail(reportError);    }).error(function(response) {      alert('Permission was assigned and request marked as done, but failed to issue template! Error: ' + response);   }); } }); //