SpanishDict Quick Translatoin

When double click a word, show it's meanings

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);