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

Der Seiteninhalt wurde durch einen anderen Text ersetzt: „global mw: console.log('[LabelScan] gadget file loaded'); document.addEventListener('DOMContentLoaded', function () { console.log('[LabelScan] DOM ready'); var ids = ['ados-scan-run','ados-scan-photo','ados-scan-pick','ados-scan-file']; ids.forEach(function(id){ console.log('[LabelScan] lookup', id, !!document.getElementById(id)); }); var run = document.getElementById('ados-scan-run'); if (run) { run.a…“
Markierung: Ersetzt
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
/* global mw */
/* global mw */
console.log('[LabelScan] gadget file loaded');
console.log('[LabelScan] gadget file loaded');
document.addEventListener('DOMContentLoaded', function () {
  console.log('[LabelScan] DOM ready');


   var ids = ['ados-scan-run','ados-scan-photo','ados-scan-pick','ados-scan-file'];
// --- robuste Ready/Bind-Mechanik ---
  ids.forEach(function(id){
function bind(origin) {
     console.log('[LabelScan] lookup', id, !!document.getElementById(id));
   try {
    console.log('[LabelScan] bind from:', origin, 'state=', document.readyState);
 
    var run = document.getElementById('ados-scan-run');
    var photo = document.getElementById('ados-scan-photo');
    var pick = document.getElementById('ados-scan-pick');
    var file = document.getElementById('ados-scan-file');
 
    console.log('[LabelScan] elements:', {
      run: !!run, photo: !!photo, pick: !!pick, file: !!file
    });
 
    if (!run) {
      console.warn('[LabelScan] kein #ados-scan-run gefunden – steht das HTML wirklich auf dieser Seite?');
      return;
    }
 
    if (!run.dataset._bound) {
      run.dataset._bound = '1';
      run.addEventListener('click', function () {
        alert('Click OK – Gadget greift!');
      }, { once: true });
      console.log('[LabelScan] click bound on run');
     } else {
      console.log('[LabelScan] already bound, skip');
    }
 
    // OPTIONAL: Buttons für Foto/Galerie testweise anklemmen
    function clickInput(withCapture) {
      try {
        if (file) {
          if (withCapture) { file.setAttribute('capture', 'environment'); }
          else { file.removeAttribute('capture'); }
          file.click();
        }
      } catch (e) {}
    }
    if (photo && !photo.dataset._bound) {
      photo.dataset._bound = '1';
      photo.addEventListener('click', function () { clickInput(true); });
      console.log('[LabelScan] photo bound');
    }
    if (pick && !pick.dataset._bound) {
      pick.dataset._bound = '1';
      pick.addEventListener('click', function () { clickInput(false); });
      console.log('[LabelScan] pick bound');
    }
 
  } catch (e) {
    console.error('[LabelScan] bind error:', e);
  }
}
 
// 1) Sofort versuchen (falls DOM schon bereit ist)
if (document.readyState !== 'loading') {
  bind('immediate');
} else {
  // 2) Klassisches DOMContentLoaded
  document.addEventListener('DOMContentLoaded', function onDom() {
    bind('DOMContentLoaded');
  }, { once: true });
}
 
// 3) MediaWiki-Hook für dynamisch ersetzte Seiteninhalte
if (mw && mw.hook) {
  mw.hook('wikipage.content').add(function () {
    bind('mw.hook(wikipage.content)');
   });
   });
}
// 4) Fallbacks (manche Skins/Module laden später)
setTimeout(function(){ bind('timeout-250ms'); }, 250);
setTimeout(function(){ bind('timeout-1000ms'); }, 1000);


// 5) Optional: MutationObserver, falls der Inhalt später ins DOM kommt
var mo = new MutationObserver(function () {
  // Versuche nur zu binden, wenn noch nicht gebunden
   var run = document.getElementById('ados-scan-run');
   var run = document.getElementById('ados-scan-run');
   if (run) {
   if (run && !run.dataset._bound) {
    run.addEventListener('click', function () {
     bind('mutation');
      alert('Click OK – Gadget greift!');
    }, { once: true });
    console.log('[LabelScan] click bound for run');
  } else {
     console.warn('[LabelScan] run button not found');
   }
   }
});
});
mo.observe(document.documentElement || document.body, { childList: true, subtree: true });