Search code examples
javascriptscreen-readers

Activating device native screan reader


Is there a JavaScript API to query the device's native screen reader to start?

It would be OK if support was very limited but preferable, if it was at least a standard, so that in the future, more and more devices would be able to use it. A bit like the share API.

Edit

I changed the tags, because it was wrong to tag it accessibility - the function was not intended for the visually impaired, but so that people can listen to a certain news article on our site, rather than reading it.

Also the screenreader should only start on genuine user input (a button click), not automatically. (I imagine a web standard would ensure that anyway. Like with requestFullscreen)


Solution

  • As discussed in the comments, there is no API for activating a native screen reader (if any), but you could want something like the Web Speech API, in particular speech synthesis.

    You could simply pass in the innerText of your article-to-speak and get decent output.

    function speak() {
      var article = document.querySelector('article');
      var msg = new SpeechSynthesisUtterance(article.innerText);
      window.speechSynthesis.speak(msg);
    }
    body {
      font-family: garamond, georgia, serif;
      background-color: #f5f5f5;
      color: #333;
      text-align: center;
    }
    
    article {
      width: 60%;
      margin: 40px auto;
      padding: 20px;
      background-color: #ffffff;
      box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
      font-size: 18px;
    }
    
    h1 {
      font-weight: bold;
      text-align: center;
      margin-bottom: 20px;
      border-bottom: 2px solid #333;
      padding-bottom: 10px;
    }
    
    .byline {
      font-style: italic;
      color: #666;
    }
    
    p {
      margin-bottom: 20px;
      text-align: justify;
    }
    
    article p:first-child::first-letter {
      font-size: 26px;
      font-weight: bold;
      float: left;
      line-height: 1;
      padding-right: 5px;
    }
      <button onclick="speak()">Speak article</button>
      <article>
        <h1>BREAKING: Cows Plot World Domination</h1>
    
        <p>
          <b class="byline">Bovington, September 2, 2024</b> In a stunning development, cows around the world have announced plans to take over the planet. Led by a charismatic cow named “Moolinda,” the bovines are demanding control of global dairy supplies
          and have formed alliances with goats and chickens.
        </p>
    
        <p>
          Their strategy? A social media campaign called #COWTAKEOVER and a new milk-based currency. World leaders are scrambling to respond, but experts warn that the cows’ next move could be anything—and the steaks have never been higher.
        </p>
      </article>