MediaWiki:Common.js: Difference between revisions

From PsychoactiveWiki
Jump to navigation Jump to search
(Created page with "→‎Any JavaScript here will be loaded for all users on every page load.: // Auto-preload template for Shared_Experiences new story form mw.hook('wikipage.content').add(function () { // Only run on the new-section edit screen if (mw.config.get('wgPageName') === 'Shared_Experiences' && mw.config.get('wgAction') === 'edit' && new URLSearchParams(window.location.search).get('section') === 'new') { var textbox = document.getElementById('wpTe...")
 
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. */
// Auto-preload template for Shared_Experiences new story form
// Auto-preload template for Shared_Experiences new story form
mw.hook('wikipage.content').add(function () {
mw.hook('wikipage.content').add(function () {
     // Only run on the new-section edit screen
     // Only run on new-section add
     if (mw.config.get('wgPageName') === 'Shared_Experiences' &&
     if (
        mw.config.get('wgPageName') === 'Shared_Experiences' &&
         mw.config.get('wgAction') === 'edit' &&
         mw.config.get('wgAction') === 'edit' &&
         new URLSearchParams(window.location.search).get('section') === 'new') {
         new URLSearchParams(window.location.search).get('section') === 'new'
    ) {
        var textbox = document.getElementById('wpTextbox1');


         var textbox = document.getElementById('wpTextbox1');
         // Only insert if the textbox is empty
         if (textbox && textbox.value.trim() === '') {
         if (textbox && textbox.value.trim() === '') {
            // Load the preload content
             new mw.Api().get({
             new mw.Api().get({
                 action: 'parse',
                 action: 'parse',
Line 16: Line 19:
                 format: 'json'
                 format: 'json'
             }).done(function (data) {
             }).done(function (data) {
                 textbox.value = data.parse.wikitext;
                // FIX: MediaWiki stores wikitext under ['*']
                 textbox.value = data.parse.wikitext['*'];
             });
             });
         }
         }
     }
     }
});
});

Revision as of 04:55, 26 November 2025

/* Any JavaScript here will be loaded for all users on every page load. */

// Auto-preload template for Shared_Experiences new story form
mw.hook('wikipage.content').add(function () {
    // Only run on new-section add
    if (
        mw.config.get('wgPageName') === 'Shared_Experiences' &&
        mw.config.get('wgAction') === 'edit' &&
        new URLSearchParams(window.location.search).get('section') === 'new'
    ) {
        var textbox = document.getElementById('wpTextbox1');

        // Only insert if the textbox is empty
        if (textbox && textbox.value.trim() === '') {
            new mw.Api().get({
                action: 'parse',
                page: 'Template:SharedExperience/Preload',
                prop: 'wikitext',
                format: 'json'
            }).done(function (data) {
                // FIX: MediaWiki stores wikitext under ['*']
                textbox.value = data.parse.wikitext['*'];
            });
        }
    }
});