DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Translate English Words in Browser and Console


Everytime I want to get the meaning of a english word, I have to copy the word, open dict.cn, paste the word into the box and get the meaning, which is boring. Here we demonstrate with the word "spoiler".

Chrome

Right click in Chrome's address bar, and select "Edit search engines". Find "dict.cn", set its title to "dict.cn" (in the 1st column), set the shortcut name to "dc" (the 2nd column, whose origin value is "dict.cn"), remain the command as http://dict.cn/%s (in the 3rd column).

Now input "dc" in address bar and press Space key, the address bar title is now "Search Dict.CN", and input "spoiler".

BTW: With the same method, define Google with shortcut "gg" and command "https://www.google.com/search?q=%s", and press "Make default". Now input a word in address bar and press Enter, you will get the search result from Google.

Console

Translate Shell

Install Translate Shell:

gawk --version          # should above 4.0
wget git.io/trans -O ~/.local/bin/trans
chmod 755 ~/.local/bin/trans
trans -V                # print versions
trans -H                # print help message
trans :zh muse          # translate with default engine: google
trans -S                # list translation engines
trans -e bing :zh muse  # search with MS Bing
trans -T                # list available languages
trans -U                # upgrade this program
trans -I                # shell mode, useful when reading a paper

Using Configuration to change its default behavior:

cat << EOF > ~/.config/translate-shell/init.trans
{
 :verbose         true
 :hl              "en"
 :tl              "zh"
}
EOF

Now trans muse equals to 'trans :zh muse'. If you want using the brief mode as the default behavior (trans -b :zh muse), change :verbose in 'init.trans' to false.

ici

Install ici with sudo pip install ici, run it with ici spoiler.

Firefox

While there're some google translate extensions of firefox, but they are slow and not very complete. For example: the word meaning of "spoiler" in "This synopsis may contain spoilers." on page Synopsis for Memento. So I have to build my own translate extension based on dict.cn.

Steps

  1. Install Greasemonkey extension in Firefox.

  2. Build a new script with the following codes.

  3. Select some text and press "\" key.

    // ==UserScript== // @name TranslateIt // @namespace LeoScript // @include * // @version 1 // @grant none // ==/UserScript==

    // document.addEventListener('mouseup', getSel, false); document.addEventListener('keypress', transIt, false);

    function transIt(e) { // console.log(e.key); if (e.key !== '\'){ return; } var sel = document.getSelection().toString(); if (sel) { window.open('http://dict.cn/' + sel); } }

Sometimes the popuped windows will be blocked by the browser. Add the website into the whitelist of the blocked sites.

To debug the script, modify and save the script, reload the web page, and test the script.

References:

Greasemonkey Tutorial for Beginners

Get selected text:

greasemonkey: addEventListener questions

HTML DOM addEventListener() Method

Test if ctrl or alt key is pressed on clicking:

JavaScript: Check if CTRL button was pressed

search 'altKey' in HTML DOM Events

Open a URL in a new tab using JavaScript



Published

Sep 12, 2015

Last Updated

Sep 12, 2015

Category

Tech

Tags

  • browser 4
  • chrome 9
  • extension 2
  • firfox 1
  • translate 3

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor