User:DodoMan/blockanddiscuss.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
No edit summary
 
(2 intermediate revisions by one other user not shown)
(No difference)

Latest revision as of 12:56, 2 April 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);
  });
}