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 2: Line 2:
   var cache = Object.create(null);
   var cache = Object.create(null);


   function playFromHref(href) {
   function toAudioUrl(href) {
     if (!href) return;
     if (!href) return null;
     var url = new URL(href, location.href).toString();
     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]);
      return mw.util.getUrl('Special:FilePath/' + title);
    }
 
    if (/\/Special:FilePath\//i.test(abs)) return abs;
 
    return null;
  }
 
  $(document).on('click', '.click-audio', function (e) {
    if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
 
    var $a = $(this).find('a').first();
    if ($a.length === 0) return;
 
    var url = toAudioUrl($a.attr('href'));
    if (!url) return;
 
    e.preventDefault();
    e.stopImmediatePropagation();
 
     var a = cache[url] || (cache[url] = new Audio(url));
     var a = cache[url] || (cache[url] = new Audio(url));
     try {
     try {
Line 10: Line 36:
       a.play && a.play();
       a.play && a.play();
     } catch (_) {}
     } catch (_) {}
  }
  $(document).on('click', '.click-audio a.image', function (e) {
    var href = $(this).attr('href') || '';
    if (/\/(Media:|Special:FilePath\/)/i.test(href) || /\.(mp3|wav|ogg|m4a|webm)(\?|#|$)/i.test(href)) {
      e.preventDefault();
      playFromHref(href);
    }
   });
   });


Line 23: Line 41:
     $c.find('.click-audio')
     $c.find('.click-audio')
       .css('cursor', 'pointer')
       .css('cursor', 'pointer')
       .attr({ role: 'button', tabindex: 0 });
       .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:49, 10 September 2025

mw.loader.using('mediawiki.util').then(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]);
      return mw.util.getUrl('Special:FilePath/' + title);
    }

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

    return null;
  }

  $(document).on('click', '.click-audio', function (e) {
    if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;

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

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

    e.preventDefault();
    e.stopImmediatePropagation();

    var a = cache[url] || (cache[url] = new Audio(url));
    try {
      a.currentTime = 0;
      a.play && a.play();
    } catch (_) {}
  });

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