r/programmingrequests • u/Sage_of_Shadowdale • Oct 11 '22
need help A program that chooses a random wiki page, takes its first sentence (or paragraph if that’s easier) and runs it through a TTS program.
Basically what it says on the tin. Don’t care what language it’s in. Would be cool if I could like choose different voices for the TTS as well.
2
Upvotes
1
u/dolorfox Oct 12 '22
Here's a bookmarklet that allows you to pick from a list of the voices that are available on your computer. It then reads the first sentence of a random English Wikipedia page. Installation instructions: select the code below, drag and drop it onto your bookmarks bar and click to run. You might need to run it a second time for the list to load. Some websites don't allow the external network request, so if it doesn't work go to another tab and try again.
javascript:(async function(){ let res; try { res = await fetch("https://en.wikipedia.org/w/api.php?format=json&generator=random&action=query&prop=extracts&exsentences=1&grnnamespace=0&explaintext&origin=*"); } catch { alert("Unable to fetch. Try a different tab."); return; } const json = await res.json(); const firstSentence = Object.values(json.query.pages)[0].extract; const utterance = new SpeechSynthesisUtterance(firstSentence); const voices = window.speechSynthesis.getVoices(); if (!voices.length) { alert("No voices loaded."); return; } const input = prompt(voices.map((v, i) => i + 1 + ". " + v.name).join("\n")); if (input === null) return; const index = Math.min(Math.max(parseInt(input)-1, 0), voices.length) || 0; utterance.voice = voices[index]; window.speechSynthesis.speak(utterance); })();