TTS jpdb

TTS for sentence list, hide English translations

  1. // ==UserScript==
  2. // @name TTS jpdb
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.11
  5. // @description TTS for sentence list, hide English translations
  6. // @author chaiknees
  7. // @match https://jpdb.io/review*
  8. // @icon https://www.google.com/s2/favicons?domain=jpdb.io
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. let voice;
  16. let lines = [];
  17. function speakIn( nv ) {
  18. voice = nv;
  19. }
  20. function read( text ) {
  21. let voiceline = new SpeechSynthesisUtterance( text );
  22. voiceline.rate = 0.8;
  23. voiceline.voice = voice;
  24. voiceline.onend = function( event ) {
  25. console.log( `Had fun in ${event.elapsedTime} seconds.` );
  26. if ( lines.length > 0 ) {
  27. read( lines.shift() );
  28. };
  29. };
  30. window.speechSynthesis.speak( voiceline );
  31. }
  32. function takeSententences() {
  33. for (let elem of Array.from(document.querySelectorAll('.answer-box .sentence, .used-in .jp')).slice(0, 2)) {
  34. let words = [];
  35. for (let node of Array.from(elem.childNodes)) {
  36. if (node.nodeType === Node.TEXT_NODE) {
  37. words.push( node.textContent );
  38. } else if ( node.tagName === 'RUBY' ) {
  39. words.push( node.childNodes[ 0 ].textContent );
  40. } else if ( node.tagName === 'SPAN' ) {
  41. words.push( node.textContent );
  42. }
  43. }
  44. lines.push( words.join( '' ) );
  45. }
  46. }
  47. function hideTranslation() {
  48. document.querySelector('#show-checkbox-examples').checked = true;
  49. for (let elem of Array.from(document.querySelectorAll('.used-in .en, .sentence-translation'))) {
  50. elem.style.color = '#222';
  51. elem.addEventListener('mouseover', function(event) {
  52. event.target.style.color = 'darkgray';
  53. });
  54. elem.addEventListener('mouseout', function(event) {
  55. event.target.style.color = '#222';
  56. });
  57. }
  58. }
  59. hideTranslation();
  60. let voiceReady = false;
  61. window.speechSynthesis.onvoiceschanged = function() {
  62. let voices = window.speechSynthesis.getVoices().filter( function( voice ) {
  63. return voice.lang == 'ja-JP';
  64. } );
  65. speakIn( voices[ 0 ] );
  66. voiceReady = true;
  67. };
  68. let word = '';
  69. setInterval(function() {
  70. let nv = document.querySelector('.answer-box a.plain').textContent;
  71. if ( nv !== word && voiceReady ) {
  72. word = nv;
  73. takeSententences();
  74. setTimeout(() => read( lines.shift() ), 750);
  75. }
  76. }, 250);
  77. })();