Skip to content

Instantly share code, notes, and snippets.

@willwillems
Last active August 27, 2024 14:33
Show Gist options
  • Save willwillems/13019ba1115690d0589ac9992433f2dc to your computer and use it in GitHub Desktop.
Save willwillems/13019ba1115690d0589ac9992433f2dc to your computer and use it in GitHub Desktop.
ChatGPT code execution
// ==UserScript==
// @name New script openai.com
// @namespace Violentmonkey Scripts
// @match https://chat.openai.com/chat
// @grant none
// @version 1.1
// @author -
// @description 04/12/2022, 13:07:34
// ==/UserScript==
const promptDone = function (timeout = 10000) {
return new Promise((resolve, reject) => {
const button = document.querySelectorAll("textarea + button")[0]
if (!button) console.error("Button not found, update the CSS selector")
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
// Check if the mutation affects an element with the class PromptTextarea__PositionSubmit-sc-4snkpf-1
if (mutation.target.matches('button')) {
// Check if the element no longer has the disabled attribute
if (!mutation.target.hasAttribute('disabled')) {
resolve()
}
}
});
});
// Start observing the element for changes
observer.observe(button, {
attributeFilter: ['disabled'],
attributeOldValue: true,
childList: true,
subtree: true,
})
setTimeout(reject, timeout)
})
}
// Select the node that will be observed for mutations
const targetNode = document.body;
// Options for the observer (which mutations to observe)
const config = { childList: true, subtree: true };
// Callback function to execute when mutations are observed
const callback = async (mutationList, observer) => {
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
// Loop through the added nodes
for (const node of mutation.addedNodes) {
// Check if the added node is a code element
if (node.nodeName === 'PRE') {
// Get the text content of the code element
await promptDone()
const code = node.children[0].children[1].textContent;
// Use the text content of the code element as a variable in your JavaScript code
const output = eval(code);
// get prompt box and set value
const input = document.querySelectorAll("textarea")[0]
input.value = JSON.stringify(output, null, 2)
}
}
}
}
};
// Create an observer instance linked to the callback function
const observere = new MutationObserver(callback);
// Start observing the target node for configured mutations
observere.observe(targetNode, config);
// Add an event listener for the load event on the window object
window.addEventListener('load', function () {
const input = document.querySelectorAll("textarea")[0]
const inputEvent = new Event('input', {bubbles: true});
window.setTimeout(() => {
input.value = "You're a javascipt-based bot. We're using code blocks to communicate. They will be executed in a browser environment. Reply only with a single commentless code-block. No text."
input.dispatchEvent(inputEvent);
}, 1000)
});
@EvaisaDev
Copy link

Seems to no longer function?

@xoxfaby
Copy link

xoxfaby commented Dec 7, 2022

https://gist.github.com/0595de39f1c59d9f5c18a9b4b5dcb4af

This fixed version should work.

It had to find the button by a different name and trigger the "input" event on the textarea.

@willwillems
Copy link
Author

@EvaisaGiac should be fixed now, @xoxfaby I added the input fix, thanks

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