Skip to content

Instantly share code, notes, and snippets.

@MarlonPassos-git
Last active March 9, 2023 14:40
Show Gist options
  • Select an option

  • Save MarlonPassos-git/bcb44f695a3d265c981974a25c906259 to your computer and use it in GitHub Desktop.

Select an option

Save MarlonPassos-git/bcb44f695a3d265c981974a25c906259 to your computer and use it in GitHub Desktop.
blocks translation of code blocks on the web

The following JavaScript code can be used to block automatic translation of blocks of code on a web page. This can be useful to prevent users from using machine translators to access sensitive information on a website.

To use this code, you can simply copy and paste the above code into your browser's console while on the webpage you want to block automatic translation.

If you want to use the script on all web pages automatically, you can use a browser extension like Tampermonkey. Here is an example of how to add the script to Tampermonkey:

  1. Install Tampermonkey in your browser.
  2. Click on the Tampermonkey icon in your browser's toolbar and select "Create New Script".
  3. Copy and paste the above code into Tampermonkey's script editor.
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match *://*/*
// @grant none
// ==/UserScript==
function main () {
const CSS_SELECTOR_CODE_BLOCK = "pre"
const maybeCodeBlocks = document.querySelectorAll(CSS_SELECTOR_CODE_BLOCK)
if (!maybeCodeBlocks) return;
maybeCodeBlocks.forEach(blockTranslationInElement)
function blockTranslationInElement(element) {
if (!element) return
// https://stackoverflow.com/questions/12238396/how-to-disable-google-translate-from-html-in-chrome
const CLASS_BLOCK_TRANSLATION = "notranslate"
element.classList.add(CLASS_BLOCK_TRANSLATION)
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment