Last active
November 20, 2024 01:11
-
-
Save 0x1306a94/43976bd23e912e4dc2e073d4ba798bd0 to your computer and use it in GitHub Desktop.
让谷歌翻译插件翻译网页的时候,绕过代码块和一些无需翻译的元素
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name 谷歌翻译绕过代码块(适配github,mathworks等) | |
// @version 0.3 | |
// @description 让谷歌翻译插件翻译网页的时候,绕过代码块和一些无需翻译的元素 | |
// @match http*://*.*/* | |
// @match http://localhost:*/* | |
// @match http://127.0.0.1:*/* | |
// @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', | |
'code', | |
'.prism-code', | |
'.codeinput', | |
'.CodeMirror-sizer', | |
'.CodeMirror-lines', | |
'.CodeMirror-scroll', | |
'.CodeMirror-line', | |
'.enlighter' | |
] | |
// 延迟加载 | |
const delaySelectorArray = [] | |
if (window.location.hostname.indexOf("mathjax") !== -1) { | |
const selectors = [ | |
'.math', | |
'.MathJax', | |
'.MathJax_Display', | |
'.MathRow', | |
'.MathEquation', | |
'.CodeBlock' | |
] | |
delaySelectorArray.push.apply(delaySelectorArray, selectors) | |
} | |
if (window.location.hostname.indexOf("github") !== -1) { | |
// 如果是github 还需要处理一些别的元素 | |
const githubSelector = [ | |
'.bg-gray-light.pt-3.hide-full-screen.mb-5', | |
'summary.btn.css-truncate', | |
'.commit-author', | |
'.js-navigation-open.link-gray-dark', | |
'.Box-title', | |
'.d-flex', | |
'.file-navigation.mb-3.d-flex', | |
'.pt-3', | |
'.Box-row', | |
'.Box-header', | |
'.BorderGrid-cell > div.mt-3 > a.muted-link', | |
'.BorderGrid-cell > ul.list-style-none', | |
'header.AppHeader', | |
'#repos-file-tree', | |
'div.Box-sc-g0xbh4-0.eIgvIk', | |
'div.Box-sc-g0xbh4-0.jQCQnS', | |
'div.Box-sc-g0xbh4-0.ePiodO', | |
'div.Box-sc-g0xbh4-0.eLcVee', | |
// 主页文件列表 | |
'tr.Box-sc-g0xbh4-0.jEbBOT', | |
// README 头部 | |
'nav.Box-sc-g0xbh4-0.dvTdPK', | |
] | |
bypassSelectorArray.push.apply(bypassSelectorArray, githubSelector) | |
//如果还有github的插件 还需要延迟追加一些 | |
const githubPluginSelector = [ | |
'.github-repo-size-div', | |
'.octotree-tree-view', | |
'tr.react-directory-row', | |
] | |
delaySelectorArray.push.apply(delaySelectorArray, githubPluginSelector) | |
} | |
if (window.location.hostname.indexOf("mathworks") !== -1) { | |
// 如果是mathworks | |
const selectors = [ | |
'.codeinput', | |
'.code_responsive', | |
'.inlineequation', | |
'inline' | |
] | |
bypassSelectorArray.push.apply(bypassSelectorArray, selectors) | |
} | |
if (window.location.hostname.indexOf("threejsfundamentals") !== -1) { | |
const selectors = [ | |
'.threejs_example_container', | |
] | |
bypassSelectorArray.push.apply(bypassSelectorArray, selectors) | |
} | |
if (window.location.hostname.indexOf("kodeco.com") !== -1) { | |
const selectors = [ | |
'pre.language-swift', | |
] | |
bypassSelectorArray.push.apply(bypassSelectorArray, selectors) | |
} | |
if (window.location.hostname.indexOf("questdb.io") !== -1) { | |
const selectors = [ | |
'.prism-code', | |
'.codeBlockContainer_J+bg', | |
] | |
delaySelectorArray.push.apply(delaySelectorArray, selectors) | |
} | |
if (window.location.hostname.indexOf("cursor.com") !== -1) { | |
const selectors = [ | |
'div.nice-code-block-container', | |
] | |
delaySelectorArray.push.apply(delaySelectorArray, selectors) | |
} | |
noTranslate(bypassSelectorArray) | |
setTimeout(function () { | |
noTranslate(delaySelectorArray) | |
}, 2000) | |
})() |
@Guaiii 可以继续添加自己的需求
我用的 暴力猴 2.25.0
补充下,当前的文件头不符合规范
以下是修改过后的
// ==UserScript==
// @name 谷歌翻译绕过代码块(适配github,mathworks等)
// @version 0.3
// @description 让谷歌翻译插件翻译网页的时候,绕过代码块和一些无需翻译的元素
// @match http*://*.*/*
// @match http://localhost:*/*
// @match http://127.0.0.1:*/*
// @match http://[::]:*/*
// @license MIT
// @grant none
// ==/UserScript==
/*jshint esversion: 6 */
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍,本来想尝试着写一个,没想到搜索到了,体验之后觉得很棒。我认为一些带有 a 标签属性的也是可以跳过翻译的,一些文档,文档的 api 手册经常会用到 a 标签来跳转对应地址,当然我是这么干的。