Created
January 16, 2023 08:23
-
-
Save dgnsrekt/b7b933e06eb77e4babfdf377d6456f1d to your computer and use it in GitHub Desktop.
Chat GPT TTS in chrome written and refactor by ChatGPT.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just dump this in the chrome devtools console and hit enter.