I asked ChatGPT to make me a bookmarklet that alerts the user and prompts to ask a question, then using the user’s input and alerting the answer back. It generated this code after lots and lots of trying and it still doesn’t work:
javascript:(async function() {
const question = prompt("What is your question?");
const typingIndicator = document.createElement("div");
typingIndicator.textContent = "ChatGPT is typing...";
typingIndicator.style.position = "fixed";
typingIndicator.style.bottom = "20px";
typingIndicator.style.left = "20px";
typingIndicator.style.background = "rgba(255, 255, 255, 0.9)";
typingIndicator.style.padding = "10px";
document.body.appendChild(typingIndicator);
const response = await fetch("https://api.openai.com/v1/engines/davinci/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer INSERT_YOUR_API_KEY_HERE"
},
body: JSON.stringify({
prompt: question,
max_tokens: 1024,
temperature: 0.7
})
});
const data = await response.json();
typingIndicator.remove();
alert(data.choices[0].text);
})();
(Yes, I did enter my own API key, I just removed it here for security reasons)
What’s wrong with the code?
I asked it over and over again to troubleshoot and regenerate the code with different guidelines, but to no avail. The only reason I’m asking ChatGPT is because I’m really inexperienced with javscript. What happens is the prompt shows up, and you enter the question. At the bottom left of the screen, “ChatGPT is typing…” pops up for not even half a second and leaves the question unanswered.