Jump to content

MediaWiki:Common.js: Difference between revisions

From Pure Evil Villains
Dude of Wealth and Taste (talk | contribs)
Created page with "Any JavaScript here will be loaded for all users on every page load.: // CreatePageButton - add to MediaWiki:Common.js or as a gadget ( function () { if ( mw.user.isAnon() ) return; // optional: only for logged in users function createPage(title) { if (!title) return; title = title.trim().replace(/\s+/g, ' '); window.location.href = mw.config.get('wgScript') + '?title=' + encodeURIComponent(title) + '&action=edit'; } // add toolbar button (Vecto..."
 
Dude of Wealth and Taste (talk | contribs)
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
// CreatePageButton - add to MediaWiki:Common.js or as a gadget
// === CreatePageButton handler (WikiOasis safe version) ===
( function () {
mw.loader.using('mediawiki.util', function () {
   if ( mw.user.isAnon() ) return; // optional: only for logged in users
   $(document).on('click', '.create-page-button', function (e) {
  function createPage(title) {
    e.preventDefault();
    var prefix = $(this).attr('data-prefix') || '';
    var title = prompt('Enter the title of the new page:');
     if (!title) return;
     if (!title) return;
     title = title.trim().replace(/\s+/g, ' ');
     title = title.trim().replace(/\s+/g, ' ');
     window.location.href = mw.config.get('wgScript') + '?title=' + encodeURIComponent(title) + '&action=edit';
     var fullTitle = prefix ? prefix + title : title;
  }
    var url = mw.util.getUrl(fullTitle, { action: 'edit' });
 
     window.location.href = url;
  // add toolbar button (Vector/Timeless/other skins)
  var $btn = $('<a>')
    .attr('id', 'create-new-page-button')
    .attr('href', '#')
    .text('Create page')
    .css({
      'margin-left': '8px',
      'padding': '6px 10px',
      'background': '#2a7ae2',
      'color': '#fff',
      'border-radius': '4px',
      'text-decoration':'none',
      'font-weight':'600'
    })
    .click(function (e) {
      e.preventDefault();
      var title = prompt('Title of the new page:');
      createPage(title);
     });
 
  // Try to append to the personal toolbar or site actions if present
  $(function () {
    var appended = false;
    // Vector/Timeless
    var $personal = $('#p-personal, .mw-portlet-personal, .vector-user-links, #p-cactions');
    if ($personal.length) {
      $personal.first().append($('<li>').append($btn));
      appended = true;
    }
    if (!appended) {
      // fallback: append to masthead
      $('#mw-panel, #mw-navigation, #p-views').first().append($btn);
    }
   });
   });
}() );
});

Latest revision as of 14:19, 11 October 2025

/* Any JavaScript here will be loaded for all users on every page load. */
// === CreatePageButton handler (WikiOasis safe version) ===
mw.loader.using('mediawiki.util', function () {
  $(document).on('click', '.create-page-button', function (e) {
    e.preventDefault();
    var prefix = $(this).attr('data-prefix') || '';
    var title = prompt('Enter the title of the new page:');
    if (!title) return;
    title = title.trim().replace(/\s+/g, ' ');
    var fullTitle = prefix ? prefix + title : title;
    var url = mw.util.getUrl(fullTitle, { action: 'edit' });
    window.location.href = url;
  });
});
Cookies help us deliver our services. By using our services, you agree to our use of cookies.