Created
January 29, 2011 10:54
-
-
Save mooz/801744 to your computer and use it in GitHub Desktop.
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
// http://d.hatena.ne.jp/sr10/20110129/1296290450 | |
(function(){ | |
function translate(word, next) { | |
display.echoStatusBar(word + " getting..."); | |
const base = "https://www.googleapis.com/language/translate/v2?key=%s&q=%s&source=%s&target=%s"; | |
const apikey = "AIzaSyBq48p8NhFgaJ1DfUJ5ltbwLxeXpjEL86A"; | |
let ep = util.format(base, apikey, encodeURIComponent(word), "en", "ja"); | |
util.httpGet(ep, false, function (res) { | |
let result = ""; | |
if (res.status === 200) { | |
let json = decodeJSON(res.responseText); | |
result = json.data.translations[0].translatedText; | |
} else { | |
result = "ERROR!"; | |
} | |
next(result); | |
}); | |
}; | |
function echo(from, to){ | |
display.echoStatusBar(from + " " + to); | |
}; | |
function decodeJSON(json) { | |
return util.safeEval("(" + json + ")"); | |
}; | |
function lookupword(word){ | |
translate(word, function (translated) { | |
echo(word, translated); | |
}); | |
}; | |
function read (aInitialInput) { | |
let prevText = ""; | |
prompt.reader({ | |
message : "word or sentence to translate:", | |
initialinput : aInitialInput, | |
keymap : {"C-RET" : "lookup"}, | |
actions : [[function(arg){lookupword(arg.textbox.value);},"lookup","lookup,c"]], | |
onChange: function (arg) { | |
let word = arg.textbox.value; | |
if (word !== prevText) { | |
prevText = word; | |
lookupword(word); | |
} | |
}, | |
callback: function (s){ | |
lookupword(s); | |
} | |
}); | |
}; | |
ext.add("google-itranslate",function(){read(content.document.getSelection() || "");},"google itranslate"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment