Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more refactoring on the initSpeech function
  • Loading branch information
jeanem committed Jun 25, 2024
commit 64695685e33bead059e26060d598e1a16f4f2791
49 changes: 18 additions & 31 deletions build/ableplayer.dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -7697,52 +7697,39 @@ var AblePlayerInstances = [];
AblePlayer.prototype.initSpeech = function (context) {
var thisObj = this;

// Function to handle the initial click and enable speech synthesis
function handleInitialClick() {
// Function to attempt enabling speech synthesis
function attemptEnableSpeech() {
var greeting = new SpeechSynthesisUtterance("\x20");
thisObj.synth.speak(greeting);
greeting.onstart = function () {
// Once the utterance starts, remove this specific click event listener
// Ensures the event handler only runs once and cleans up after itself
$(document).off("click", handleInitialClick);
};
greeting.onend = function () {
// Attempt to fetch browser voices and enable speech if successful
thisObj.getBrowserVoices();
thisObj.speechEnabled = true;
if (thisObj.descVoices.length || context !== "init") {
thisObj.speechEnabled = true;
}
};
}

if (this.speechEnabled === null) {
if (typeof this.synth !== "undefined") {
// Cancel any previous synth instance and reinitialize
this.synth.cancel();
}
// Function to handle the initial click and enable speech synthesis
function handleInitialClick() {
attemptEnableSpeech();
// Once the utterance starts, remove this specific click event listener
// Ensures the event handler only runs once and cleans up after itself
$(document).off("click", handleInitialClick);
}

if (this.speechEnabled === null) {
if (window.speechSynthesis) {
// Browser supports speech synthesis
this.synth = window.speechSynthesis;
this.synth.cancel();
this.synth.cancel(); // Cancel any ongoing speech synthesis

if (context === "init") {
// Bind the click event listener using a named function to ensure it can be specifically unbound later within the handleInitialClick function
// For initial setup, require a user click to enable speech synthesis
// Scoping to a particular handler to avoid conflicts with other click events
$(document).on("click", handleInitialClick);

// Call getBrowserVoices in case it works without a click, for browsers that don't require a click
this.getBrowserVoices();
if (this.descVoices.length) {
this.speechEnabled = true;
}
} else {
// Context is either 'play', 'prefs', or 'desc'
// Directly attempt to enable speech synthesis without additional user action
var greeting = new SpeechSynthesisUtterance("\x20");
thisObj.synth.speak(greeting);
greeting.onend = function () {
// Attempt to fetch browser voices and enable speech if successful
thisObj.getBrowserVoices();
thisObj.speechEnabled = true;
};
// For other contexts, attempt to enable speech synthesis directly
attemptEnableSpeech();
}
} else {
// Browser does not support speech synthesis
Expand Down
49 changes: 18 additions & 31 deletions build/ableplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7697,52 +7697,39 @@ var AblePlayerInstances = [];
AblePlayer.prototype.initSpeech = function (context) {
var thisObj = this;

// Function to handle the initial click and enable speech synthesis
function handleInitialClick() {
// Function to attempt enabling speech synthesis
function attemptEnableSpeech() {
var greeting = new SpeechSynthesisUtterance("\x20");
thisObj.synth.speak(greeting);
greeting.onstart = function () {
// Once the utterance starts, remove this specific click event listener
// Ensures the event handler only runs once and cleans up after itself
$(document).off("click", handleInitialClick);
};
greeting.onend = function () {
// Attempt to fetch browser voices and enable speech if successful
thisObj.getBrowserVoices();
thisObj.speechEnabled = true;
if (thisObj.descVoices.length || context !== "init") {
thisObj.speechEnabled = true;
}
};
}

if (this.speechEnabled === null) {
if (typeof this.synth !== "undefined") {
// Cancel any previous synth instance and reinitialize
this.synth.cancel();
}
// Function to handle the initial click and enable speech synthesis
function handleInitialClick() {
attemptEnableSpeech();
// Once the utterance starts, remove this specific click event listener
// Ensures the event handler only runs once and cleans up after itself
$(document).off("click", handleInitialClick);
}

if (this.speechEnabled === null) {
if (window.speechSynthesis) {
// Browser supports speech synthesis
this.synth = window.speechSynthesis;
this.synth.cancel();
this.synth.cancel(); // Cancel any ongoing speech synthesis

if (context === "init") {
// Bind the click event listener using a named function to ensure it can be specifically unbound later within the handleInitialClick function
// For initial setup, require a user click to enable speech synthesis
// Scoping to a particular handler to avoid conflicts with other click events
$(document).on("click", handleInitialClick);

// Call getBrowserVoices in case it works without a click, for browsers that don't require a click
this.getBrowserVoices();
if (this.descVoices.length) {
this.speechEnabled = true;
}
} else {
// Context is either 'play', 'prefs', or 'desc'
// Directly attempt to enable speech synthesis without additional user action
var greeting = new SpeechSynthesisUtterance("\x20");
thisObj.synth.speak(greeting);
greeting.onend = function () {
// Attempt to fetch browser voices and enable speech if successful
thisObj.getBrowserVoices();
thisObj.speechEnabled = true;
};
// For other contexts, attempt to enable speech synthesis directly
attemptEnableSpeech();
}
} else {
// Browser does not support speech synthesis
Expand Down
2 changes: 1 addition & 1 deletion build/ableplayer.min.js

Large diffs are not rendered by default.

49 changes: 18 additions & 31 deletions scripts/description.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,52 +157,39 @@
AblePlayer.prototype.initSpeech = function (context) {
var thisObj = this;

// Function to handle the initial click and enable speech synthesis
function handleInitialClick() {
// Function to attempt enabling speech synthesis
function attemptEnableSpeech() {
var greeting = new SpeechSynthesisUtterance("\x20");
thisObj.synth.speak(greeting);
greeting.onstart = function () {
// Once the utterance starts, remove this specific click event listener
// Ensures the event handler only runs once and cleans up after itself
$(document).off("click", handleInitialClick);
};
greeting.onend = function () {
// Attempt to fetch browser voices and enable speech if successful
thisObj.getBrowserVoices();
thisObj.speechEnabled = true;
if (thisObj.descVoices.length || context !== "init") {
thisObj.speechEnabled = true;
}
};
}

if (this.speechEnabled === null) {
if (typeof this.synth !== "undefined") {
// Cancel any previous synth instance and reinitialize
this.synth.cancel();
}
// Function to handle the initial click and enable speech synthesis
function handleInitialClick() {
attemptEnableSpeech();
// Once the utterance starts, remove this specific click event listener
// Ensures the event handler only runs once and cleans up after itself
$(document).off("click", handleInitialClick);
}

if (this.speechEnabled === null) {
if (window.speechSynthesis) {
// Browser supports speech synthesis
this.synth = window.speechSynthesis;
this.synth.cancel();
this.synth.cancel(); // Cancel any ongoing speech synthesis

if (context === "init") {
// Bind the click event listener using a named function to ensure it can be specifically unbound later within the handleInitialClick function
// For initial setup, require a user click to enable speech synthesis
// Scoping to a particular handler to avoid conflicts with other click events
$(document).on("click", handleInitialClick);

// Call getBrowserVoices in case it works without a click, for browsers that don't require a click
this.getBrowserVoices();
if (this.descVoices.length) {
this.speechEnabled = true;
}
} else {
// Context is either 'play', 'prefs', or 'desc'
// Directly attempt to enable speech synthesis without additional user action
var greeting = new SpeechSynthesisUtterance("\x20");
thisObj.synth.speak(greeting);
greeting.onend = function () {
// Attempt to fetch browser voices and enable speech if successful
thisObj.getBrowserVoices();
thisObj.speechEnabled = true;
};
// For other contexts, attempt to enable speech synthesis directly
attemptEnableSpeech();
}
} else {
// Browser does not support speech synthesis
Expand Down