TTS over javascript output

Hello, I wish to allow javasript output to read it aloud as AnkiDroid (Android) TTS, but it can only read plain texts as it is repreasented in class = shuffle ( but i want to hide it from here and the shuffled output to be read by TTS) and html texts but it cannot read html output from javasript which is generated array. So please guide me so that i can force TTS to read the shuffled options to be read aloud.

<div class="shuffle">{{Opt1}}</div>
<div class="shuffle">{{Opt2}}</div>
<div class="shuffle">{{Opt3}}</div>
<div class="shuffle">{{Opt4}}</div>
<div id="container"></div>

   function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    return array;

// Select elements using querySelectorAll and convert NodeList to array
const elements = Array.from(document.querySelectorAll('.shuffle'));

// Shuffle the elements
const shuffledElements = shuffleArray(elements);

// Add prefixes to each element's text content
const prefixedElements =, index) => {
    el.textContent = `${['(A)', '(B)', '(C)', '(D)'][index]} ${el.textContent}`;
    return el;

// Get the container element by ID
const container = document.getElementById('container');

// Append each element to the container
prefixedElements.forEach(el => container.appendChild(el));

// Log each element to the console (optional)
// prefixedElements.forEach(el => console.log(el.textContent));

I’m not sure I quite understand but isn’t this what you want:

  var api;
  var jsApiContract = {
    version: "0.0.3",
    developer: "your_email_or_github_here"
  try {
    api = new AnkiDroidJS(jsApiContract);
    // Set the language the content is in
  } catch {}

prefixedElements.forEach(el => {
    // Wait until previous speak ends before speaking
    api.ankiTtsSpeak(el.textContent, 1)

Or to speak out the non-prefixed elements, just replace prefixedElements with shuffledElements in the forEach loop.

Documentation on the AnkiDroid TTS functions

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.