// ==UserScript== // @name Hi, Google Translate, don't translate here (*) // @name:zh-CN Hi, 谷歌翻译,不要翻译代码块 (*) // @namespace https://github.com/xianghongai/Tampermonkey-UserScript // @version 1.0.0 // @description Google Translate, don't translate code // @description:zh-CN 谷歌翻译不翻译代码块 // @author Nicholas Hsiang // @icon https://xinlu.ink/favicon.ico // @match http*://*/* // @exclude *://localhost:* // @exclude *://127.0.0.1:* // @exclude *://10.* // @exclude *://172.* // @exclude *://192.* // @grant GM_addStyle // @grant GM_addElement // @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; } .google-translate__no svg { width: 18px; height: 18px; } `; // DEPRECATED! // 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); GM_addStyle(style); } function createElement() { const icon = ` `; // DEPRECATED! // 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'); // ele.innerHTML = icon; // bodyContainer.appendChild(ele); GM_addElement(document.querySelector("body"), 'span', { title: `Hi, Google Translate, don't translate here`, class: 'google-translate__no', }); const ele = document.querySelector('.google-translate__no'); ele.innerHTML = icon; } // 有的站点不生效,需要手动触发 function manual() { const ele = document.querySelector('.google-translate__no'); ele.addEventListener('click', () => { notranslate(); }) } createStyleSheet(); createElement(); manual(); notranslate(); })();