Inject Google Translate Widget

Userscript version of "Google Translate This" from https://github.com/andreicristianpetcu/google_translate_this for Tampermonkey or Violentmonkey. v0.5 2019-11-06

  1. // ==UserScript==
  2. // @name Inject Google Translate Widget
  3. // @description Userscript version of "Google Translate This" from https://github.com/andreicristianpetcu/google_translate_this for Tampermonkey or Violentmonkey. v0.5 2019-11-06
  4. // @author Jefferson "jscher2000" Scher
  5. // @namespace JeffersonScher
  6. // @version 0.5
  7. // @copyright Copyright 2019 Jefferson Scher. Mostly Copyright 2019 Andrei Cristian Petcu.
  8. // @license GPL-3.0
  9. // @match http*://*/*
  10. // @grant GM_registerMenuCommand
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // ==/UserScript==
  14.  
  15. var injectedStatus = false, hostarray = [];
  16.  
  17. function injectGoogleTranslateWidget(){
  18. if (window.self !== window.top) return; // Not in frames
  19. if (injectedStatus !== false) return; // Not if already injected
  20. // From: https://github.com/andreicristianpetcu/google_translate_this/blob/master/scripts/inject_google_translate_content.js as of Sept. 8, 2019 (last line omitted)
  21. var gtdiv = document.createElement("div");
  22. gtdiv.setAttribute("id", "google_translate_element");
  23. gtdiv.style.display="none";
  24. document.body.appendChild(gtdiv);
  25.  
  26. var googleTranslateElementInitCode = "function(){ \
  27. new google.translate.TranslateElement({pageLanguage: 'auto', autoDisplay: true}, 'google_translate_element'); \
  28. setTimeout(function(){ \
  29. var iframe = document.getElementsByClassName('goog-te-banner-frame')[0]; \
  30. var iframeDocument = iframe.contentDocument || iframe.contentWindow.document; \
  31. iframeDocument.getElementsByClassName('goog-te-button')[0].children[0].children[0].click(); \
  32. }, 1000); \
  33. }";
  34.  
  35. var globalFunctionScript = document.createElement('script');
  36. globalFunctionScript.text = "googleTranslateElementInit = " + googleTranslateElementInitCode;
  37. globalFunctionScript.type = "text/javascript";
  38. document.getElementsByTagName('head')[0].appendChild(globalFunctionScript);
  39.  
  40. var bg_script = document.createElement('script');
  41. bg_script.type = "text/javascript";
  42. bg_script.src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit";
  43. document.getElementsByTagName('head')[0].appendChild(bg_script);
  44. injectedStatus = true; // not detecting whether it failed due to CSP
  45. }
  46.  
  47. function addHost(){
  48. hostarray.push(location.hostname);
  49. GM_setValue("autoinjecthosts", JSON.stringify(hostarray));
  50. if (injectedStatus == false) injectGoogleTranslateWidget;
  51. }
  52.  
  53. function removeHost(){
  54. var index = hostarray.indexOf(location.hostname);
  55. if (index > -1){
  56. hostarray.splice(index, 1);
  57. GM_setValue("autoinjecthosts", JSON.stringify(hostarray));
  58. }
  59. }
  60.  
  61. // This should work in Violentmonkey and Tampermonkey, but unfortunately not Greasemonkey.
  62. try {
  63. hostarray = JSON.parse(GM_getValue("autoinjecthosts", "[]"));
  64. if (hostarray.includes(location.hostname)){ // auto-inject
  65. if (injectedStatus == false) window.setTimeout(injectGoogleTranslateWidget, 100);
  66. GM_registerMenuCommand("Stop Auto-Injecting Widget", removeHost);
  67. } else { // on-demand
  68. GM_registerMenuCommand("Inject Google Translate Widget", injectGoogleTranslateWidget);
  69. GM_registerMenuCommand("Auto-Inject on " + location.hostname, addHost);
  70. }
  71. } catch (err) {
  72. console.log('Error adding Inject Google Translate Widget menu items: ' + err.message);
  73. }