Skip to content

Instantly share code, notes, and snippets.

@zero-plusplus
Last active November 6, 2021 09:51
Show Gist options
  • Save zero-plusplus/c4bd1eab92db20484acf8456fb41334b to your computer and use it in GitHub Desktop.
Save zero-plusplus/c4bd1eab92db20484acf8456fb41334b to your computer and use it in GitHub Desktop.
For Tampermonkey
// ==UserScript==
// @name fixpagetranslate
// @namespace
// @author zero-plusplus
// @include https://*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
// ==/UserScript==
(function() {
let initedProcessing = false;
setTimeout(() => {
fixGoogleTranslate()
initedProcessing = true;
}, 500);
let currentUrl = document.location.href;
const observer = new MutationObserver(function () {
if (initedProcessing && currentUrl !== document.location.href) {
fixGoogleTranslate()
currentUrl = document.location.href;
}
})
observer.observe(document, { childList: true, subtree: true })
function fixGoogleTranslate() {
$('p, li, td:has(> a)').each((i, element) => {
if ($(element).children().length === 1) {
return
}
$(element).children('a').each((i, child) => {
$(child).css('display', 'inline');
})
});
$('p, li, td:has(> code)').each((i, element) => {
if (0 < $(element).has('code').length) {
element.innerHTML = element.innerHTML.replace(/<code[^>]*>|<\/code>/gi, '"');
}
});
}
}());
// ==UserScript==
// @name notranslate
// @namespace
// @author zero-plusplus
// @include https://*
// ==/UserScript==
(function() {
execNotranslate()
function execNotranslate() {
try {
const codeHighlightClasses = [
'origin', // AutoHotkey code sample
'highlight', // [pygments] https://github.com/dagwieers/pygments
'prettyprint', // [code-prettify] https://github.com/google/code-prettify
'CodeMirror', // [CodeMirror] https://github.com/codemirror/CodeMirror
'prism', // [prism] https://github.com/PrismJS/prism
'prism-code',
'hljs', // [highlight.js] https://github.com/highlightjs/highlight.js/
];
// The syntax highlighting library is excluded from the translation.
codeHighlightClasses.forEach(className => {
const nodes = document.querySelectorAll(`.${className}`);
for (const element of nodes) {
element.classList.add('notranslate');
}
});
}
catch (e) {
alert(e.message);
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment