Created
February 20, 2023 18:39
-
-
Save ChaseH88/944bb610ee1dfcf4c449630a60bce19f to your computer and use it in GitHub Desktop.
This is a script that can be used directly in the console of ChatGPT to create voice playback buttons so you can hear the responses.
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
// ==UserScript== | |
// @name Add Button Before Element - ChatGPT | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Add HTML button before an element on the page. | |
// @author Chase Harrison | |
// @match https://chat.openai.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const buttonStyles = { | |
shared: { | |
borderColor: 'rgba(86,88,105,var(--tw-border-opacity))', | |
color: 'rgba(217,217,227,var(--tw-text-opacity))', | |
fontSize: '.875rem', | |
lineHeight: '1.25rem', | |
padding: '0.5rem 0.75rem', | |
borderRadius: '0.25rem', | |
marginLeft: 'auto', | |
position: 'relative', | |
bottom: '5px', | |
}, | |
normal: { | |
backgroundColor: 'rgba(52,53,65,var(--tw-bg-opacity))' | |
}, | |
hover: { | |
backgroundColor: 'rgba(64,65,79,var(--tw-bg-opacity))' | |
}, | |
} | |
function createButton() { | |
const button = document.createElement('button'); | |
button.innerHTML = 'Play Sound'; | |
Object.assign(button.style, buttonStyles.shared, buttonStyles.normal); | |
return button; | |
} | |
// Add the button | |
function addButton() { | |
// Find the element to insert the button before | |
const elements = document.querySelectorAll('.markdown.prose'); | |
elements.forEach((elm) => { | |
if(elm.previousElementSibling?.nodeName === 'BUTTON') return; | |
const button = createButton(); | |
button.addEventListener('mouseenter', () => { | |
button.style.backgroundColor = buttonStyles.hover.backgroundColor; | |
}); | |
button.addEventListener('mouseleave', () => { | |
button.style.backgroundColor = buttonStyles.normal.backgroundColor; | |
}); | |
// Add an event listener to the button | |
button.addEventListener('click', () => { | |
if(button.classList.contains('playing')) { | |
window.speechSynthesis.cancel(); | |
button.innerHTML = 'Play Sound'; | |
button.classList.remove('playing'); | |
return; | |
} | |
button.innerHTML = 'Playing...'; | |
button.classList.add('playing'); | |
const msg = new SpeechSynthesisUtterance(button.nextElementSibling.textContent); | |
msg.rate = 0.825; | |
window.speechSynthesis.speak(msg); | |
msg.addEventListener('end', () => { | |
button.innerHTML = 'Play Sound'; | |
button.classList.remove('playing'); | |
}); | |
}); | |
// Insert the button before the element | |
elm.parentNode.insertBefore(button, elm); | |
}); | |
} | |
window.addEventListener('keydown', (event) => { | |
if (event.key === 'Escape') { | |
window.speechSynthesis.cancel(); | |
const buttons = document.querySelectorAll('button.playing'); | |
buttons.forEach((button) => { | |
button.innerHTML = 'Play Sound'; | |
button.classList.remove('playing'); | |
}); | |
} | |
}); | |
setInterval(() => { | |
addButton(); | |
}, 2000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment