User:BZPN/Email.js

From Test Wiki
Revision as of 22:26, 24 February 2025 by BZPN (talk | contribs)
Jump to navigation Jump to search

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
mw.loader.using(['mediawiki.api', 'jquery'], function () {
  $(function () {
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'EmailUser') return;

    // Znajdź pole "Wyślij kopię na moją stronę dyskusji" po tekście etykiety
    var $copyField = $('label:contains("Send a copy to my talk page")').closest('.mw-htmlform-field-HTMLCheckField');
    if (!$copyField.length) return;

    // Stwórz nowe pole checkbox używając spójnego stylu OOUI
    var $notifyCheckbox = $(
      '<div class="mw-htmlform-field-HTMLCheckField">' +
        '<div class="oo-ui-fieldLayout oo-ui-fieldLayout-align-inline">' +
          '<div class="oo-ui-fieldLayout-body">' +
            '<span class="oo-ui-fieldLayout-header">' +
              '<label class="oo-ui-labelElement-label">Notify user on talk page</label>' +
            '</span>' +
            '<div class="oo-ui-fieldLayout-field">' +
              '<div class="oo-ui-widget oo-ui-widget-enabled oo-ui-checkboxInput">' +
                '<input type="checkbox" id="notifyOnTalk" name="notifyOnTalk" class="oo-ui-inputWidget-input">' +
              '</div>' +
            '</div>' +
          '</div>' +
        '</div>' +
      '</div>'
    );

    $copyField.after($notifyCheckbox);

    // Obsługa formularza
    $('form.mw-htmlform').on('submit', function (e) {
      if ($('#notifyOnTalk').is(':checked')) {
        e.preventDefault();
        
        var targetUser = $('#mw-input-wpTarget').val();
        if (!targetUser) return this.submit();

        var talkPage = 'User talk:' + targetUser;
        var text = '\n\n== You\'ve got mail! ==\n{{subst:You\'ve got mail}}'+'~~'+'~~\n';

        new mw.Api().postWithToken('csrf', {
          action: 'edit',
          title: talkPage,
          appendtext: text,
          summary: 'Notification: You\'ve got mail!',
          format: 'json'
        }).done(function() {
          console.log('Notification posted successfully');
        }).fail(function(err) {
          console.error('Error posting notification:', err);
        }).always(function() {
          $(e.target).off('submit').trigger('submit');
        });
      }
    });
  });
});