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

De alcolois
Aller à la navigation Aller à la recherche
Ajout d'une fonction pour extraire le nom du dossier à partir de l'url d'un item Zotero de type case
Balise : Révoqué
Annulation des modifications 17641 de Marc (discussion)
Balise : Annulation
 
Ligne 53 : Ligne 53 :
         rel: 'noopener noreferrer'
         rel: 'noopener noreferrer'
       });
       });
    }
  });
});
mw.loader.using('mediawiki.util', function () {
  document.addEventListener('DOMContentLoaded', function () {
    const input = document.getElementById('zurl');
    const button = document.getElementById('zurl-button');
    const result = document.getElementById('zotero-formlink-result');
    if (!input || !button || !result) return;
    button.addEventListener('click', function () {
      const url = input.value.trim();
      const itemKey = extractItemKey(url);
      if (!itemKey.match(/^[A-Z0-9]+$/)) {
        result.innerHTML = '<p style="color:red;">Clé Zotero invalide.</p>';
        return;
      }
      const apiUrl = `https://api.zotero.org/groups/4893620/items/${itemKey}?include=data`;
      fetch(apiUrl)
        .then(res => res.json())
        .then(data => {
          const caseName = (data.data && data.data.caseName) ? data.data.caseName : "NomInconnu";
          const clean = caseName.replace(/[\[\]{}<>#%|^~`]/g, '').trim();
          const title = encodeURIComponent(clean);
          const formUrl = `/index.php?title=${title}&action=formedit&form=Décision_provenant_de_Zotero`;
          result.innerHTML = `<p><a href="${formUrl}">Créer la page « ${clean} »</a></p>`;
        })
        .catch(() => {
          result.innerHTML = '<p style="color:red;">Erreur lors de l’appel Zotero.</p>';
        });
    });
    function extractItemKey(url) {
      const parts = url.split('/');
      return (parts.length >= 8) ? parts[7].split('?')[0] : '';
     }
     }
   });
   });
});
});

Dernière version du 13 juin 2025 à 17:46

/* Tout JavaScript présent ici sera exécuté par tous les utilisateurs à chaque chargement de page. */
/**
 * Opens in a new tab:
 * - all internal links to MediaWiki and Category namespaces
 * 
 * To add other namespaces, do the following:
 * 1. Add their numeric ID to the `targetedNS` array below.
 *    You can find these IDs using the browser console:
 *      console.table(mw.config.get('wgFormattedNamespaces'));
 * 
 * 2. Add their canonical name to the `namespaceNames.push(...)` line if needed.
 *    These are required to detect alias usages (e.g., [[:Category:Name]]).
 *    To get canonical names via the API:
 *      fetch('/w/api.php?action=query&meta=siteinfo&siprop=namespaces&format=json&uselang=en')
 *        .then(r => r.json())
 *        .then(data => console.table(data.query.namespaces));
 */

$(function () {
  var targetedNS = [8, 14]; // MediaWiki and Category

  // Get localized namespace names from MediaWiki configuration
  var namespaces = mw.config.get('wgFormattedNamespaces');
  var namespaceNames = [];

  targetedNS.forEach(function (ns) {
    var localized = namespaces[ns];
    if (localized) {
      namespaceNames.push(localized);
    }
  });

  // Manually add known canonical aliases (e.g., English names on a localized wiki)
  namespaceNames.push('Category', 'MediaWiki');

  // Build encoded URL prefixes for comparison
  var namespacePrefixes = namespaceNames.map(function (name) {
    return '/wiki/' + encodeURIComponent(name) + ':';
  });

  // Apply target="_blank" to internal links in targeted namespaces
  $('a[href]').each(function () {
    var href = $(this).attr('href');
    if (
      href &&
      href.indexOf('#') === -1 &&
      namespacePrefixes.some(function (prefix) {
        return href.indexOf(prefix) === 0;
      })
    ) {
      $(this).attr({
        target: '_blank',
        rel: 'noopener noreferrer'
      });
    }
  });
});