Created
February 2, 2014 23:14
-
-
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.
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
#!/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