Ente

Übersetze ausgewählte englische Worte nach Deutsch mit dict.cc.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        Ente
// @namespace   meyerk.com
// @match       *://*/*
// @grant       none
// @version     0.6
// @author      MeyerK
// @description Übersetze ausgewählte englische Worte nach Deutsch mit dict.cc.
// @noframes
// ==/UserScript==

class ente
{
  
  constructor()
  {
    this.rightAltKeyIsOn = false;
    this.isVisible = false;
    this.frame = document.createElement('iframe');      
    this.width = 300;
  }
  
  setup()
  {
    this.frame.id = 'enteFrame';
    this.frame.style.position = 'fixed';
    this.frame.style.top = '10px';
    this.frame.style.width = this.width+'px';
    this.frame.style.padding = '5px';
    this.frame.style.height = '100px';
    this.frame.style.border = '1px solid blue';
    this.frame.style.backgroundColor = 'aliceblue';
    this.frame.style.display = 'none';
    this.frame.style.borderRadius = '5px';
    this.frame.style.zIndex = '2147483646';
        
    document.getElementsByTagName('body')[0].appendChild(this.frame);
  }
  
  handleKeys(ev)
  {     
    if (this.rightAltKeyIsOn)
    {
      if (ev.code == 'KeyU')
      {
        let text = window.getSelection().toString();
        this.frame.src = "//syn.dict.cc/dcc-gadget.php?s=" + encodeURIComponent(text);
        this.show();
        
        return false;
      }
    }
    
    if (ev.code == 'AltRight')
    {
      this.rightAltKeyIsOn = (ev.type == 'keydown') ? true : false; 
    }    
  }

  handleMouse(ev)
  {
    if (this.isVisible)
    {      
      this.hide();    
      
      ev.preventDefault();
      return false;
    }    
  }
  
  show()
  {
    let leftPos = parseInt(window.innerWidth / 2) - parseInt(this.width / 2);    
    this.frame.style.left = leftPos+'px';    
    this.frame.style.display = 'block';
    this.isVisible = true;    
  }
  
  hide()
  {
    this.frame.style.display = 'none';
    this.isVisible = false;
  }
    
}

var en2de = new ente();
en2de.setup();
document.addEventListener('keydown', en2de.handleKeys.bind(en2de));
document.addEventListener('keyup',   en2de.handleKeys.bind(en2de));
document.addEventListener('click',   en2de.handleMouse.bind(en2de));