From 2df176122b11c2717efbc02953249c027d38aa2f Mon Sep 17 00:00:00 2001 From: lopezloo Date: Wed, 12 Apr 2017 21:07:30 +0200 Subject: [PATCH] Auto reading; spacebar pauses/starts queue now --- manifest.json | 2 +- script.js | 114 +++++++++++++++++++++++++++----------------------- 2 files changed, 62 insertions(+), 54 deletions(-) diff --git a/manifest.json b/manifest.json index 13dadba..8b8fd81 100644 --- a/manifest.json +++ b/manifest.json @@ -3,7 +3,7 @@ "name": "YouTube live chat text2speech", "description": "This extension reads every message on YouTube live chat.", - "version": "0.7.0.3", + "version": "0.7.1.0", "author": "lopezloo", "homepage_url": "https://github.com/lopezloo/yt-live-text2speech", diff --git a/script.js b/script.js index 4249430..bac1099 100644 --- a/script.js +++ b/script.js @@ -1,15 +1,9 @@ -/* -TODO: -auto mode -*/ - var options = { voiceType: '', voice: null, emojisEnabled: true, voiceRate: 1.0, voicePitch: 1.0, - // automode } function loadOptions() { @@ -29,6 +23,23 @@ function loadOptions() { } loadOptions(); +var voices = []; +window.speechSynthesis.onvoiceschanged = function() { + voices = speechSynthesis.getVoices(); + console.log('Loaded ' + voices.length + ' voices.'); + updateVoice(); +}; + +function updateVoice() { + for(i = 0; i < voices.length; i++) { + if(voices[i].lang == options.voiceType) { + options.voice = voices[i]; + console.log('Using voice: ' + voices[i].name + ' (' + voices[i].lang + ')' + ' (localService: ' + voices[i].localService + ')') + break; + } + } +} + chrome.storage.onChanged.addListener(function(changes, areaName) { if(changes.voiceType) { options.voiceType = changes.voiceType.newValue; @@ -50,48 +61,62 @@ class ChatWatcher { constructor() { this.queue = {}; this.currentMsg = null; + this.paused = false; } onSpeechEnd() { - delete watcher.queue[watcher.currentMsg]; - watcher.currentMsg = null; - // if this.auto - //watcher.updateSpeech(); + delete this.queue[this.currentMsg]; + this.currentMsg = null; + this.updateSpeech(); } - updateSpeech() { - if(this.currentMsg !== null) { - // Skip current - this.removeMsg(this.currentMsg); - } + switchPause() { + this.paused = !this.paused; + this.updateSpeech(); + } - if(voices.length == 0) { - console.log('ERROR: No voices loaded.') - return; - } + updateSpeech() { + if(!this.paused && this.currentMsg === null) { + if(voices.length == 0) { + console.log('ERROR: No voices loaded.') + return; + } - if(Object.keys(this.queue).length > 0) { - let id = Object.keys(this.queue)[0]; - this.currentMsg = id; - let msg = this.queue[id]; - let msgt = msg[0] + ': ' + msg[1]; - console.log(msgt + ' (' + Object.keys(this.queue).length + ' in queue)'); - - speechSynthesis.cancel(); - let u = new SpeechSynthesisUtterance(msgt); - u.onend = this.onSpeechEnd; - u.voice = options.voice; - u.rate = options.voiceRate; - u.pitch = options.voicePitch; - speechSynthesis.speak(u); + if(Object.keys(this.queue).length > 0) { + let id = Object.keys(this.queue)[0]; + this.currentMsg = id; + let msg = this.queue[id]; + let msgt = msg[0] + ': ' + msg[1]; + console.log(msgt + ' (' + Object.keys(this.queue).length + ' in queue)'); + + let u = new SpeechSynthesisUtterance(msgt); + + // Don't trust it. It's buggy. + //u.onend = this.onSpeechEnd; + + u.voice = options.voice; + u.rate = options.voiceRate; + u.pitch = options.voicePitch; + speechSynthesis.speak(u); + + // Thanks to: https://gist.github.com/mapio/967b6a65b50d39c2ae4f + let _this = this; + function _wait() { + if(!speechSynthesis.speaking) { + _this.onSpeechEnd(); + return; + } + window.setTimeout(_wait, 200); + } + _wait(); + } } } addToQueue(id, author, msg) { //console.log('addToQueue ' + id); this.queue[id] = [author, msg]; - // if this.auto - //this.updateSpeech(); + this.updateSpeech(); } updateMsgID(id, newId) { @@ -120,23 +145,6 @@ class ChatWatcher { } var watcher = new ChatWatcher(); -var voices = []; -window.speechSynthesis.onvoiceschanged = function() { - voices = speechSynthesis.getVoices(); - console.log('Loaded ' + voices.length + ' voices.'); - updateVoice(); -}; - -function updateVoice() { - for(i = 0; i < voices.length; i++) { - if(voices[i].lang == options.voiceType) { - options.voice = voices[i]; - console.log('Using voice: ' + voices[i].name + ' (' + voices[i].lang + ')' + ' (localService: ' + voices[i].localService + ')') - break; - } - } -} - $(document).ready(function() { console.log('yt-live-text2speech ready!'); @@ -205,7 +213,7 @@ $(document).ready(function() { keypressed = true; let focused = $('yt-live-chat-text-input-field-renderer').attr('focused') == ''; if(!focused) { - watcher.updateSpeech(); + watcher.switchPause(); e.preventDefault(); } }