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..."
(No difference)

Revision as of 13:51, 11 October 2025

/* 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 (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);
    }
  });
}() );
Cookies help us deliver our services. By using our services, you agree to our use of cookies.