Jump to content

MediaWiki:Common.js: Difference between revisions

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
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
// === Create Page Button (sitewide) ===
// Adds a "Create page" button for easy new page creation
( function () {
( function () {
   if ( mw.user.isAnon() ) return; // optional: only for logged in users
   // Only run when the page is fully loaded
   function createPage(title) {
   $(function () {
     if (!title) return;
     // If you also want it to show for anonymous users, remove the next line
    title = title.trim().replace(/\s+/g, ' ');
     if ( mw.user.isAnon() ) return;
     window.location.href = mw.config.get('wgScript') + '?title=' + encodeURIComponent(title) + '&action=edit';
  }


  // add toolbar button (Vector/Timeless/other skins)
    // Function to open the edit page for a new title
  var $btn = $('<a>')
     function createPage(title) {
     .attr('id', 'create-new-page-button')
      if (!title) return;
    .attr('href', '#')
      title = title.trim().replace(/\s+/g, ' ');
    .text('Create page')
       var url = mw.config.get('wgScript') + '?title=' + encodeURIComponent(title) + '&action=edit';
    .css({
       window.location.href = url;
      '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
    // Create the button element
  $(function () {
    var $btn = $('<a>')
    var appended = false;
      .attr('id', 'create-new-page-button')
     // Vector/Timeless
      .attr('href', '#')
     var $personal = $('#p-personal, .mw-portlet-personal, .vector-user-links, #p-cactions');
      .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) {
     if ($personal.length) {
       $personal.first().append($('<li>').append($btn));
       $personal.append($('<li>').append($btn));
      appended = true;
     } else {
     }
       // Fallback: add to the left sidebar navigation
    if (!appended) {
       $('#p-navigation .body ul').append($('<li>').append($btn));
       // 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.