Cannot set prototype of SpeechSynthesisVoice manually

Viewed 808

This question is somewhat for understanding the prototype get-set.

I've a scenario where I need to map the SpeechSynthesis supported voice to Google Translation API supported language codes. For example,

enter image description here

Now, I can do the same by either fetching the voices runtime or by storing the voice and hard-coding the mapping in some method in javascript. If I go for runtime approach, I need to call getVoice() in speechSynthesis.onvoiceschanged = () => {} and then map the numbers, which gets called in every voice change event. So, I want to go for hard-coding one.

Now, when I store the mapping array in a variable and call it by index, I get the SpeechSynthesisVoice Object, just like what we do in getVoices()[index].

Further, If I set this object value to speechSynthesis.voice, I get an error:

Uncaught TypeError: Failed to set the 'voice' property on 'SpeechSynthesisUtterance': The provided value is not of type 'SpeechSynthesisVoice'.

This is due to mis-match of prototype of the manually stored object value.

For example,

1. SpeechSynthesisVoice object:

enter image description here

2. Manually stored value of SpeechSynthesisVoice object:

enter image description here

To resolve, this I've fetched the proto of the SpeechSynthesisVoice object using getVoice(), and then set it to a variable and further, set this variable to my manual mapped object. Like,

Get:

voicePrototype = getVoices()[9].__proto__;

Set:

voices[index].SpeechSynthesisVoice.__proto__ = voicePrototype;

And, it gets set, as in below screenshot:

enter image description here

I've also tried through Object.setPrototypeOf() and got the same result.

Now, again when I want to set this object to speechSynthesis.voice, I still get the same error, although my prototype matches.

Can anyone, please advise, if its possible to set the object proto likewise and use it? Thanks in advance.

0 Answers
Related