SpanishDict Quick Translatoin

When double click a word, show it's meanings

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         SpanishDict Quick Translatoin
// @namespace    http://tampermonkey.net/
// @version      0.2.4
// @description  When double click a word, show it's meanings
// @author       Andy
// @match        http://www.spanishdict.com/*
// @exclude        http://www.spanishdict.com/answers/*
// ==/UserScript==

(($) => {
    $(() => {
      $('span.icon-dash').text(' ');
    });
    const tipUrl = 'http://www.spanishdict.com/dictionary/quick_translation?word=';
    const dictUrl = 'http://www.spanishdict.com/translate/';
    const audioUrl = 'http://audio1.spanishdict.com/audio?lang=es&text=';
    let timeoutHandle;
    const playSound = (url) => {
        var audio = document.createElement('audio');
        audio.style.display = "none";
        audio.src = url;
        audio.autoplay = true;
        audio.onended = function(){
            audio.remove(); //Remove when played.
        };
        document.body.appendChild(audio);
    };
    const hideTip = () => {
        clearTimeout(timeoutHandle);
        $('body>.tooltip').remove();
    };
    const showTip = (source, tip, x, y) => {
        hideTip();
        playSound(audioUrl + source);
        if (tip.length === 0) {
            tip = 'no quick translation found';
        } else {
            tip = `<a style="color:#fff" target="spanishdict" href="${dictUrl + source}">${tip}</a>`;
        }
        const $tip = $(`<div class="tooltip fade top in" style="display: block;"><div class="tooltip-arrow"></div><div class="tooltip-inner">${tip}</div></div>`);
        $('body').append($tip);
        let width = $tip.outerWidth();
        let height = $tip.outerHeight();
        $tip.css({ left: x - width / 2, top: y - height - 5 });
        timeoutHandle = setTimeout(hideTip, 5000);
    };
    $('body').click(hideTip);
    $('.main-container').dblclick((e) => {
        const selected = window.getSelection().toString().trim();
        if (selected.length && selected.indexOf(' ') === -1) {
            fetch(tipUrl + selected)
            .then(res => res.text())
            .then(result => {
                result = result.trim().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
                showTip(selected, result, e.pageX, e.pageY);
            });
        }
    });
})(jQuery);