User:Loki Laufeyjarson/common.js
Jump to navigation
Jump to search
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
(function () {
function resolveUrl($el) {
var u = $el.attr('data-audio-url');
if (u) return u;
var f = $el.attr('data-audio-file');
if (!f || /\{\{/.test(f)) return null; // don’t play if a template param didn’t expand
return mw.util.getUrl('Special:FilePath/' + f);
}
var cache = {};
mw.hook('wikipage.content').add(function ($c) {
$c.on('click', '.click-audio', function (e) {
e.preventDefault();
var u = resolveUrl($(this));
if (!u) return;
var a = cache[u] || (cache[u] = new Audio(u));
a.currentTime = 0;
a.play && a.play();
});
$c.find('.click-audio').css('cursor', 'pointer').attr({ role: 'button', tabindex: 0 });
});
})();