Created
January 27, 2009 16:04
-
-
Save ecarnevale/53394 to your computer and use it in GitHub Desktop.
Ubiquity command to utilize http://json-zh.appspot.com service and get pinyin reading for Chinese characters.
This file contains 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
var noun_type_destination = new CmdUtils.NounType( "destination", ['clipboard', 'selected text', 'displayed message'] ); | |
CmdUtils.CreateCommand({ | |
name: "get-pinyin", | |
takes: {"Hanzi to read as PinYin": noun_arb_text}, | |
preview: function(pblock, hanzi) { | |
searchHanzi = jQuery.trim(hanzi.text); | |
if (searchHanzi.length < 1) { | |
pblock.innerHTML = "Gets the <b>Pinyin reading</b> of selected Chinese characters."; | |
return; | |
} | |
pblock.innerHTML = "Gets the <b>Pinyin reading</b> of " + hanzi.text + "."; | |
}, | |
modifiers: {to: noun_type_destination}, | |
homepage: "http://json-zh.appspot.com/", | |
author: { name: "Emanuel Carnevale", email: "me AT myName myFamilyName DOT com"}, | |
description: "Gets the most common pinyin reading of the selected Chinese ideograms (hanzi) connecting to http://json-zh.appspot.com", | |
help: "You can specify where to send the result, to a displayed message, to the clipboard or to the selected Chinese Ideogram thus replacing it.", | |
execute: function(hanzi, destination) { | |
var baseURL = "http://json-zh.appspot.com/pinyin"; | |
var params = {hanzi: hanzi.text}; | |
jQuery.get(baseURL,params,function(data){ | |
try { | |
var pinyinText = Utils.decodeJson(data); | |
} catch(e) { | |
displayMessage("Errore connecting to http://json-zh.appspot.com/!"); | |
} finally { | |
var toDestination = destination.to.text || "displayed message"; | |
switch (toDestination){ | |
case "clipboard": | |
CmdUtils.copyToClipboard(pinyinText.pinyin); | |
displayMessage(pinyinText.pinyin + " successfully copied to the clipboard"); | |
break; | |
case "selected text": | |
CmdUtils.setSelection(pinyinText.pinyin); | |
break; | |
case "displayed message": | |
displayMessage(pinyinText.pinyin); | |
break; | |
default : displayMessage(pinyinText.pinyin); | |
} | |
} | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment