Aller au contenu

« MediaWiki:Common.js » : différence entre les versions

De Wikiquestia
Aucun résumé des modifications
Aucun résumé des modifications
 
(2 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
document.addEventListener('DOMContentLoaded', function () {
(function () {
   function fallbackCopyText(text) {
   try {
     const textarea = document.createElement("textarea");
     if (window.__copySyntaxInit) return;
     textarea.value = text;
     window.__copySyntaxInit = true;
     textarea.setAttribute("readonly", "");
 
    textarea.style.position = "absolute";
     document.addEventListener('click', function (e) {
    textarea.style.left = "-9999px";
      var btn = e.target.closest('.copy-syntax-btn');
    document.body.appendChild(textarea);
      if (!btn) return;
    textarea.select();
 
    try {
       var wrapper = btn.closest('.syntax-copy-wrapper');
       document.execCommand("copy");
       if (!wrapper) return;
    } catch (err) {
       console.error("Échec de la copie fallback :", err);
    }
    document.body.removeChild(textarea);
  }


  document.querySelectorAll('.copy-button').forEach(button => {
      var pre = wrapper.querySelector('.syntax-to-copy');
    button.addEventListener('click', function () {
       if (!pre) return;
      const targetId = this.getAttribute('data-copy-target');
      const target = document.getElementById(targetId);
       if (!target) return;


       const text = target.innerText || target.textContent;
       var text = pre.innerText;


       fallbackCopyText(text);
       navigator.clipboard.writeText(text).then(function () {
      this.textContent = '✅ Copié';
        btn.textContent = '✅ Copié';
      setTimeout(() => this.textContent = '📋 Copier', 2000);
        setTimeout(function () {
          btn.textContent = '📋 Copier la syntaxe';
        }, 1500);
      });
     });
     });
   });
   } catch (err) {
});
    console.error('CopierSyntaxe error:', err);
  }
})();

Dernière version du 17 janvier 2026 à 22:20

(function () {
  try {
    if (window.__copySyntaxInit) return;
    window.__copySyntaxInit = true;

    document.addEventListener('click', function (e) {
      var btn = e.target.closest('.copy-syntax-btn');
      if (!btn) return;

      var wrapper = btn.closest('.syntax-copy-wrapper');
      if (!wrapper) return;

      var pre = wrapper.querySelector('.syntax-to-copy');
      if (!pre) return;

      var text = pre.innerText;

      navigator.clipboard.writeText(text).then(function () {
        btn.textContent = '✅ Copié';
        setTimeout(function () {
          btn.textContent = '📋 Copier la syntaxe';
        }, 1500);
      });
    });
  } catch (err) {
    console.error('CopierSyntaxe error:', err);
  }
})();