Skip to content

Instantly share code, notes, and snippets.

@abel533
Last active March 7, 2022 03:34
Show Gist options
  • Save abel533/5839d3eca4686646baba113fc47e9b22 to your computer and use it in GitHub Desktop.
Save abel533/5839d3eca4686646baba113fc47e9b22 to your computer and use it in GitHub Desktop.
浏览器翻译时排除代码片段
// ==UserScript==
// @name notranslate
// @namespace https://gist.github.com/abel533/5839d3eca4686646baba113fc47e9b22
// @version 1.0
// @description 浏览器翻译时排除代码片段
// @author isea533
// @match *://**/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.addEventListener('load', function() {
noTranslate(document.getElementsByTagName('pre'));
noTranslate(document.getElementsByClassName('gist'));
noTranslate(document.getElementsByClassName('CodeMirror-code'));
}, false);
function noTranslate(items) {
if(items && items.length > 0) {
for(var i = 0; i < items.length; i++) {
items[i].classList.add('notranslate');
}
}
}
})();
@vace
Copy link

vace commented Dec 21, 2021

(function() {

    function noTranslate () {
        [
            ...document.getElementsByTagName('pre'),
            ...document.getElementsByClassName('gist'),
            ...document.getElementsByClassName('CodeMirror-code')
        ].forEach(item => {
            if (!item.classList.contains('notranslate')) {
                item.classList.add('notranslate')
            }
        })
    }
    function triggle () {
        requestAnimationFrame(noTranslate)
    }
    window.addEventListener('hashchange', noTranslate)
    window.addEventListener('load', noTranslate)
    window.addEventListener('resize', noTranslate)
    triggle()
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment