Aller au contenu

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

De Wikiquestia
Aucun résumé des modifications
Aucun résumé des modifications
 
(4 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
function fallbackCopyText(text) {
(function () {
  const textarea = document.createElement("textarea");
  textarea.value = text;
  textarea.setAttribute("readonly", "");
  textarea.style.position = "absolute";
  textarea.style.left = "-9999px";
  document.body.appendChild(textarea);
  textarea.select();
   try {
   try {
     document.execCommand("copy");
    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) {
   } catch (err) {
     console.error("Échec de la copie fallback :", err);
     console.error('CopierSyntaxe error:', err);
   }
   }
  document.body.removeChild(textarea);
})();
}
 
document.addEventListener('DOMContentLoaded', function () {
  document.querySelectorAll('.copy-button').forEach(button => {
    button.addEventListener('click', function () {
      const targetId = this.getAttribute('data-copy-target');
      const target = document.getElementById(targetId);
      if (!target) return;
      const text = target.innerText || target.textContent;
      // Essayons clipboard.writeText d'abord
      if (navigator.clipboard && window.isSecureContext) {
        navigator.clipboard.writeText(text).then(() => {
          this.textContent = '✅ Copié';
          setTimeout(() => this.textContent = '📋 Copier', 2000);
        }).catch(() => {
          fallbackCopyText(text);
          this.textContent = '📎 Copié';
          setTimeout(() => this.textContent = '📋 Copier', 2000);
        });
      } else {
        fallbackCopyText(text);
        this.textContent = '📎 Copié';
        setTimeout(() => this.textContent = '📋 Copier', 2000);
      }
    });
  });
});

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);
  }
})();