暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

谷歌翻译技术文章绕过代码块教程

原创 小小亮 2022-08-25
1541

阅读英文技术文章的时候,谷歌浏览器自带的翻译插件,经常连代码也会一起翻译,非常不友好。可通过安装Tampermonkey(油猴)插件及跳过代码块脚本来解决,翻译时不翻译代码块。


  1. 安装Tampermonkey(油猴)插件

Tampermonkey(油猴)

首先需要安装 Tampermonkey 扩展程序

油猴插件官方下载:https://www.tampermonkey.net/

点击“下载”会跳到到谷歌拓展程序 - 从谷歌商店安装


安装好后,点击谷歌浏览器 【拓展程序】图标,“固定”油猴插件


  1. 油猴插件安装跳过代码块脚本

(1)点击油猴插件图标 - “添加新脚本”

(2)复制黏贴以下脚本到 编辑框



// ==UserScript==
// @name         谷歌翻译绕过代码块(适配githubundefinedmathworks等)
// @description  让谷歌翻译插件翻译网页的时候,绕过代码块和一些无需翻译的元素
// @match        https://github.com/*
// @match        https://npmjs.com/*
// @match        https://stackoverflow.com/*
// @match        https://*.google.com/*
// @match        https://*.mathworks.*/*
// @match        http*://*/*
// @license      MIT
// @grant        none
// ==/UserScript==
/*jshint esversion: 6 */
(function () {
    'use strict'

    function noTranslate (array) {
        array.forEach((name) => {
            [...document.querySelectorAll(name)].forEach(node => {
                if (node.className.indexOf('notranslate') === -1) {
                    node.classList.add('notranslate')
                }
            })
        })
    }

    const bypassSelectorArray = [
        'pre'undefined
        'code'undefined
        '.prism-code'undefined
        '.codeinput'undefined
    ]
    if (window.location.hostname.indexOf("github") !== -1) {
        // 如果是github 还需要处理一些别的元素
        const githubSelector = [
            '.bg-gray-light.pt-3.hide-full-screen.mb-5'undefined
            'summary.btn.css-truncate'undefined
            '.commit-author'undefined
            '.js-navigation-open.link-gray-dark'undefined
            '.Box-title'undefined
            '.BorderGrid-cell > div.mt-3 > a.muted-link'undefined
            '.BorderGrid-cell > ul.list-style-none'
        ]
        bypassSelectorArray.push.apply(bypassSelectorArrayundefined githubSelector)

        //如果还有github的插件 还需要延迟追加一些
        setTimeout(function () {
            const githubPluginSelector = [
                '.github-repo-size-div'undefined
                '.octotree-tree-view'
            ]
            noTranslate(githubPluginSelector)
        }undefined 3000)
    }
    if (window.location.hostname.indexOf("mathworks") !== -1) {
        // 如果是mathworks 
        const mathworksSelector = [
            '.codeinput'undefined
            '.code_responsive'undefined
            '.MathRow.HBox'undefined
          '.MathEquation'undefined
          '.inlineequation'undefined
          'inline'
        ]
        bypassSelectorArray.push.apply(bypassSelectorArrayundefined mathworksSelector)
    }
    noTranslate(bypassSelectorArray)
})()


(3)脚本 match 内添加需要生效的网站,然后点击文件,保存。

(第9行代码处,输入【网页地址*】)


刷新网页,即可体验。


翻译技术文章到墨天轮发表:

  1. 打开需要翻译的网页,复制黏贴到墨天轮文章编辑框

直接在文章处复制,黏贴到墨天轮富文本模式下,此时代码块未被翻译

(更方便去除不需要的内容)


也可以在翻译的网页,右键“检查” - 在【元素】区 选择所需部分内容的文章代码


文章代码 黏贴到墨天轮Markdown模式下,此时 代码块 更好的展示出来了。

(注意:需要剔除不需要的广告内容)

最后修改时间:2022-08-26 10:11:58
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论