MediaWiki:Common.js: Difference between revisions
Appearance
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 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. */ | ||
// | // === Create Page Button (sitewide) === | ||
// Adds a "Create page" button for easy new page creation | |||
( function () { | ( function () { | ||
// Only run when the page is fully loaded | |||
function | $(function () { | ||
// If you also want it to show for anonymous users, remove the next line | |||
if ( mw.user.isAnon() ) return; | |||
// Function to open the edit page for a new title | |||
function createPage(title) { | |||
if (!title) return; | |||
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', '#') | ||
var $personal = $('#p-personal | .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 | $personal.append($('<li>').append($btn)); | ||
} else { | |||
} | // Fallback: add to the left sidebar navigation | ||
$('#p-navigation .body ul').append($('<li>').append($btn)); | |||
// | |||
$('# | |||
} | } | ||
}); | }); | ||
}() ); | }() ); |
Revision as of 14:06, 11 October 2025
/* Any JavaScript here will be loaded for all users on every page load. */
// === Create Page Button (sitewide) ===
// Adds a "Create page" button for easy new page creation
( function () {
// Only run when the page is fully loaded
$(function () {
// If you also want it to show for anonymous users, remove the next line
if ( mw.user.isAnon() ) return;
// Function to open the edit page for a new title
function createPage(title) {
if (!title) return;
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));
}
});
}() );