Small Google Chrome Inline translate extension

Today I noticed, that I often want to translate some text in Google Chrome but not the entire page. As you know, you can translate the entire page by clicking “Translate to …” in the context menu of Google Chrome. But if you want to translate just a single word or sentence you have to go to and enter the text.

So digged into the Google Chrome extension API and after a short amount of time I had my own little Inline Translate Extension, wich translates to german.
Here is the Code:


  "name": "Inline Translate",
  "version": "0.1",
  "description": "Translate selected Text with Google Translate.",
  "permissions": [
  "background_page": "background.html"


<script src="inlineTranslate.js"></script>


var API_KEY = "Your Google Translate API Key";

function translateSelection(info, tab) {
    var textToTranslate = escape(info["selectionText"]);
    var url = "" + API_KEY + "&target=de&q=" + textToTranslate;
    var xhr = new XMLHttpRequest();"GET", url, true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            var resp = JSON.parse(xhr.responseText);
            var translation =[0]["translatedText"];

            if (translation.length > 0) {
                var notification = webkitNotifications.createNotification('', 'Translation result:', translation);


var contextMenu = chrome.contextMenus.create({
    "title": "Translate",
    "contexts": ["selection"],
    "onclick": translateSelection

This was my first experience with Google Chrome extensions and I liked it 🙂

This entry was posted in Google Chrome, Javascript. Bookmark the permalink.