Skip to content

Instantly share code, notes, and snippets.

@MarcosNASA
Last active December 6, 2022 17:15
Show Gist options
  • Save MarcosNASA/c0beb0d04fe83c9585fb3e159e8fff00 to your computer and use it in GitHub Desktop.
Save MarcosNASA/c0beb0d04fe83c9585fb3e159e8fff00 to your computer and use it in GitHub Desktop.
Gives ChatGPT a voice
const debounce = (delay) => (fn) => {
let timeoutId;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
fn(...args);
}, delay);
};
};
const speak = (text) => {
const utterance = new SpeechSynthesisUtterance(text);
window.speechSynthesis.speak(utterance);
};
const debouncedSpeak = debounce(600)(speak);
const messagesContainer = document.querySelector(
'div[class*="h-full dark:bg-gray-800"]',
);
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
const [newMessageContainer] = mutation.addedNodes;
if (!newMessageContainer) continue;
if (!newMessageContainer.classList) continue;
if (
!Array.from(newMessageContainer.classList).includes('dark:bg-[#444654]')
)
continue;
const observer = new MutationObserver((mutations) => {
const [newMessage] = mutations;
debouncedSpeak(
newMessage.target.parentNode.closest('.prose').textContent,
);
});
observer.observe(newMessageContainer, {
subtree: true,
characterData: true,
characterDataOldValue: true,
});
}
});
observer.observe(messagesContainer, { subtree: true, childList: true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment