User:DodoMan/blockanddiscuss.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
No edit summary
No edit summary
Line 27: Line 27:
.end()
.end()
.find('.oo-ui-fieldLayout-field')
.find('.oo-ui-fieldLayout-field')
.replaceWith($('<textarea id="' + textId + '" rows="10">{{Averto-3||}}~~' + '~~</textarea>'))
.replaceWith($('<textarea id="' + textId + '" rows="10">{{Blocked user}}~~' + '~~</textarea>'))
.end()
.end()
.appendTo($watchBlock.parent())
.appendTo($watchBlock.parent())

Revision as of 14:27, 17 March 2024

if (mw.config.get('wgCanonicalNamespace') == 'Special' &&
    mw.config.get('wgCanonicalSpecialPageName') == 'Block') {
  $(function() {
      var api = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
      var checkId = 'mw-input-wpBlockMessage';
      var textId = 'mw-input-wpBlockMessageText';
      var blockTemplate = '\\{\\{(Blocked user\\|[^\\|]*\\|[^\\}]*)\\}\\}';
      var reason = '(\\s*Avec le motif suivant\\s*: «[^»]*»\\.)?~~' + '~~';
      var $watchBlock = $('#mw-input-wpWatch').closest('.oo-ui-fieldLayout');

      $watchBlock.clone()
        .find('label')
          .attr('for', checkId)
          .text('Laisser un message sur la page de discussion de cet utilisateur')
        .end().find('input')
          .attr('id', checkId)
          .attr('value', 0)
          .click(function() {
            $('#' + textId + '-tr').toggle();
          })
        .end()
        .appendTo($watchBlock.parent())
      .clone()
        .attr('id', textId + '-tr')
        .find('label')
          .remove()
        .end()
        .find('.oo-ui-fieldLayout-field')
          .replaceWith($('<textarea id="' + textId + '" rows="10">{{Blocked user}}~~' + '~~</textarea>'))
        .end()
        .appendTo($watchBlock.parent())
        .hide();

      $watchBlock.closest('form').submit(function (event) {
        var form = this;
        if ($('#' + checkId).prop('checked')) {
          event.preventDefault();
          var talkPage = 'User_talk:' + $('#mw-bi-target input').val();
          $.getJSON(api + '?action=query&meta=tokens&type=csrf&prop=revisions&format=json&curtimestamp=true&titles=' + talkPage, function (response) {
            $.each(response.query.pages, function (_, page) {
              $.post(api, {
                action: 'edit',
                title: talkPage,
                section: 'new',
                sectiontitle: 'Blocage',
                text: $('#' + textId).val(),
                basetimestamp: page.revisions[0].timestamp,
                starttimestamp: response.curtimestamp,
                token: response.query.tokens.csrftoken,
                format: 'json'
              }, function (response) {
                // TODO do something if there was a problem
                form.submit();
              });
            });
          });
        }
      });

      $('#mw-input-wpExpiry').change(function () {
        var duration = $(this).find('option:selected').text();
        var template = '{{Vandale banni}}';
        if (duration != 'indéfiniment') {
          template = '{{Blocked user|' + duration.replace(' ', '|') + '}}';
        }
        $('#' + textId).val(
          $('#' + textId).val().replace(new RegExp(blockTemplate), template)
        );
      });

      function changeReason() {
        var defaultText = $('#mw-input-wpReason option:selected').text();
        if (defaultText == 'Autre') {
          defaultText = '';
        }
        var customText = $('#mw-input-wpReason-other').val();
        if (defaultText && customText) {
          defaultText += ' ';
        }
        $('#' + textId).val(
          // TODO if the user removed the reason from the message, don't add it again
          $('#' + textId).val().replace(new RegExp(reason), '\n\nAvec le motif suivant : « ' + defaultText + customText + ' ».~~' + '~~')
        );
      }

      $('#mw-input-wpReason').change(changeReason);
      $('#mw-input-wpReason-other').change(changeReason);
      $('#mw-input-wpReason-other').keyup(changeReason);
  });
}