Created
September 20, 2009 03:58
-
-
Save weihsiu/189706 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
// ==UserScript== | |
// @name Twitter Translate | |
// @namespace http://www.netgents.com/twitter-translate | |
// @description Translate tweets in a foreign language to English | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js | |
// @include http://twitter.com/* | |
// ==/UserScript== | |
$(document).ready(function() { | |
function addTranslate() { | |
$("li > span.status-body > span.meta").filter(function() { | |
return !$(this).hasClass("retweet-meta") && $(this).children("a.translate").length == 0; | |
}).append("<a style='color:gold;font-style:italic' class='translate' href=''>translate</a>"); | |
} | |
function setup() { | |
GM_log("locale = " + navigator.language); | |
var language = unsafeWindow.google.language; | |
addTranslate(); | |
$("a[class='translate']").live("click", function(event) { | |
var content = $(this).parent().prev(); | |
language.detect(content.text(), function(result) { | |
if (!result.error && result.language) { | |
language.translate(content.text(), result.language, "en", function(result) { | |
content.text(result.translation); | |
content.css("background-color", "yellow"); | |
}); | |
} | |
}); | |
event.preventDefault(); | |
return false; | |
}); | |
window.setInterval(addTranslate, 5000); | |
} | |
unsafeWindow.doneLoadingScript = function() { | |
unsafeWindow.google.load("language", "1", {"callback": setup}); | |
} | |
var script = document.createElement("script"); | |
script.src = "http://www.google.com/jsapi?callback=doneLoadingScript"; | |
script.type = "text/javascript"; | |
document.getElementsByTagName("head")[0].appendChild(script); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment