brooklynsweb
brooklynsweb

Reputation: 817

Passing questions to ChatGPT via URL

So officially you cannot pass ChatGPT questions via the browser URL (only via API), unofficially however, I've gotten very close by creating a user script, which essentially reads a parameter I pass to chatgpt, with the question, then interacts with the UI to submit it... well, that's the goal anyway.

I'm not really a coder and I got as far as this (code below); I'm able to pull the question from the URL and input it into the text area (both value and innerHTML) but I can't submit it; it seems you have to manually press a key or spacebar and then click on the submit button.

Question pattern I'm using: https://chat.openai.com/?question:what+is+a+cow

Javascript:

// 1. Pull out the question from the URL
window.addEventListener('load', function () {
  console.log('DOMContentLoaded event fired.');

  const url = window.location.href;
  console.log('URL:', url);
  const match = url.match(/\?question:(.*)/);

  if (match) {
    const questionParam = match[1];
    const question = decodeURIComponent(questionParam.replace(/\+/g, ' '));
    console.log('Matched Question:', question);

    // 2. Insert the string into the text-area object with the id "prompt-textarea"
    const textarea = document.querySelector('#prompt-textarea');

    if (textarea) {
        setTimeout(function() {textarea.value = question;}, 2000); // 2000 milliseconds = 2 seconds

setTimeout(function() {textarea.innerHTML = question;}, 2000); // 2000 milliseconds = 2 seconds

      console.log(textarea.value);
    } else {
      console.error('Textarea element with id "prompt-textarea" not found.');
    }
  } else {
    console.error('URL pattern does not match.');
  }
});

And as far as trying to simulate keboard intearctions, I tried this (code below), but regardless of where I drop it (bookmarklet, console, the user script itself, there seems to be a focus issue that I can't get over. While in console the I get 'true' so it should be working but it's not.

setTimeout(function() { alert(1);
    let myElement =  document.querySelector('#prompt-textarea');
    myElement.focus();
    myElement.dispatchEvent(new KeyboardEvent('keydown', { key: '' }));
    myElement.dispatchEvent(new KeyboardEvent('keyup', { key: '' }));
    myElement.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));
    myElement.dispatchEvent(new KeyboardEvent('keyup', { key: 'Enter' }));

Guidance would be appreciated; thanks in advance!

Upvotes: 0

Views: 692

Answers (1)

Josh
Josh

Reputation: 161

im assuming you can set the text proper so, are you trying to simulate enter / submit answer button on chatpgt?

if so you can invoke the button press like this (javascript):

var t = document.querySelector('button[data-testid="send-button"]');
t.click();

Upvotes: 0

Related Questions