Skip to content

Commit

Permalink
Add Firefox support
Browse files Browse the repository at this point in the history
  • Loading branch information
lopezloo committed Jun 11, 2017
1 parent 938e7a8 commit 5546d4e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
21 changes: 19 additions & 2 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
function fixStorage() {
if(navigator.userAgent.search('Chrome') == -1) {
// Seems like storage.sync doesn't work on Firefox.
chrome.storage.sync = chrome.storage.local;
console.log('WARNING: Using local storage.');
}
}
fixStorage();

document.addEventListener('DOMContentLoaded', function() {
let voiceSelect = document.getElementById('voice');
let emojisCheck = document.getElementById('emojis');
Expand Down Expand Up @@ -40,7 +49,7 @@ document.addEventListener('DOMContentLoaded', function() {
}, function(items) {
console.log('Options loaded! Voice: ' + items.voiceType + '; emojis: ' + items.emojisEnabled + '; rate: ' + items.voiceRate + '; pitch: ' + items.voicePitch + '; volume: ' + items.voiceVolume);

voiceSelect.value = voiceSelect.options[0];
voiceSelect.value = voiceSelect.options[0].value;
for(let i=0; i < voiceSelect.options.length; i++) {
if(voiceSelect.options[i].value == items.voiceType || voiceSelect.options[i].getAttribute('data-lang') == items.voiceType) {
voiceSelect.value = voiceSelect.options[i].value;
Expand Down Expand Up @@ -69,7 +78,7 @@ document.addEventListener('DOMContentLoaded', function() {
volumeInput.addEventListener('change', saveChanges);
delayInput.addEventListener('change', saveChanges);

window.speechSynthesis.onvoiceschanged = function() {
function loadVoices() {
let voices = speechSynthesis.getVoices();
for(i = 0; i < voices.length; i++) {
let option = document.createElement('option');
Expand All @@ -88,6 +97,14 @@ document.addEventListener('DOMContentLoaded', function() {
}
console.log('Options: loaded ' + voices.length + ' voices.');
loadOptions();
}

if(speechSynthesis.getVoices().length > 0) {
loadVoices();
}

window.speechSynthesis.onvoiceschanged = function() {
loadVoices();
};

if(window.location.href.endsWith('?dark')) {
Expand Down
53 changes: 37 additions & 16 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
function fixStorage() {
if(navigator.userAgent.search('Chrome') == -1) {
// Seems like storage.sync doesn't work on Firefox.
chrome.storage.sync = chrome.storage.local;
console.log('WARNING: Using local storage.');
}
}
fixStorage();

var options = {
voiceType: '',
voice: null,
Expand Down Expand Up @@ -193,6 +202,8 @@ function getTextWithAlts(e) {
var watcher = null;
function initWatching() {
console.log('yt-live-text2speech: initializing...')

initInterface();
watcher = new ChatWatcher();

// without .iron-selected = detached chat
Expand Down Expand Up @@ -296,9 +307,13 @@ function initInterface() {
');
$(menu).find('paper-menu > div').append($option);

let $optionName = $('<'+prefix2+'-formatted-string class="style-scope '+prefix+'-menu-service-item-renderer x-scope '+prefix2+'-formatted-string-0"></'+prefix2+'-formatted-string>');
$option.append($optionName);
$optionName[0].innerHTML = 'Speech options';
setTimeout(function() {
let $optionName = $('<'+prefix2+'-formatted-string class="style-scope '+prefix+'-menu-service-item-renderer x-scope '+prefix2+'-formatted-string-0"></'+prefix2+'-formatted-string>');
$option.append($optionName);
setTimeout(function() {
$optionName[0].innerHTML = 'Speech options';
}, 1);
}, 1);

if(isYTGaming()) {
// Some YTG artifact
Expand Down Expand Up @@ -444,27 +459,33 @@ function isDarkMode() {
return isYTGaming() || document.body.getAttribute('dark') == 'true';
}

function loadVoices() {
if(voices.length == 0) {
voices = speechSynthesis.getVoices();
console.log('Loaded ' + voices.length + ' voices.');
updateVoice();

if(watcher === null) {
// Init chat after 2s (simple way to prevent reading old messages)
setTimeout(initWatching, 2000);
}
}
}

$(document).ready(function() {
console.log('yt-live-text2speech ready!');
if(speechSynthesis.getVoices().length > 0) {
loadVoices();
}

initInterface();
speechSynthesis.onvoiceschanged = function() {
// For some reason, this event can fire multiple times.
if(voices.length == 0) {
voices = speechSynthesis.getVoices();
console.log('Loaded ' + voices.length + ' voices.');
updateVoice();

if(watcher === null) {
// Init chat after 2s (simple way to prevent reading old messages)
setTimeout(initWatching, 2000);
}
}
// For some reason, this event can fire multiple times (Chromium).
loadVoices();
};
});

window.onbeforeunload = function() {
// Browser won't stop speaking after closing tab.
// Chromium won't stop speaking after closing tab.
// So shut up, pls.
speechSynthesis.cancel();
};

0 comments on commit 5546d4e

Please sign in to comment.