Jump to content

User:IagoQnsi/ipareader.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// powered by <ipa-reader dot xyz>
// full list of voices at https://docs.aws.amazon.com/polly/latest/dg/voicelist.html

function loadConfig() {
	var config = window.ipareaderConfig;
	if (typeof config === 'undefined') {
		config = {};
	}
	if (typeof config.voice === 'undefined') {
		config.voice = 'Salli';
	}
	return config;
}

function speak(text, voice) {
	var requestData = { text: text, voice: voice };
	$.ajax({
		url: "https://iawll6of90.execute-api.us-east-1.amazonaws.com/production",
		method: "POST",
		contentType: "application/json",
		data: JSON.stringify(requestData),
		success: function(data) {
			var uri = "data:audio/mpeg;base64," + data;
			(new Audio(uri)).play();
		}
	});
}

(function() {
	var config = loadConfig();
	mw.util.addCSS('.ipareader-play { cursor:pointer; margin-left: 0.25em; }');
	
	$('.IPA').each(function() {
		var button = $('<span role="button" class="ipareader-play" title="IPA pronunciation">🔈</span>');
		$(this).append(button);
		button.click(function() {
			var text = $(this).parent().text();
			speak(text, config.voice);
		});
	});
})();