User:Evalprime/cvu.js

mw.loader.using('oojs-ui-core').done(function {  var conf = mw.config.get([ 'wgArticlePath', 'wgPageName', 'wgScriptPath', 'wgRestrictionEdit', 'wgUserGroups', 'wgScript', ])

function showSuccess { OO.ui.alert('Thanks for your submission. The page will now be reloaded.').done(function {      document.location.replace(conf.wgArticlePath.replace('$1', conf.wgPageName))    }) }

function showError(msg) { OO.ui.alert(msg) }

function callAPI(data, method, callback) { data['format'] = 'json' $.ajax({     data: data,      dataType: 'json',      url: conf.wgScriptPath + '/api.php',      type: method,      success: function (response) {        if (response.error) showError('API error: ' + response.error.info)        else callback(response)      },      error: function (xhr, error) {        showError('AJAX error: ' + error)      },    }) }

function cvuSubmit(editor) { callAPI(     {        action: 'query',        prop: 'info|revisions',        intoken: 'edit',        titles: conf.wgPageName,        rvprop: 'content',        rvlimit: '1',        indexpageids: 'true',      },      'GET',      function (response) {        var page = response.query.pages[response.query.pageids[0]]        var content = page.revisions[0]['*']        var template = ''

if (content.match(template) === null) showError('An error occurred.') content = content.replace(template, '{' + '{CVU|' + editor + '}' + '}')

callAPI(         {            minor: 'yes',            summary:              'Reported new user: [' +              '[Special:Contributions/' +              editor +              '|' +              editor +              ']' +              ']',            action: 'edit',            title: conf.wgPageName,            basetimestamp: page.revisions[0].timestamp,            startimestamp: page.starttimestamp,            token: page.edittoken,            text: content,          },          'POST',          function (response) {            if (response.edit.result == 'Success') showSuccess            else showError('An error occurred while submitting the edit.')          }        ) }   )    return false  }

if (conf.wgPageName == 'Big_Nate_COMMENTS_Wiki:Counter-Vandalism_Unit') { var $div = $('#guide') var $p = $('')

var ppLink = 'Due to vandalism, this page has been protected from editing.'

if (     $.inArray('content-moderator', conf.wgRestrictionEdit) > -1 &&      $.inArray('content-moderator', conf.wgUserGroups) == -1    ) $p.append($('').append(ppLink + ' Please contact a staff member.')) else if (     $.inArray('autoconfirmed', conf.wgRestrictionEdit) > -1 &&      $.inArray('autoconfirmed', conf.wgUserGroups) == -1    ) $p.append(       $('').append( ppLink + ' Please log in to report users.' )     )    else { var button = new OO.ui.ButtonWidget({       label: 'Report a vandal',        icon: 'alert',        title: 'Reports a new vandal to the CVU',      })

$p.append(       button.$element.click(function  { new OO.ui.prompt('Enter a username', { textInput: { placeholder: 'Username' } }).done(           function (result) {              if (result !== null && result !== '') {                cvuSubmit(result)              }            }          ) })     )    }

if ($div !== null) $div.empty.append($p)

$('span').each(function {      var name = this.className      if (this.id == 'cvuremove' && this.className != 'insert vandal') {        var $img1 = $(' ').attr( 'src', conf.wgArticlePath.replace('$1', 'Special:FilePath/Yes.svg') )       $img1          .attr('width', '15')          .attr('height', '16')          .attr('title', 'Remove this user as blocked')          .css('cursor', 'pointer')        $img1.click(function  { removeUser(name.replace(/ /g, '_').replace(/-/g, '.'), 'blocked') })

var $img2 = $(' ').attr(         'src',          conf.wgArticlePath.replace('$1', 'Special:FilePath/No.svg')        ) $img2 .attr('width', '15') .attr('height', '16') .attr('title', 'Disregard this report') .css('cursor', 'pointer') $img2.click(function {          removeUser(name.replace(/ /g, '_').replace(/-/g, '.'), '')        })

$(this).append(' ').append($img1).append($img2) }   })  }

function removeUser(user, state) { var template = ''

callAPI(     {        action: 'query',        prop: 'info|revisions',        intoken: 'edit',        titles: conf.wgPageName,        rvprop: 'content',        rvlimit: '1',        indexpageids: 'true',      },      'GET',      function (response) {        var page = response.query.pages[response.query.pageids[0]]        var content = page.revisions[0]['*']

if (content.match(template) === null) { showError('An error occurred.') return }

content = content.replace(         '{' + '{CVU|' + user.split('_').join(' '),          '{' + '{CVU|insert vandal}' + '}'        ) content = content.replace(/\}\}(\|.*?)?\}\}/, '}' + '}')

var summary = '[' + '[Special:Contributions/' + user + '|' + user.split('_').join(' ') + ']' + ']' if (state == 'blocked') summary += ' has been blocked.' else summary += ' has not been blocked.'

callAPI(         {            minor: 'yes',            summary: summary,            action: 'edit',            title: conf.wgPageName,            basetimestamp: page.revisions[0].timestamp,            startimestamp: page.starttimestamp,            token: page.edittoken,            text: content,          },          'POST',          function (response) {            if (response.edit.result != 'Success')              showError('An error occurred while submitting the edit.')            else document.location.replace(conf.wgArticlePath.replace('$1', conf.wgPageName))          }        ) }   )  } })