Duolingo Hebrew Text-to-Speech

Automatically reads Hebrew text aloud in Duolingo's Hebrew course with a slower TTS rate and an optional button for manual playback, ensuring only one automatic playback per text change.

  1. // ==UserScript==
  2. // @name Duolingo Hebrew Text-to-Speech
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.3
  5. // @description Automatically reads Hebrew text aloud in Duolingo's Hebrew course with a slower TTS rate and an optional button for manual playback, ensuring only one automatic playback per text change.
  6. // @author justsomelearner
  7. // @match https://www.duolingo.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. console.log("Duolingo Hebrew Text-to-Speech script loaded");
  15.  
  16. // Check if the browser supports the Web Speech API for TTS
  17. if (!('speechSynthesis' in window)) {
  18. alert("Your browser does not support the Web Speech API for text-to-speech");
  19. return;
  20. }
  21.  
  22. let lastReadText = ''; // Variable to store the last read text
  23.  
  24. // Function to read the Hebrew text aloud
  25. function readText(text) {
  26. const utterance = new SpeechSynthesisUtterance(text);
  27. utterance.lang = 'he-IL'; // Set the language to Hebrew
  28. utterance.rate = 0.8; // Set the rate to be slower than the default
  29. speechSynthesis.speak(utterance);
  30. console.log("Reading text: ", text);
  31. }
  32.  
  33. // Function to add a TTS button next to Hebrew text
  34. function addTTSButton() {
  35. console.log("Attempting to add TTS button");
  36.  
  37. // Select the Hebrew text element
  38. const hebrewTextElement = document.querySelector('#root > div.kPqwA._2kkzG > div._3yE3H > div > div.RMEuZ._1GVfY > div > div > div > div > div._34aEz._1IiFg.f7WE2._3rat3 > div.aMPis._35mGI > span > span > span');
  39. console.log("Hebrew text element: ", hebrewTextElement);
  40.  
  41. if (hebrewTextElement && !document.getElementById('ttsButton')) {
  42. const ttsButton = document.createElement('button');
  43. ttsButton.id = 'ttsButton';
  44. ttsButton.innerText = '🔊';
  45. ttsButton.style = 'background-color: #1cb0f6; color: white; border: none; padding: 10px; margin-left: 10px; cursor: pointer; border-radius: 50%; width: 40px; height: 40px;';
  46. ttsButton.onclick = () => readText(hebrewTextElement.textContent);
  47. hebrewTextElement.parentElement.appendChild(ttsButton);
  48. console.log("TTS button added");
  49. } else {
  50. console.log("Hebrew text element not found or TTS button already exists");
  51. }
  52. }
  53.  
  54. // Function to find and read the Hebrew text automatically
  55. function readHebrewText() {
  56. console.log("Attempting to read Hebrew text");
  57.  
  58. // Select the Hebrew text element
  59. const hebrewTextElement = document.querySelector('#root > div.kPqwA._2kkzG > div._3yE3H > div > div.RMEuZ._1GVfY > div > div > div > div > div._34aEz._1IiFg.f7WE2._3rat3 > div.aMPis._35mGI > span > span > span');
  60. console.log("Hebrew text element: ", hebrewTextElement);
  61.  
  62. if (hebrewTextElement) {
  63. const text = hebrewTextElement.textContent;
  64. if (text !== lastReadText) {
  65. readText(text);
  66. lastReadText = text; // Update the last read text
  67. } else {
  68. console.log("Text has already been read");
  69. }
  70. } else {
  71. console.log("Hebrew text element not found");
  72. }
  73. }
  74.  
  75. // Observe changes in the Duolingo interface to read the text when it changes
  76. const observer = new MutationObserver((mutations) => {
  77. mutations.forEach((mutation) => {
  78. if (mutation.addedNodes.length || mutation.removedNodes.length) {
  79. console.log("Mutation observed");
  80. readHebrewText();
  81. addTTSButton();
  82. }
  83. });
  84. });
  85.  
  86. observer.observe(document.body, { childList: true, subtree: true });
  87.  
  88. // Initial call to read the text if it's already there and add the button
  89. window.addEventListener('load', () => {
  90. console.log("Page loaded");
  91. readHebrewText();
  92. addTTSButton();
  93. });
  94.  
  95. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址