Filippo / Dizionario

// ==UserScript==
// @name         Dizionario
// @namespace    http://your.homepage/
// @version      0.8.2
// @description  Press e and i to lookup term
// @author       Filippo Pagano
// @match        http://tampermonkey.net/faq.php?version=3.9&ext=dhdg&show=gcal
// @grant        none
// @include         *
// @updateURL    https://openuserjs.org/install/Filippo/Dizionario.user.js
// ==/UserScript==

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

window.onkeypress = function(event) { 
	var code = (event.keyCode ? event.keyCode : event.which);
	
if (getSelectionText() && (document.activeElement.nodeName != 'TEXTAREA')){
   if (code == 105) {
		var win = window.open('http://www.wordreference.com/enit/' + getSelectionText(), 'dizionario', '_blank');
		if(win){
			//Browser has allowed it to be opened
			win.focus();
		}else{
			//Broswer has blocked it
			alert('Please allow popups for this site');
		}

   } else if (code == 101) {
       
    var win = window.open('http://www.wordreference.com/iten/' + getSelectionText(), 'dizionario', '_blank');
	if(win){
		//Browser has allowed it to be opened
		win.focus();
	}else{
		//Broswer has blocked it
		alert('Please allow popups for this site');
	}

   
       
   } else if (code == 119) {
       
    var win = window.open('http://it.wikipedia.org/wiki/' + getSelectionText(), 'dizionario', '_blank');
	if(win){
		//Browser has allowed it to be opened
		win.focus();
	}else{
		//Broswer has blocked it
		alert('Please allow popups for this site');
	}

   
       
   } else if (code == 87) {
       
    var win = window.open('http://en.wikipedia.org/wiki/' + getSelectionText(), 'dizionario', '_blank');
	if(win){
		//Browser has allowed it to be opened
		win.focus();
	}else{
		//Broswer has blocked it
		alert('Please allow popups for this site');
	}

   
       
   } else if (code == 100) {
       
    var win = window.open('http://en.wiktionary.org/wiki/' + getSelectionText(), 'dizionario', '_blank');
	if(win){
		//Browser has allowed it to be opened
		win.focus();
	}else{
		//Broswer has blocked it
		alert('Please allow popups for this site');
	}
       
   } else if (code == 103) {
       
    var win = window.open('https://www.google.it/#safe=off&q=' + getSelectionText(), 'dizionario', '_blank');
	if(win){
		//Browser has allowed it to be opened
		win.focus();
	}else{
		//Broswer has blocked it
		alert('Please allow popups for this site');
	}
   
       
   } 
}else if(code == 27 && location.hostname.split('.').slice(1).join('.') == "wordreference.com") {
		 window.close();
		 
	 }       
}