User:BZPN/Email.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
No edit summary
No edit summary
Line 3: Line 3:
if (mw.config.get('wgCanonicalSpecialPageName') !== 'EmailUser') return;
if (mw.config.get('wgCanonicalSpecialPageName') !== 'EmailUser') return;


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


// Stwórz nowe pole checkbox używając spójnego stylu OOUI
var $notifyCheckbox = $('<div class="mw-htmlform-field-HTMLCheckField">' +
var $notifyCheckbox = $(
'<div class="oo-ui-fieldLayout-field">' +
'<div class="mw-htmlform-field-HTMLCheckField">' +
'<input type="checkbox" id="notifyOnTalk" name="notifyOnTalk" value="1"> ' +
'<div class="oo-ui-fieldLayout oo-ui-fieldLayout-align-inline">' +
'<label for="notifyOnTalk">Notify user on talk page</label>' +
'</div>' +
'<div class="oo-ui-fieldLayout-body">' +
'<span class="oo-ui-fieldLayout-header">' +
'</div>');
'<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);
$copyField.after($notifyCheckbox);


// Obsługa formularza
$('form.mw-htmlform').on('submit', function (e) {
$('form.mw-htmlform').on('submit', function (e) {
if ($('#notifyOnTalk').is(':checked')) {
if ($('#notifyOnTalk').is(':checked')) {
e.preventDefault();
e.preventDefault();

var targetUser = $('#mw-input-wpTarget').val();
var targetUser = $('#mw-input-wpTarget').val();
if (!targetUser) return this.submit();
if (!targetUser) return this.submit();


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


new mw.Api().postWithToken('csrf', {
new mw.Api().postWithToken('csrf', {
Line 31: Line 44:
summary: 'Notification: You\'ve got mail!',
summary: 'Notification: You\'ve got mail!',
format: 'json'
format: 'json'
}).done(function() {
}).always(() => $('form.mw-htmlform').off('submit').submit());
console.log('Notification posted successfully');
}).fail(function(err) {
console.error('Error posting notification:', err);
}).always(function() {
$(e.target).off('submit').trigger('submit');
});
}
}
});
});

Revision as of 22:26, 24 February 2025

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');
        });
      }
    });
  });
});