|
|
(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));
| |
| } | |
| }); | | }); |
| }() ); | | }); |