Skip to content

Instantly share code, notes, and snippets.

@happylinks
Created April 17, 2024 13:14
Show Gist options
  • Save happylinks/f328431a02d754aa66fa02fa92fad60f to your computer and use it in GitHub Desktop.
Save happylinks/f328431a02d754aa66fa02fa92fad60f to your computer and use it in GitHub Desktop.
Interrupt ChatGPT when it's typing and you just want to send something else without clicking stop first
document.addEventListener("DOMContentLoaded", (event) => {
function handleElementAppeared(textarea) {
console.log(textarea);
textarea.addEventListener("keydown", e => {
if (e.key === "Enter") {
var stopButton = document.querySelector('[aria-label="Stop generating"]');
if (stopButton) {
stopButton.click();
textarea.submit();
}
}
});
};
const config = { childList: true, subtree: true };
const callback = function(mutationsList, observer) {
for (const mutation of mutationsList) {
if (mutation.addedNodes.length > 0) {
mutation.addedNodes.forEach(node => {
if (node.id === 'prompt-textarea') {
handleElementAppeared(node);
} else if (node.querySelector) {
let targetElement = node.querySelector('#prompt-textarea');
if (targetElement) {
handleElementAppeared(targetElement);
}
}
});
}
}
};
const observer = new MutationObserver(callback);
observer.observe(document.body, config);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment