Test Translator

AI Translation using Gemini

  1. // ==UserScript==
  2. // @name Test Translator
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description AI Translation using Gemini
  6. // @author You
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (async function() {
  12. 'use strict';
  13. let gemini;
  14. // Add styles
  15. const style = document.createElement('style');
  16. style.textContent = `
  17. #translate-prompt {
  18. position: fixed;
  19. top: 20px;
  20. right: 20px;
  21. background: white;
  22. padding: 10px;
  23. border-radius: 5px;
  24. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  25. z-index: 10000;
  26. display: none;
  27. }
  28. button {
  29. margin: 5px;
  30. padding: 5px 10px;
  31. cursor: pointer;
  32. }
  33. `;
  34. document.head.appendChild(style);
  35.  
  36. // Add prompt HTML
  37. const promptDiv = document.createElement('div');
  38. promptDiv.id = 'translate-prompt';
  39. promptDiv.innerHTML = `
  40. Translate?
  41.  
  42.  
  43. Yes
  44. No
  45. `;
  46. document.body.appendChild(promptDiv);
  47.  
  48. // Load Gemini API dynamically
  49. try {
  50. const module = await import('https://api.websim.ai/blobs/0194e43b-7e45-75e7-ad7e-5848f3b43828.js');
  51. gemini = module.gemini;
  52. // Function to detect non-English text
  53. function hasNonEnglishText(text) {
  54. const nonEnglishPattern = /[^\x00-\x7F]+/;
  55. return nonEnglishPattern.test(text);
  56. }
  57.  
  58. // Function to translate text using Gemini
  59. async function translateText(text) {
  60. try {
  61. const prompt = `Translate this text to English: ${text}`;
  62. const response = await gemini.generate(prompt);
  63. return response;
  64. } catch (error) {
  65. console.error('Translation error:', error);
  66. return text;
  67. }
  68. }
  69.  
  70. // Function to replace text in the DOM
  71. function replaceText(element, translatedText) {
  72. element.textContent = translatedText;
  73. }
  74.  
  75. // Main function to check page content
  76. function checkPageContent() {
  77. const textNodes = document.evaluate(
  78. '//text()',
  79. document,
  80. null,
  81. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  82. null
  83. );
  84.  
  85. let hasNonEnglish = false;
  86. for (let i = 0; i < textNodes.snapshotLength; i++) {
  87. const node = textNodes.snapshotItem(i);
  88. if (hasNonEnglishText(node.textContent)) {
  89. hasNonEnglish = true;
  90. break;
  91. }
  92. }
  93.  
  94. if (hasNonEnglish) {
  95. showTranslatePrompt();
  96. }
  97. }
  98.  
  99. function showTranslatePrompt() {
  100. const prompt = document.getElementById('translate-prompt');
  101. prompt.style.display = 'block';
  102. document.getElementById('yes').addEventListener('click', async () => {
  103. const textNodes = document.evaluate(
  104. '//text()',
  105. document,
  106. null,
  107. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  108. null
  109. );
  110. for (let i = 0; i < textNodes.snapshotLength; i++) {
  111. const node = textNodes.snapshotItem(i);
  112. if (hasNonEnglishText(node.textContent)) {
  113. const translatedText = await translateText(node.textContent);
  114. replaceText(node, translatedText);
  115. }
  116. }
  117. prompt.style.display = 'none';
  118. });
  119. document.getElementById('no').addEventListener('click', () => {
  120. prompt.style.display = 'none';
  121. });
  122. }
  123. // Run the check
  124. checkPageContent();
  125. } catch (err) {
  126. console.error('Failed to load Gemini:', err);
  127. }
  128. })();