// ==UserScript== // @name Hi, Google Translate, don't translate here (*) // @name:zh-CN Hi, 谷歌翻译,不要翻译代码块 (*) // @namespace https://github.com/xianghongai/Tampermonkey-UserScript // @version 0.0.3 // @description Google Translate, don't translate code // @description:zh-CN 谷歌翻译不翻译代码块 // @author Nicholas Hsiang // @icon https://xinlu.ink/favicon.ico // @match http*://*/* // @grant none // @license MIT // @downloadURL none // ==/UserScript== (function () { 'use strict'; function notranslate() { const preEles = [ ...document.querySelectorAll('pre'), ...document.querySelectorAll('code'), ...document.querySelectorAll('.prism-code'), // https://formatjs.io ...document.querySelectorAll('a.type'), // https://nodejs.org ...document.querySelectorAll('.example-wrap'), // https://www.wolframalpha.com/ ...document.querySelectorAll('#handbook-content h2, .handbook-toc, #sidebar'), // https://www.typescriptlang.org/ ]; preEles.forEach((tiem) => { tiem.classList.add('notranslate'); tiem.setAttribute('translate', 'no'); }); } function createStyleSheet() { const style = `.google-translate__no { position: fixed; right: 10px; bottom: 10px; z-index: 999; cursor: pointer; margin: 0; padding: 0; width: 18px; height: 18px; line-height: 18px; text-align: center; }`; const headEle = document.head || document.getElementsByTagName('head')[0]; const styleEle = document.createElement('style'); styleEle.type = 'text/css'; if (styleEle.styleSheet) { styleEle.styleSheet.cssText = style; } else { styleEle.appendChild(document.createTextNode(style)); } headEle.appendChild(styleEle); return style; } function createElement() { const icon = ` `; const bodyContainer = document.querySelector("body"); const ele = document.createElement('span'); ele.setAttribute('title', `Hi, Google Translate, don't translate here`) ele.classList.add('google-translate__no'); // const text = document.createTextNode(`don't translate code`); // ele.appendChild(text); ele.innerHTML = icon; bodyContainer.appendChild(ele); } // 有的站点不生效,需要手动触发 function manual() { const ele = document.querySelector('.google-translate__no'); ele.addEventListener('click', () => { notranslate(); }) } // @grant GM_addStyle // GM_addStyle(createStyleSheet()); createStyleSheet(); createElement(); manual(); notranslate(); })();