吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3485|回复: 102
收起左侧

[其他原创] 油猴脚本实现谷歌翻译接口划词翻译【接口来着论坛其他大佬】

  [复制链接]
似水流年小小 发表于 2023-12-8 07:48
本帖最后由 似水流年小小 于 2024-1-19 11:04 编辑

免费的小红心点起来本脚本集成了【zhurui】大佬提供的谷歌翻译接口,感谢大佬。如大佬接口不能使用,那本脚本也随之失效。
Tips: 接口大佬很坚挺,仍然稳如狗
image.png 附大佬链接:https://www.52pojie.cn/thread-1863856-1-1.html

直接上代码:
[JavaScript] 纯文本查看 复制代码
// ==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();
    });
})();



PS:  代码粘过来被强行变化了,我把代码打包上来。请下载附件

使用方法:直接复制本脚本代码,打开油猴脚本管理界面,点击新建脚本,把复制的代码粘贴进去,保存即可。
实际截图如下(代码开头被改变了,参照图片上改一下,或者直接下载附件):


image.png
image.png

划词翻译.txt

3.67 KB, 下载次数: 197, 下载积分: 吾爱币 -1 CB

免费评分

参与人数 18吾爱币 +21 热心值 +15 收起 理由
Harmon666 + 1 我很赞同!
twbly + 2 + 1 谢谢@Thanks!
sjminh + 1 + 1 谢谢@Thanks!
wind315_ + 1 + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
FZyuLI428 + 1 热心回复!
虫鱼疏rc + 1 + 1 好使
lingniao + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
旋风小志 + 1 我很赞同!
helh0275 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
MakiseSatsuki + 1 热心回复!
mjst2311 + 1 + 1 我很赞同!
hsx778899 + 1 + 1 我很赞同!
C至简 + 1 谢谢@Thanks!
wudaoxiyang + 1 + 1 我很赞同!
pod2023 + 1 谢谢@Thanks!
ccchhhh87665 + 1 我很赞同!
wangdanq + 1 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hsx778899 发表于 2023-12-8 09:07
本帖最后由 hsx778899 于 2023-12-8 10:01 编辑
破解专用户 发表于 2023-12-8 08:40
我是不是应该回复,大佬挺住,您一定要坚挺啊!!!
--------------------------
同时我又自私的回复楼主 ...

你的建议不错,我试着改了下
[Asm] 纯文本查看 复制代码
// ==UserScript==
// @name         文本翻译脚本
// @namespace    https://example.com
// @version      1.0
// @description  鼠标左键选中文本后,自动请求翻译并显示结果
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // Function to decode HTML entities
    function decodeHtml(html) {
        var txt = document.createElement("textarea");
        txt.innerHTML = html;
        return txt.value;
    }

    // 创建气泡样式
    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: auto;
    `;

    const buttonStyle = `
        background-color: #555;
        color: #fff;
        border: none;
        padding: 5px 10px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 12px;
        margin: 5px 2px;
        cursor: pointer;
        border-radius: 2px;
    `;

    // 创建气泡元素
    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.innerHTML = text;

        // Add copy button
        const copyButton = document.createElement('button');
        copyButton.textContent = '复制';
        copyButton.style.cssText = buttonStyle;
        copyButton.onclick = function() {
            // Decode HTML entities before copying
            const decodedText = decodeHtml(text);
            navigator.clipboard.writeText(decodedText).then(function() {
                // Show success message
                copyButton.textContent = '复制成功';
                setTimeout(function() {
                    copyButton.textContent = '复制';
                }, 2000);
            }, function(err) {
                console.error('复制失败:', err);
            });
        };
        tooltip.appendChild(copyButton);

        // Adjust tooltip position
        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();
    });
})();
Harmon666 发表于 2024-3-8 14:31
吾爱水货 发表于 2024-3-4 15:24
twbly 发表于 2024-3-2 20:47
可用 非常感谢 谷歌翻译不好使就一直没找到好用的
宝宝很腻害 发表于 2024-3-1 21:23
拿了..........
chenCC520 发表于 2024-3-1 14:48
测试了一下,好用,大佬
街角盒饭 发表于 2024-2-29 17:37
很强啊,收下了!!!!!
qiaoshang 发表于 2024-3-3 15:23

今天试了下,又可以用了,神奇!之前忘记截图了
wind315_ 发表于 2024-1-24 14:21

大佬威武
zhangmi008 发表于 2024-1-19 16:09
给大佬点赞
wangdanq 发表于 2023-12-8 08:01
谢谢楼主分享  试试
marco527 发表于 2023-12-8 08:02
第二位一会儿研究研究。
xiaosd 发表于 2023-12-8 08:06
试试翻译效果~~
ccchhhh87665 发表于 2023-12-8 08:07
不错不错,挺精简的
chuanyuehuoxian 发表于 2023-12-8 08:13
感谢楼主分享谢谢
xiaozhuzi 发表于 2023-12-8 08:14
沙发坐下
ssyszbd 发表于 2023-12-8 08:15
棒!棒!棒!棒!棒!
pod2023 发表于 2023-12-8 08:19
感谢分享
darifo 发表于 2023-12-8 08:22
不错不错!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-20 18:37

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表