MediaWiki:Gadget-GlobalVariables.js: Unterschied zwischen den Versionen
Erscheinungsbild
Saya (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
Saya (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung Markierung: Zurückgesetzt |
||
| Zeile 2: | Zeile 2: | ||
const globalVars = {}; // speichert die Variablenwerte | const globalVars = {}; // speichert die Variablenwerte | ||
const seenVars = new Set(); // speichert, welche Variablen schon | const seenVars = new Set(); // speichert, welche Variablen schon in der Leiste sind | ||
const variableBlocks = {}; // speichert die Blöcke, die jede Variable nutzen | |||
// Leiste | // --- Globale Leiste erstellen --- | ||
const bar = document.createElement('div'); | const bar = document.createElement('div'); | ||
bar.id = 'global-var-bar'; | bar.id = 'global-var-bar'; | ||
| Zeile 18: | Zeile 19: | ||
function addVarOnce(varName) { | function addVarOnce(varName) { | ||
if (seenVars.has(varName)) return; | if (seenVars.has(varName)) return; | ||
seenVars.add(varName); | seenVars.add(varName); | ||
| Zeile 40: | Zeile 41: | ||
inp.style.fontFamily = 'monospace'; | inp.style.fontFamily = 'monospace'; | ||
inp.style.fontSize = '12px'; | inp.style.fontSize = '12px'; | ||
// Wenn Input geändert wird: alle Blöcke updaten | |||
inp.addEventListener('input', () => { | |||
globalVars[varName] = inp.value; | |||
updateAllBlocks(varName); | |||
}); | |||
lbl.appendChild(inp); | lbl.appendChild(inp); | ||
| Zeile 45: | Zeile 52: | ||
} | } | ||
// | // --- Dynamische Codeblöcke initialisieren --- | ||
function initDynamicCodeBlock(pre) { | |||
if (pre.dataset.dynamicInit) return; | |||
pre.dataset.dynamicInit = true; | |||
const originalCode = pre.textContent; | |||
const regex = /{{(\w+)}}/g; | const regex = /{{(\w+)}}/g; | ||
const vars = []; | |||
let match; | let match; | ||
while ((match = regex.exec(pre.textContent)) !== | while ((match = regex.exec(originalCode)) !== null) { | ||
if (!vars.includes(match[1])) vars.push(match[1]); | |||
} | |||
if (vars.length === 0) return; | |||
// Kopier-Button rechts oben in der Box | |||
const btn = document.createElement('button'); | |||
btn.textContent = '📋 Kopieren'; | |||
btn.style.position = 'absolute'; | |||
btn.style.top = '4px'; | |||
btn.style.right = '4px'; | |||
btn.style.padding = '4px 8px'; | |||
btn.style.fontSize = '12px'; | |||
btn.style.cursor = 'pointer'; | |||
btn.style.border = '1px solid #888'; | |||
btn.style.borderRadius = '4px'; | |||
btn.style.background = '#eee'; | |||
btn.onclick = () => copyText(pre.textContent, btn); | |||
// Wrapper um Codeblock, um Button absolut zu positionieren | |||
const wrapper = document.createElement('div'); | |||
wrapper.style.position = 'relative'; | |||
wrapper.appendChild(btn); | |||
pre.parentNode.insertBefore(wrapper, pre); | |||
wrapper.appendChild(pre); | |||
// Variable für globale Updates registrieren | |||
vars.forEach(v => { | |||
addVarOnce(v); | |||
if (!variableBlocks[v]) variableBlocks[v] = []; | |||
variableBlocks[v].push(pre); | |||
}); | |||
// initial update | |||
updateAllBlocks(); | |||
} | |||
// --- Alle Blöcke updaten --- | |||
function updateAllBlocks(varName) { | |||
const varsToUpdate = varName ? [varName] : Object.keys(variableBlocks); | |||
varsToUpdate.forEach(name => { | |||
const blocks = variableBlocks[name] || []; | |||
blocks.forEach(pre => { | |||
let updated = pre.textContent; | |||
// alle Variablen im Block ersetzen | |||
const regex = /{{(\w+)}}/g; | |||
updated = updated.replace(regex, (match, p1) => globalVars[p1] ?? p1); | |||
pre.textContent = updated; | |||
}); | |||
}); | |||
// Sync Inputs in der Leiste | |||
Object.keys(globalVars).forEach(name => { | |||
const inp = document.querySelector(`#global-var-bar input[data-var-id="${name}"]`); | |||
if (inp && inp.value !== globalVars[name]) inp.value = globalVars[name]; | |||
}); | |||
} | |||
// --- Copy-Funktion --- | |||
function copyText(text, btn) { | |||
if (navigator.clipboard && window.isSecureContext) { | |||
navigator.clipboard.writeText(text).then(success); | |||
} else { | |||
const textarea = document.createElement('textarea'); | |||
textarea.value = text; | |||
document.body.appendChild(textarea); | |||
textarea.select(); | |||
document.execCommand('copy'); | |||
document.body.removeChild(textarea); | |||
success(); | |||
} | } | ||
function success() { | |||
btn.textContent = '✅ Kopiert!'; | |||
setTimeout(() => btn.textContent = '📋 Kopieren', 1500); | |||
} | |||
} | |||
// --- Initialisierung --- | |||
function initPage() { | |||
document.querySelectorAll('pre.dynamic-code').forEach(initDynamicCodeBlock); | |||
} | |||
initPage(); | |||
mw.hook('wikipage.content').add(initPage); | |||
}); | }); | ||
Version vom 14. Februar 2026, 12:24 Uhr
mw.loader.using(['mediawiki.util']).then(function () {
const globalVars = {}; // speichert die Variablenwerte
const seenVars = new Set(); // speichert, welche Variablen schon in der Leiste sind
const variableBlocks = {}; // speichert die Blöcke, die jede Variable nutzen
// --- Globale Leiste 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);
function addVarOnce(varName) {
if (seenVars.has(varName)) return;
seenVars.add(varName);
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 = '';
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';
// Wenn Input geändert wird: alle Blöcke updaten
inp.addEventListener('input', () => {
globalVars[varName] = inp.value;
updateAllBlocks(varName);
});
lbl.appendChild(inp);
bar.appendChild(lbl);
}
// --- Dynamische Codeblöcke initialisieren ---
function initDynamicCodeBlock(pre) {
if (pre.dataset.dynamicInit) return;
pre.dataset.dynamicInit = true;
const originalCode = pre.textContent;
const regex = /{{(\w+)}}/g;
const vars = [];
let match;
while ((match = regex.exec(originalCode)) !== null) {
if (!vars.includes(match[1])) vars.push(match[1]);
}
if (vars.length === 0) return;
// Kopier-Button rechts oben in der Box
const btn = document.createElement('button');
btn.textContent = '📋 Kopieren';
btn.style.position = 'absolute';
btn.style.top = '4px';
btn.style.right = '4px';
btn.style.padding = '4px 8px';
btn.style.fontSize = '12px';
btn.style.cursor = 'pointer';
btn.style.border = '1px solid #888';
btn.style.borderRadius = '4px';
btn.style.background = '#eee';
btn.onclick = () => copyText(pre.textContent, btn);
// Wrapper um Codeblock, um Button absolut zu positionieren
const wrapper = document.createElement('div');
wrapper.style.position = 'relative';
wrapper.appendChild(btn);
pre.parentNode.insertBefore(wrapper, pre);
wrapper.appendChild(pre);
// Variable für globale Updates registrieren
vars.forEach(v => {
addVarOnce(v);
if (!variableBlocks[v]) variableBlocks[v] = [];
variableBlocks[v].push(pre);
});
// initial update
updateAllBlocks();
}
// --- Alle Blöcke updaten ---
function updateAllBlocks(varName) {
const varsToUpdate = varName ? [varName] : Object.keys(variableBlocks);
varsToUpdate.forEach(name => {
const blocks = variableBlocks[name] || [];
blocks.forEach(pre => {
let updated = pre.textContent;
// alle Variablen im Block ersetzen
const regex = /{{(\w+)}}/g;
updated = updated.replace(regex, (match, p1) => globalVars[p1] ?? p1);
pre.textContent = updated;
});
});
// Sync Inputs in der Leiste
Object.keys(globalVars).forEach(name => {
const inp = document.querySelector(`#global-var-bar input[data-var-id="${name}"]`);
if (inp && inp.value !== globalVars[name]) inp.value = globalVars[name];
});
}
// --- Copy-Funktion ---
function copyText(text, btn) {
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(text).then(success);
} else {
const textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
success();
}
function success() {
btn.textContent = '✅ Kopiert!';
setTimeout(() => btn.textContent = '📋 Kopieren', 1500);
}
}
// --- Initialisierung ---
function initPage() {
document.querySelectorAll('pre.dynamic-code').forEach(initDynamicCodeBlock);
}
initPage();
mw.hook('wikipage.content').add(initPage);
});