阅读英文官方文档的时候,浏览器自带的翻译插件,经常是无差别翻译,阅读页代码块非常不友好。尝试通过网上的方法解决这个问题。
Tampermonkey
首先需要安装 Tampermonkey 扩展程序
官方下载
编辑框输入下面的脚本
// ==UserScript==
// @name 谷歌翻译绕过代码块
// @namespace http://tampermonkey.net/
// @version 0.4
// @description 让谷歌翻译插件翻译网页的时候,绕过代码块和一些无需翻译的元素
// @author xiandan
// @homeurl https://github.com/xiandanin/LardMonkeyScripts
// @match https://github.com/*
// @match https://npmjs.com/*
// @match https://stackoverflow.com/*
// @match https://*.google.com/*
// @match https://jmespath.org/*
// @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']if (window.location.hostname.indexOf("github") !== -1) {// 如果是github 还需要处理一些别的元素const githubSelector = ['#repository-container-header > div:nth-child(1)','summary.btn.css-truncate','.commit-author','.js-navigation-open.link-gray-dark','.Box-title','.BorderGrid-cell > div.mt-3 > a.Link--muted','.BorderGrid-cell > a[data-pjax="#repo-content-pjax-container"] > div > div:first-child','.BorderGrid-cell > ul.list-style-none','div[role="rowheader"]']bypassSelectorArray.push.apply(bypassSelectorArray, githubSelector)//如果还有github的插件 还需要延迟追加一些setTimeout(function () {const githubPluginSelector = ['.github-repo-size-div','.octotree-tree-view']noTranslate(githubPluginSelector)}, 3000)}noTranslate(bypassSelectorArray)
})()
脚本 match 内添加需要生效的网站,然后点击文件,保存。
刷新网页,即可体验。