ਵਰਤੋਂਕਾਰ:Benipal hardarshan/saveandedit.js

ਵਿਕੀਸਰੋਤ ਤੋਂ

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.
/**
 * To use this script, add following line to your [[Special:MyPage/common.js]] (without the "*"):
 * importScript('User:Mabdul/saveandedit.js');
 */
/*global mw, $ */
(function () {
  var editButtons = document.getElementsByClassName('editButtons')[0];

  if (!editButtons || !document.editform) {
    return;
  }

  var conf = mw.config.get(['wgPageName']);

  $('<input type="button"/>')
    .val('Save and edit')
    .on('click', function () {
      saveandedit_prompt();
    })
    .appendTo(editButtons);

  // Function for returning
  function saveandedit_prompt() {
    mw.loader.using(['mediawiki.util', 'mediawiki.api'], function () {
      var section = mw.util.getParamValue('section');
      var summary = document.editform.wpSummary.value;
      // Some advertising
      summary += ' (using [[User:Mabdul/saveandedit.js|Save&Edit]])';

      var params = {
        action: 'edit',
        title: conf.wgPageName,
        text: document.editform.wpTextbox1.value,
        summary: summary
      };
      if (document.editform.wpMinoredit.checked) {
        params.notminor = '1';
      }
      // Check if only a section will be edited
      if (section) {
        params.section = section;
      }

      var api = new mw.Api();
      api.postWithEditToken(params).done(function (response) {
        if (response.edit.result === 'Success') {
          // Reload on the success
          location.reload();
        } else {
          // Handle out the error message
          var error = $.parseHTML( '<div><div class="error">Edit failed on ' +
            mw.html.element('a', { href: mw.util.getUrl(conf.wgPageName), title: conf.wgPageName }, conf.wgPageName ) +
            '</div> Best try it again (<b>Don\'t forget to copy the text!</b>). Error info: ' +
            (response.error && mw.html.escape(response.error.code + ' : ' + response.error.info)) +
            '</div>' );
          mw.notify(error, { autoHide: false });
        }
      });
    });
  }
}());