Skip to content

Instantly share code, notes, and snippets.

@noelleleigh
Last active September 27, 2019 22:42
Show Gist options
  • Save noelleleigh/c39dc63fa323694e0c0c753840c71238 to your computer and use it in GitHub Desktop.
Save noelleleigh/c39dc63fa323694e0c0c753840c71238 to your computer and use it in GitHub Desktop.
Bookmarklet to translate a webpage into English
javascript: (function () {
const remoteScript = document.createElement('script');
remoteScript.src = 'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
remoteScript.type = 'text/javascript';
document.body.appendChild(remoteScript);
const containerElem = document.createElement('div');
containerElem.id = 'google_translate_element';
document.body.insertBefore(containerElem, document.body.firstChild);
const localScript = document.createElement('script');
localScript.type = 'text/javascript';
localScript.text = `function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: '',
layout: google.translate.TranslateElement.FloatPosition.TOP_LEFT
}, 'google_translate_element');
}`;
document.body.appendChild(localScript);
})()
@noelleleigh
Copy link
Author

If you're using uBlock Origin's medium mode, make sure to set these rules to allow the bookmarklet to load the necessary Javascript:

* https://translate.google.com script noop
* translate.googleapis.com * noop

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