Skip to content

Instantly share code, notes, and snippets.

@shivprasad
Created July 18, 2011 09:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shivprasad/1088985 to your computer and use it in GitHub Desktop.
Save shivprasad/1088985 to your computer and use it in GitHub Desktop.
A simple userscript which looks up selected word on google dictionary and alerts its meaning.
// ==UserScript==
// @name Google Dictionary
// @namespace http://byteco.de
// @include *
// ==/UserScript==
var t = '';
function gText(e) {
t = (document.all) ? document.selection.createRange().text : document.getSelection();
t = t.replace(/^\s+|\s+$/g,"");
if(t && t.indexOf(' ') == -1){
GM_xmlhttpRequest({
method: "GET",
url: "http://www.google.com/dictionary/json?callback=callback&q="+t+"&sl=en&tl=en&restrict=pr%2Cde&client=te",
onload: function(response) {
var data = eval("(" + response.responseText + ")");
}
});
}
}
function callback(data,http,flag){
var meaning = null;
try{
meaning = data.primaries[0].entries[1].terms[0].text;
if(meaning) alert(meaning);
}catch(e){
}
}
document.addEventListener("mouseup",gText,false);
@DanKaplanSES
Copy link

DanKaplanSES commented May 20, 2022

Just want to point out for others that this is eleven years old and no longer works. I tried to modernize it, but didn't want to put in the effort to figure out what happened to http://www.google.com/dictionary. It 404s. I may look into this later. Here's as far as I got:

// ==UserScript==
// @name           Google Dictionary
// @namespace      http://byteco.de
// @include        *
// @grant        GM_xmlhttpRequest
// ==/UserScript==

/* TODO: WIP google api has changed. now 404s */

function getSelectionText() {
    var text = "";
    var activeEl = document.activeElement;
    var activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;
    if (
      (activeElTagName == "textarea") || (activeElTagName == "input" &&
      /^(?:text|search|password|tel|url)$/i.test(activeEl.type)) &&
      (typeof activeEl.selectionStart == "number")
    ) {
        text = activeEl.value.slice(activeEl.selectionStart, activeEl.selectionEnd);
    } else if (window.getSelection) {
        text = window.getSelection().toString();
    }
    return text;
}


let t = '';
function gText(e) {
    t = getSelectionText();
	t = t.replace(/^\s+|\s+$/g,"");
	if(t && t.indexOf(' ') == -1){
	GM_xmlhttpRequest({
      method: "GET",
      url: "http://www.google.com/dictionary/json?callback=callback&q="+t+"&sl=en&tl=en&restrict=pr%2Cde&client=te",
      onload: function(response) {
          console.log("response.responseText", response.responseText);
		var data = eval("(" + response.responseText + ")");
        }
    });
	}
}

function callback(data,http,flag){
	var meaning = null;
	try{
		meaning = data.primaries[0].entries[1].terms[0].text;
		if(meaning) alert(meaning);
	}catch(e){
	}
}



document.addEventListener("mouseup",gText,false);

@shivprasad
Copy link
Author

Yup it was done a long time back. Google since has released it's own extension which offers similar feature - https://chrome.google.com/webstore/detail/google-dictionary-by-goog/mgijmajocgfcbeboacabfgobmjgjcoja

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment