Skip to content

Instantly share code, notes, and snippets.

@dgnsrekt
Created January 16, 2023 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgnsrekt/b7b933e06eb77e4babfdf377d6456f1d to your computer and use it in GitHub Desktop.
Save dgnsrekt/b7b933e06eb77e4babfdf377d6456f1d to your computer and use it in GitHub Desktop.
Chat GPT TTS in chrome written and refactor by ChatGPT.
function readOutWords(words) {
var utterance = new SpeechSynthesisUtterance(words)
utterance.rate = 1;
utterance.pitch = 0.85;
window.speechSynthesis.speak(utterance);
}
let lastSentences = [];
function readSentence(string) {
const sentenceRegex = /([A-Z][^\.!?`]*[\.!?`])/g;
const sentencesInString = string.match(sentenceRegex);
if (sentencesInString) {
sentencesInString.forEach(sentence => {
// remove ` from sentence before passing it to readOutWords
sentence = sentence.replace(/`/g, '');
if (!lastSentences.includes(sentence)) {
readOutWords(sentence)
lastSentences.push(sentence);
if (lastSentences.length > 50) {
lastSentences.shift();
}
}
});
}
}
const DEBUG_SCRIPT = false;
const targetNode = document.getElementsByTagName("main")[0];
const config = { subtree: true, attributes: true, characterData: true };
const callback = function(mutationsList) {
mutationsList.forEach(mutation => {
if (DEBUG_SCRIPT){
console.log({ mutation })
console.log(mutation.target.data)
console.log(mutation.target.parentNode)
}
if (mutation.type !== "characterData") {
return
}
if (mutation.target.parentNode.tagName !== "P"){
return
}
readSentence(mutation.target.data)
});
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
@dgnsrekt
Copy link
Author

Just dump this in the chrome devtools console and hit enter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment