Created
March 25, 2009 04:07
-
-
Save weihsiu/84553 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CmdUtils.CreateCommand({ | |
name: "pronounce", | |
author: { name: "Walter Chang", email: "[email protected]" }, | |
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