[油猴脚本👍] 划词翻译 谷歌翻译接口API(学英语摸鱼神器)

终身学习是每个人都应该贯彻到每一天的事情

尤其是在全球化的今天,更应该将上班学习贯彻到底


废话不多说了,直接上效果

运行效果(个人感觉很棒哦)


直接复制本脚本代码,打开油猴脚本管理界面,保存即可

// ==UserScript==
// @name         文本翻译脚本
// @namespace    https://example.com
// @version      1.0
// @description  鼠标左键选中文本后,自动请求翻译并显示结果
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // 创建气泡样式
    const tooltipStyle = `
        position: absolute;
        z-index: 9999;
        background-color: #333;
        color: #fff;
        padding: 5px;
        font-size: 14px;
        border-radius: 3px;
        max-width: 450px;
        text-align: left;
        white-space: normal;
        word-wrap: break-word;
        pointer-events: none;
    `;

    // 创建气泡元素
    function createTooltip() {
        const tooltip = document.createElement('div');
        tooltip.id = 'translation-tooltip';
        tooltip.style.cssText = tooltipStyle;
        return tooltip;
    }

    // 显示气泡
    function showTooltip(text, x, y) {
        let tooltip = document.getElementById('translation-tooltip');
        if (!tooltip) {
            tooltip = createTooltip();
            document.body.appendChild(tooltip);
        }
        tooltip.textContent = text;

        // 调整气泡位置
        const tooltipWidth = tooltip.offsetWidth;
        const tooltipHeight = tooltip.offsetHeight;
        const windowWidth = window.innerWidth;
        const windowHeight = window.innerHeight;
        const tooltipX = Math.max(0, Math.min(x - tooltipWidth / 2, windowWidth - tooltipWidth));
        const tooltipY = Math.max(0, y - tooltipHeight - 10);

        tooltip.style.left = tooltipX   'px';
        tooltip.style.top = tooltipY   'px';
    }

    // 隐藏气泡
    function hideTooltip() {
        const tooltip = document.getElementById('translation-tooltip');
        if (tooltip) {
            tooltip.remove();
        }
    }

    // 发送翻译请求
    function translateText(text) {
        const url = 'https://findmyip.net/api/translate.php?text='   encodeURIComponent(text);
        GM_xmlhttpRequest({
            method: 'GET',
            url: url,
            onload: function(response) {
                const Jresponse = JSON.parse(response.responseText);
                var translation = '内部接口错误,请联系开发者'
                if(Jresponse.code==400){
                    translation = Jresponse.error;
                }else{
                    translation = Jresponse.data.translate_result;
                }
                const selection = window.getSelection();
                if (selection.rangeCount > 0) {
                    const range = selection.getRangeAt(0);
                    const rect = range.getBoundingClientRect();
                    const x = rect.left   window.pageXOffset   rect.width / 2;
                    const y = rect.top   window.pageYOffset;
                    showTooltip(translation, x, y);
                }
            },
            onerror: function(error) {
                console.error('翻译请求发生错误:', error);
            }
        });
    }

    // 监听鼠标释放事件
    window.addEventListener('mouseup', function(event) {
        if (event.button === 0) { // 鼠标左键
            const selection = window.getSelection();
            const selectedText = selection.toString().trim();
            if (selectedText !== '') {
                translateText(selectedText);
            } else {
                hideTooltip();
            }
        }
    });

    // 初始化气泡元素
    window.addEventListener('DOMContentLoaded', function() {
        createTooltip();
    });
})();

以后的开发计划

因为油猴插件只能在浏览器内使用,所以之后准备整一个exe文件可以进行全局使用的,无论是在浏览器内还是脱离浏览器都可以使用

如果这篇文章对你有了帮助,请不要吝啬你手中免费的小鸡腿!

楼主的其他帖子推荐

[接口分享👍] 谷歌翻译接口API,中英互翻,自动识别多国语言,已稳定两年有余
[接口分享👍] 灰常准的IP归属地查询接口,你不来看看吗?
🖥️查询IP的网站-查IP/归属地 WebRTC/DNS侧漏查询网站

点赞
  1. tell说道:

    感谢分享

  2. aloha说道:

    要是单词能显示多个含义就好了,整句翻译倒无所谓

  3. abcxxx说道:

    能不能加个代理功能,有时候不想开魔法也可以使用

发表回复

电子邮件地址不会被公开。必填项已用 * 标注

×
订阅图标按钮