Skip to content

Instantly share code, notes, and snippets.

@camiloaa
Forked from halfdan/gist:be037a9c37c50e3924a9
Last active January 12, 2016 11:51
Show Gist options
  • Save camiloaa/ece4f18a2db61764d63c to your computer and use it in GitHub Desktop.
Save camiloaa/ece4f18a2db61764d63c to your computer and use it in GitHub Desktop.
Duolingo Google TTS
//==UserScript==
//@name LingoGoogleTTS
//@version 1.2
//@description Use Google TTS for Duolingo
//@match https://www.duolingo.com/*
//@match http://www.duolingo.com/*
//@grant none
//==/UserScript==
function googleTTSLang(targetLang) {
if (targetLang == "dn") { return "nl"; }
if (targetLang == "zs") { return "zh"; }
return targetLang;
}
(function($) {
if (typeof duo != 'undefined' && typeof $.tts_super == 'undefined') {
var ttsBase = duo.tts_base_url, ttsPath = duo.tts_path;
$.fn.tts_super = $.fn.tts;
$.fn.tts = function(d) {
var language = d.language;
var gRand = function () {
return Math.floor(Math.random() * 1000000) + '|' +
Math.floor(Math.random() * 1000000) };
duo.tts_base_url = "http://translate.google.com/translate_tts?ie=UTF-8&";
if (d.tts_type === "sentence" && typeof d.sentence !== 'undefined') {
var quoted_text = encodeURIComponent(d.sentence.replace("/"," "));
d.language = googleTTSLang(d.language);
duo.tts_path = 'tl=' + d.language + '&q=' + quoted_text +
"&total=1&textlen=" + quoted_text.length +
"&tk=" + gRand() + "&client=t";
console.log("Requesting "+duo.tts_path);
} else {
// Use default for tokens (single words)
duo.tts_base_url = ttsBase;
duo.tts_path = ttsPath;
}
return this.each(function() {
$(this).tts_super(d);
});
}
}
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment