User:Loki Laufeyjarson/common.js: Difference between revisions

From Heroes 3 wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
mw.loader.using('mediawiki.util').then(function () {
(function () {
   var cache = Object.create(null);
   var cache = Object.create(null);


   function toAudioUrl(href) {
   function toAudioUrl(href) {
     if (!href) return null;
     if (!href) return null;
     var abs = new URL(href, location.href).toString();
     var abs = new URL(href, location.href).toString();


Line 11: Line 12:
     if (m) {
     if (m) {
       var title = decodeURIComponent(m[2]);
       var title = decodeURIComponent(m[2]);
       return mw.util.getUrl('Special:FilePath/' + title);
       var base = location.origin + location.pathname.replace(/\/index\.php.*|\/wiki\/.*|$/, '');
      return location.origin + '/wiki/Special:FilePath/' + encodeURIComponent(title);
     }
     }


Line 19: Line 21:
   }
   }


   $(document).on('click', '.click-audio', function (e) {
   document.addEventListener('click', function (e) {
    var wrap = e.target.closest('.click-audio');
    if (!wrap) return;
 
     if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
     if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;


     var $a = $(this).find('a').first();
     var a = wrap.querySelector('a');
     if ($a.length === 0) return;
     if (!a) return;


     var url = toAudioUrl($a.attr('href'));
     var url = toAudioUrl(a.getAttribute('href'));
     if (!url) return;
     if (!url) return;


     e.preventDefault();
     e.preventDefault();
     e.stopImmediatePropagation();
     e.stopPropagation();
    try { e.stopImmediatePropagation(); } catch (_) {}


     var a = cache[url] || (cache[url] = new Audio(url));
     var audio = cache[url] || (cache[url] = new Audio(url));
     try {
     try {
       a.currentTime = 0;
       audio.currentTime = 0;
       a.play && a.play();
       var p = audio.play && audio.play();
      if (p && p.catch) p.catch(function(){});
     } catch (_) {}
     } catch (_) {}
   });
   }, true);
 
})();
  mw.hook('wikipage.content').add(function ($c) {
    $c.find('.click-audio')
      .css('cursor', 'pointer')
      .attr({ role: 'button', tabindex: 0 })
      .on('keydown', function (e) {
        if (e.which === 13 || e.which === 32) {
          e.preventDefault();
          $(this).trigger('click');
        }
      });
  });
});

Revision as of 01:52, 10 September 2025

(function () {
  var cache = Object.create(null);

  function toAudioUrl(href) {
    if (!href) return null;

    var abs = new URL(href, location.href).toString();

    if (/\.(mp3|wav|ogg|m4a|webm)(\?|#|$)/i.test(abs)) return abs;

    var m = abs.match(/\/(File|Media):([^?#]+)/i);
    if (m) {
      var title = decodeURIComponent(m[2]);
      var base = location.origin + location.pathname.replace(/\/index\.php.*|\/wiki\/.*|$/, '');
      return location.origin + '/wiki/Special:FilePath/' + encodeURIComponent(title);
    }

    if (/\/Special:FilePath\//i.test(abs)) return abs;

    return null;
  }

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

    if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;

    var a = wrap.querySelector('a');
    if (!a) return;

    var url = toAudioUrl(a.getAttribute('href'));
    if (!url) return;

    e.preventDefault();
    e.stopPropagation();
    try { e.stopImmediatePropagation(); } catch (_) {}

    var audio = cache[url] || (cache[url] = new Audio(url));
    try {
      audio.currentTime = 0;
      var p = audio.play && audio.play();
      if (p && p.catch) p.catch(function(){});
    } catch (_) {}
  }, true);
})();