Skip to content

Instantly share code, notes, and snippets.

@weihsiu
Created September 20, 2009 03:58
Show Gist options
  • Save weihsiu/189706 to your computer and use it in GitHub Desktop.
Save weihsiu/189706 to your computer and use it in GitHub Desktop.
// ==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