Skip to content

Instantly share code, notes, and snippets.

@devongovett
Created February 2, 2014 23:14
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devongovett/8776453 to your computer and use it in GitHub Desktop.
Save devongovett/8776453 to your computer and use it in GitHub Desktop.
Use PhantomJS to translate stdin to english using Google Translate. Useful as a textmate command or just a command line tool.
#!/usr/bin/env phantomjs
var system = require('system');
var text = encodeURIComponent(system.stdin.read());
var url = "http://translate.google.com/#auto/en/" + text;
var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0';
page.onConsoleMessage = function (msg) {
system.stdout.write(msg);
};
page.open(url, function (status) {
if (status !== 'success') {
console.log('Unable to access network');
return;
}
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js", checkChange);
function checkChange() {
var done = page.evaluate(function () {
var result = $('#result_box').text();
if (result && result.trim() != ""){
console.log(result);
return true;
}
return false;
});
if (!done)
checkChange();
else
phantom.exit();
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment