User:Nardog/PlayAudioNow.js

/* mw.config.get('wgAction') !== 'history' && mw.loader.using('mediawiki.api', function playAudioNow {	mw.loader.addStyleTag('.playaudionow{background-image:url(//upload.wikimedia.org/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.wikimedia.org/wikipedia/commons/8/87/MaterialThrobber.svg)} .playaudionow-playing{background-image:url(//upload.wikimedia.org/wikipedia/commons/6/6b/OOjs_UI_icon_pause-progressive.svg)} .playaudionow-playing.playaudionow-pausable.playaudionow-rewindable{background-image:url(//upload.wikimedia.org/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 => $(' ', { src: derivative.src, type: derivative.type }));				if (!sources.length) return; let 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);	}); }); */