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
(One intermediate revision 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
// === Universal "Create Page" Button for WikiOasis ===
( function () {
( function () {
   if ( mw.user.isAnon() ) return; // optional: only for logged in users
   $(function () {
  function createPage(title) {
    // Remove this line if you want guests to see it too:
    if (!title) return;
    if ( mw.user.isAnon() ) return;
    title = title.trim().replace(/\s+/g, ' ');
 
    window.location.href = mw.config.get('wgScript') + '?title=' + encodeURIComponent(title) + '&action=edit';
    // Function to open the edit page
  }
    function createPage(title) {
      if (!title) return;
      title = title.trim().replace(/\s+/g, ' ');
      var url = mw.util.getUrl(title, { action: 'edit' });
      window.location.href = url;
    }
 
    // Create the button
    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',
        'cursor': 'pointer'
      })
      .click(function (e) {
        e.preventDefault();
        var title = prompt('Enter the title of the new page:');
        createPage(title);
      });
 
    // Try different common locations for modern Vector / Oasis skins
    var added = false;


  // add toolbar button (Vector/Timeless/other skins)
    // 1. New Vector user menu area
  var $btn = $('<a>')
    if ($('.vector-user-menu, .vector-menu-content-list, .vector-menu-content').length) {
    .attr('id', 'create-new-page-button')
       $('.vector-user-menu .vector-menu-content-list, .vector-menu-content').first().append($('<li>').append($btn));
    .attr('href', '#')
       added = true;
    .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
    // 2. Classic personal toolbar
  $(function () {
    if (!added && $('#p-personal ul').length) {
    var appended = false;
      $('#p-personal ul').append($('<li>').append($btn));
     // Vector/Timeless
      added = true;
     var $personal = $('#p-personal, .mw-portlet-personal, .vector-user-links, #p-cactions');
    }
    if ($personal.length) {
 
       $personal.first().append($('<li>').append($btn));
     // 3. Sidebar navigation
       appended = true;
     if (!added && $('#p-navigation .body ul').length) {
       $('#p-navigation .body ul').append($('<li>').append($btn));
       added = true;
     }
     }
     if (!appended) {
 
      // fallback: append to masthead
    // 4. Absolute fallback – inject near the top of the page
       $('#mw-panel, #mw-navigation, #p-views').first().append($btn);
     if (!added) {
       $('#mw-content-text').prepend($('<div style="margin-bottom:1em;">').append($btn));
     }
     }
   });
   });
}() );
}() );

Revision as of 14:10, 11 October 2025

/* Any JavaScript here will be loaded for all users on every page load. */
// === Universal "Create Page" Button for WikiOasis ===
( function () {
  $(function () {
    // Remove this line if you want guests to see it too:
    if ( mw.user.isAnon() ) return;

    // Function to open the edit page
    function createPage(title) {
      if (!title) return;
      title = title.trim().replace(/\s+/g, ' ');
      var url = mw.util.getUrl(title, { action: 'edit' });
      window.location.href = url;
    }

    // Create the button
    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',
        'cursor': 'pointer'
      })
      .click(function (e) {
        e.preventDefault();
        var title = prompt('Enter the title of the new page:');
        createPage(title);
      });

    // Try different common locations for modern Vector / Oasis skins
    var added = false;

    // 1. New Vector user menu area
    if ($('.vector-user-menu, .vector-menu-content-list, .vector-menu-content').length) {
      $('.vector-user-menu .vector-menu-content-list, .vector-menu-content').first().append($('<li>').append($btn));
      added = true;
    }

    // 2. Classic personal toolbar
    if (!added && $('#p-personal ul').length) {
      $('#p-personal ul').append($('<li>').append($btn));
      added = true;
    }

    // 3. Sidebar navigation
    if (!added && $('#p-navigation .body ul').length) {
      $('#p-navigation .body ul').append($('<li>').append($btn));
      added = true;
    }

    // 4. Absolute fallback – inject near the top of the page
    if (!added) {
      $('#mw-content-text').prepend($('<div style="margin-bottom:1em;">').append($btn));
    }
  });
}() );
Cookies help us deliver our services. By using our services, you agree to our use of cookies.