// ==UserScript== // @name Hi, Google Translate, don't translate here (*) // @name:zh-CN Hi, 谷歌翻译,不要翻译代码块 (*) // @namespace https://github.com/xianghongai/Tampermonkey-UserScript // @version 0.0.2 // @description Google Translate, don't translate code // @description:zh-CN 谷歌翻译不翻译代码块 // @author Nicholas Hsiang // @icon https://xinlu.ink/favicon.ico // @match http*://*/* // @grant none // @downloadURL none // ==/UserScript== (function () { 'use strict'; function handleEvent() { const preEles = [ ...document.querySelectorAll('pre'), ...document.querySelectorAll('code'), ...document.querySelectorAll('.prism-code'), // https://formatjs.io ...document.querySelectorAll('a.type'), // https://nodejs.org ]; if (Array.isArray(preEles)) { 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); } 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 addEvent() { const ele = document.querySelector('.google-translate__no'); ele.addEventListener('click', () => { handleEvent(); }) } createStyleSheet(); createElement(); addEvent(); handleEvent(); })();