Jetpack Gallery
Jetpacks
Contributors
About
Register
Log In
View Source Code for “
GTranslatifier
”
/* * GTranslatifier Jetpack feature v0.2.1.2 * Translate pages or selected text using Google Translate. * * By Elijah Grey, http://eligrey.com * * Inspired by "Jetpack: Google Translator" version 0.1 by Florian CROUZAT: * http://jetpack.floriancrouzat.net/google-translator/google-translator.html * * License: GNU GPL v3 and the X11/MIT license * See http://purl.eligrey.com/license */ const prefs = Components.classes["@mozilla.org/preferences-service;1"] .getService(Components.interfaces.nsIPrefService), gt = { // GTranslatifier namespace name: "GTranslatifier", translate: function (clickEvent) { var tab = jetpack.tabs.focused, selection = String(tab.contentWindow.getSelection()) .replace(/^\s+|\s+$/g, ""), // trim whitespace URI = tab.url, button = clickEvent.button, translatedURI = "http://translate.google.com/translate"; if (tab.contentWindow.location.hostname === "translate.google.com") { // un-translate page tab.contentWindow.location.assign(decodeURIComponent( URI.split("u=")[1].split("&")[0] )); return; } if (selection.length > 0) { // translate selection (open new tab) translatedURI += "_t" + "?text=" + encodeURIComponent(selection) + "&hl=" + gt.userLocale + "&langpair=auto|" + gt.userLocale + "&tbb=1"; gt.open(translatedURI); return } else { // translate page translatedURI += "?u=" + encodeURIComponent(URI) + "&hl=" + gt.userLocale + "&langpair=auto|" + gt.userLocale + "&tbb=1"; if (button) { // right or middle click, open in new tab gt.open(translatedURI); } else { // left click, open in same tab tab.contentWindow.location.assign(translatedURI); } return; } }, open: function (URI) { jetpack.tabs.open(URI).focus(); }, // To get the supported locales: // load http://translate.google.com/ /* Run: var opts = document.querySelector(".tllangdropdown").childNodes, locales = {}, // object instead of array to remove dupes localesArr = [], i = opts.length, locale; while (i--) { if (!opts.item(i).hasAttribute("disabled")) { locales[opts.item(i).getAttribute("value")] = null; } } for (locale in locales) { if (locales.hasOwnProperty(locale)) { localesArr.push(locale); } } prompt("Locales", JSON.stringify(localesArr.sort()).replace(/,\s{0,}/g, ", ")); */ supportedLocales: [ "af", "ar", "be", "bg", "ca", "cs", "cy", "da", "de", "el", "en", "es", "et", "fa", "fi", "fr", "ga", "gl", "hi", "hr", "hu", "id", "is", "it", "iw", "ja", "ko", "lt", "lv", "mk", "ms", "mt", "nl", "no", "pl", "pt", "ro", "ru", "sk", "sl", "sq", "sr", "sv", "sw", "th", "tl", "tr", "uk", "vi", "yi", "zh-CN", "zh-TW" ], // 17x18 Google Translate icon icon: .toString().replace(/\s/g, ""), userLocale: prefs.getBranch("general.useragent.").getCharPref("locale") }; if (gt.supportedLocales.indexOf(gt.userLocale) === -1) { // Google Translate doesn't support the language, try non-region-specific gt.userLocale = gt.userLocale.split("-")[0]; if (gt.supportedLocales.indexOf(gt.userLocale) === -1) { // still not supported gt.userLocale = "en"; // default to en (English) } } // GTranslatifier widget jetpack.statusBar.append({ html:
.toXMLString(), onReady: function (widget) { widget.addEventListener("click", gt.translate, false); } });