Skip to content

Instantly share code, notes, and snippets.

@weihsiu
Created March 25, 2009 04:07
Show Gist options
  • Save weihsiu/84553 to your computer and use it in GitHub Desktop.
Save weihsiu/84553 to your computer and use it in GitHub Desktop.
CmdUtils.CreateCommand({
name: "pronounce",
author: { name: "Walter Chang", email: "weihsiu@gmail.com" },
license: "Creative Commons 3.0 (by nc)",
description: "Pronounce a selected or typed English word",
takes: {"word": noun_arb_text},
preview: function(pblock, text) {
var msg = 'Pronounces "${word}"';
var subs = { word: this._pickWord(text.text) };
pblock.innerHTML = CmdUtils.renderTemplate(msg, subs);
},
execute: function(text) {
var word = this._pickWord(text.text);
if (word != "") {
displayMessage('Pronouncing "' + word + '"');
this._pronounce(word);
}
},
_pickWord: function(text) { return (text || CmdUtils.getSelection()).split(/\b/)[0]; },
_pronounce: function(word) {
function playSound(url) {
var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var sound = Components.classes["@mozilla.org/sound;1"].createInstance(Components.interfaces.nsISound);
sound.play(ioService.newURI(url, "", null));
}
function trim(s) {
return s.replace(/^\s+|\s+$/g, "");
}
jQuery.get("http://www.merriam-webster.com/dictionary/" + trim(word.toLowerCase()), null, function(content) {
var matches = /'\/cgi-bin\/audio\.pl\?(.+?)=.+?'/g.exec(content);
if (matches != null) {
var soundFile = matches[1];
playSound("http://cougar.eb.com/soundc11/" + soundFile[0] + "/" + soundFile);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment