Zum Inhalt springen

MediaWiki:Gadget-GlobalVariables.js: Unterschied zwischen den Versionen

Aus Home Wiki
Die Seite wurde neu angelegt: „mw.loader.using(['mediawiki.util']).then(function () { const globalVars = {}; // Hier speichern wir die Variablen der Seite // Leiste oben erstellen const bar = document.createElement('div'); bar.id = 'global-var-bar'; bar.style.display = 'flex'; bar.style.flexWrap = 'wrap'; bar.style.gap = '8px'; bar.style.padding = '8px'; bar.style.border = '1px solid #bbb'; bar.style.borderRadius = '6px'; bar.style.backgrou…“
 
Keine Bearbeitungszusammenfassung
Zeile 15: Zeile 15:
     bar.style.marginBottom = '12px';
     bar.style.marginBottom = '12px';
     document.querySelector('#content').prepend(bar);
     document.querySelector('#content').prepend(bar);
    // Hilfsfunktion: Variable nur einmal hinzufügen
    function addVarOnce(varName) {
        if (globalVars[varName]) return; // schon drin
        globalVars[varName] = '';
        const lbl = document.createElement('label');
        lbl.style.display = 'flex';
        lbl.style.flexDirection = 'column';
        lbl.style.fontSize = '12px';
        lbl.style.color = '#333';
        lbl.textContent = varName;
        const inp = document.createElement('input');
        inp.type = 'text';
        inp.value = ''; // noch leer
        inp.dataset.varId = varName;
        inp.style.width = '120px';
        inp.style.padding = '2px 4px';
        inp.style.border = '1px solid #ccc';
        inp.style.borderRadius = '4px';
        inp.style.fontFamily = 'monospace';
        inp.style.fontSize = '12px';
        lbl.appendChild(inp);
        bar.appendChild(lbl);
    }


     // Alle dynamischen Codeblöcke auf der Seite durchgehen
     // Alle dynamischen Codeblöcke auf der Seite durchgehen
Zeile 21: Zeile 48:
         let match;
         let match;
         while ((match = regex.exec(pre.textContent)) !== null) {
         while ((match = regex.exec(pre.textContent)) !== null) {
             const varName = match[1];
             addVarOnce(match[1]);
            if (!globalVars[varName]) {
                globalVars[varName] = '';
 
                // Input für Variable erstellen
                const lbl = document.createElement('label');
                lbl.style.display = 'flex';
                lbl.style.flexDirection = 'column';
                lbl.style.fontSize = '12px';
                lbl.style.color = '#333';
                lbl.textContent = varName;
 
                const inp = document.createElement('input');
                inp.type = 'text';
                inp.value = ''; // noch leer
                inp.dataset.varId = varName;
                inp.style.width = '120px';
                inp.style.padding = '2px 4px';
                inp.style.border = '1px solid #ccc';
                inp.style.borderRadius = '4px';
                inp.style.fontFamily = 'monospace';
                inp.style.fontSize = '12px';
 
                lbl.appendChild(inp);
                bar.appendChild(lbl);
            }
         }
         }
     });
     });

Version vom 14. Februar 2026, 12:21 Uhr

mw.loader.using(['mediawiki.util']).then(function () {

    const globalVars = {}; // Hier speichern wir die Variablen der Seite

    // Leiste oben erstellen
    const bar = document.createElement('div');
    bar.id = 'global-var-bar';
    bar.style.display = 'flex';
    bar.style.flexWrap = 'wrap';
    bar.style.gap = '8px';
    bar.style.padding = '8px';
    bar.style.border = '1px solid #bbb';
    bar.style.borderRadius = '6px';
    bar.style.background = '#f5f5f5';
    bar.style.marginBottom = '12px';
    document.querySelector('#content').prepend(bar);

    // Hilfsfunktion: Variable nur einmal hinzufügen
    function addVarOnce(varName) {
        if (globalVars[varName]) return; // schon drin
        globalVars[varName] = '';

        const lbl = document.createElement('label');
        lbl.style.display = 'flex';
        lbl.style.flexDirection = 'column';
        lbl.style.fontSize = '12px';
        lbl.style.color = '#333';
        lbl.textContent = varName;

        const inp = document.createElement('input');
        inp.type = 'text';
        inp.value = ''; // noch leer
        inp.dataset.varId = varName;
        inp.style.width = '120px';
        inp.style.padding = '2px 4px';
        inp.style.border = '1px solid #ccc';
        inp.style.borderRadius = '4px';
        inp.style.fontFamily = 'monospace';
        inp.style.fontSize = '12px';

        lbl.appendChild(inp);
        bar.appendChild(lbl);
    }

    // Alle dynamischen Codeblöcke auf der Seite durchgehen
    document.querySelectorAll('pre.dynamic-code').forEach(pre => {
        const regex = /{{(\w+)}}/g;
        let match;
        while ((match = regex.exec(pre.textContent)) !== null) {
            addVarOnce(match[1]);
        }
    });

    console.log('Globale Variablenleiste initialisiert:', Object.keys(globalVars));

});