User:DodoMan/blockanddiscuss.js

From Test Wiki
Revision as of 14:22, 17 March 2024 by DodoMan~usurped (talk | contribs) (Created page with "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 = '\\{\\{(Averto-3\\|[^\\|]*\\|[^\\}]*|Vandale banni)\\}\\}'; var reason = '(\\s*Avec le motif suivant\\s*: «[^»]*»\\.)?...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
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 = '\\{\\{(Averto-3\\|[^\\|]*\\|[^\\}]*|Vandale banni)\\}\\}';
      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">{{Averto-3||}}~~' + '~~</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 = '{{Averto-3|' + 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);
  });
}