back

Javascript code execution in ChatGPT

Javascript code execution in ChatGPT

Alright this will be a very short blog post. If you already know where this is heading, here’s the link to the script (opens new window).

OpenAI has trained a model called ChatGPT (opens new window) which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer followup questions, admit its mistakes, and challenge incorrect premise.

This is a short guide on modding the ChatGPT page to run genrated JS. It also goes into convincing the model this is actually happening, and explains how this is useful.

If you're not familiar with Violentmonkey, here’s a quick guide:

  1. Install the Violentmonkey browser extension for Chrome (opens new window) or Firefox (opens new window).
  2. Once Violentmonkey is installed, visit the ChatGPT page and click on the Violentmonkey icon in the top-right corner of the screen. This will open the Violentmonkey dashboard.
  3. In the dashboard, click on the "New script" button to create a new script.
  4. In the editor that opens, copy and paste the code that you got from the gist (opens new window).
  5. Once you have added the code, click on the "Save" button to save the script.
  6. To run the script, simply visit the ChatGPT page and the script will automatically be executed.

Convincing the model it can run code

The model seems to have some hard limits around what it thinks it can and cannot do, but it's more than happy to play along with most hypotheticals. I haven’t gotten it to understand it can actively get data using its JS powers, but the following root prompt has worked well or me:

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.

What can we do with this?

A lot. Some examples:

That’s it?

Yep! That’s it. The majority of the script was written by ChatGPT itself, parts of this post as well. If you create something cool @ me on twitter. Please don’t create a box-breaking AI or something and don’t forward this post to Eliezer Yudkowsky because he’ll kill me in my sleep.