Jump to content

MediaWiki:Common.js: Difference between revisions

From Pure Evil Villains
Dude of Wealth and Taste (talk | contribs)
No edit summary
Dude of Wealth and Taste (talk | contribs)
No edit summary
 
(2 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. */
// === Create Page Button (sitewide) ===
// === CreatePageButton handler (WikiOasis safe version) ===
// Adds a "Create page" button for easy new page creation
mw.loader.using('mediawiki.util', function () {
( function () {
  $(document).on('click', '.create-page-button', function (e) {
  // Only run when the page is fully loaded
    e.preventDefault();
  $(function () {
    var prefix = $(this).attr('data-prefix') || '';
    // If you also want it to show for anonymous users, remove the next line
    var title = prompt('Enter the title of the new page:');
    if ( mw.user.isAnon() ) return;
    if (!title) return;
 
     title = title.trim().replace(/\s+/g, ' ');
    // Function to open the edit page for a new title
     var fullTitle = prefix ? prefix + title : title;
    function createPage(title) {
    var url = mw.util.getUrl(fullTitle, { action: 'edit' });
      if (!title) return;
     window.location.href = url;
      title = title.trim().replace(/\s+/g, ' ');
      var url = mw.config.get('wgScript') + '?title=' + encodeURIComponent(title) + '&action=edit';
      window.location.href = url;
    }
 
    // Create the button element
    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('Enter the title of the new page:');
        createPage(title);
      });
 
     // Try to add to the top-right user toolbar
    var $personal = $('#p-personal ul');
    if ($personal.length) {
      $personal.append($('<li>').append($btn));
     } else {
      // Fallback: add to the left sidebar navigation
      $('#p-navigation .body ul').append($('<li>').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.