Skip to content

Instantly share code, notes, and snippets.

@stpettersens
Last active August 20, 2018 23:11
Show Gist options
  • Save stpettersens/8442918f3dbd7268b7eabd74e1ce191e to your computer and use it in GitHub Desktop.
Save stpettersens/8442918f3dbd7268b7eabd74e1ce191e to your computer and use it in GitHub Desktop.
User script to autoplay audio prompts on Duolingo.
// ==UserScript==
// @name Duolingo Autoplay audio
// @namespace 8442918f3dbd7268b7eabd74e1ce191e
// @version 0.1
// @description Autoplay the audio prompts on Duolingo.
// @author Sam Saint-Pettersen <[email protected]>
// @match https://www.duolingo.com/*
// @icon https://s32.postimg.org/8zxj3evit/duolingo.png
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==
const prefix = 'duolingo_autoplay_';
const played = prefix + 'played';
const disabled = prefix + 'disabled';
const toggler = prefix + 'toggler';
let timer = null;
let autoplay_btn = '<br><input style="margin-top: 5px; border: solid #1CAFF6 1px !important" checked="checked"';
autoplay_btn += ' id="autoplay_prompt" type="checkbox" value="1"></input>';
autoplay_btn += '<abbr style="color: #999; font-size: 9pt;"';
autoplay_btn += ' title="Audio prompts will autoplay if checked.">Autoplay?</abbr>';
function resetToggles() {
clearInterval(timer);
sessionStorage.setItem(played, 0);
sessionStorage.setItem(toggler, 0);
}
function playAudioPrompt() {
if(parseInt(sessionStorage.getItem(played)) === 0 &&
parseInt(sessionStorage.getItem(disabled)) !== 1) {
$('.icon-speaker-small').trigger('click');
sessionStorage.setItem(played, 1);
}
if(parseInt(sessionStorage.getItem(toggler)) === 0) {
let speaker = $('.icon-speaker-small');
$('.icon-speaker-small').replaceWith(speaker);
$('.icon-speaker-small').append(autoplay_btn);
sessionStorage.setItem(toggler, 1);
}
setAutoplay();
$('#autoplay_prompt').on('click', function() {
toggleAutoplay();
});
}
function playNextAudioPrompt() {
resetToggles();
timer = setTimeout(playAudioPrompt, 2500);
}
function toggleAutoplay() {
if($('#autoplay_prompt').is(':checked')) {
sessionStorage.setItem(disabled, 0);
} else {
sessionStorage.setItem(disabled, 1);
}
}
function setAutoplay() {
if(parseInt(sessionStorage.getItem(disabled)) === 1) {
$('#autoplay_prompt').removeAttr('checked');
} else {
$('#autoplay_prompt').attr('checked', 'checked');
}
}
(function() {
'use strict';
$(document).ready(function() {
playNextAudioPrompt();
$('#next_button').on('click', function() {
playNextAudioPrompt();
});
$(document).on('keypress', function(event) {
if(event.which === 13) {
playNextAudioPrompt();
}
});
$('a').on('click', function() {
resetToggles();
});
$('button').on('click', function() {
resetToggles();
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment