Can you send selected text to ChatGPT Webview?

Possible to use the save selected text variable like for other webviews, and have it be the input into the chat gpt webview window?

(6/2/23)

Thanks in advance. I couldn't find any help searching for this question before I posted it :disappointed:

I just needed something similar. There was an issue with the ChatGPT security policies, which previously prevented the BTT scripting from working on the chatgpt webview. With version 4.316 this should be resolved now.

Here is an example preset:
ChatGPT.bttpreset (23.2 KB)

Pressing cmd+r shows the ChatGPT floating menu
Pressing cmd+t executes a script on the webview, which gets the selected text, puts it into the prompt field and clicks the send button.

(async () => {

let selectedText = await get_string_variable({variable_name:'selected_text'})

let propmptTextArea = document.getElementById("prompt-textarea");
propmptTextArea.value = selectedText;


let fakeInputEvent = new Event('input', { bubbles: true});
propmptTextArea.dispatchEvent(fakeInputEvent);

let sendButton = document.querySelector('button.absolute');
sendButton.click()
setTimeout(() => {
  sendButton.click()

},100)
})();
1 Like

Awesome, thanks, just updated and it works great. (11/5/23)

I created an extra trigger that sends the selected text with a premade prompt. (because soon the questions will be "how do we select premade prompts?")

  • Trigger shortcut (⌥⌘V).... Action: add prompt before selected text.
  • In this example, the prompt was simply "Rewrite this" added as a prefix to selected text.
  • Result: Selected text COMBINED with the prompt gets sent to the text area of gptwebview.

Here's the code if you're interested :upside_down_face:

(async () => {
  let selectedText = await get_string_variable({ variable_name: 'selected_text' });

  // Place/define prefix/prompt
  let prefix = "Rewrite this";

  // Combine prefix + selected text
  let combinedText = `${prefix} ${selectedText}`;

  let propmptTextArea = document.getElementById("prompt-textarea");
  propmptTextArea.value = combinedText;

  let fakeInputEvent = new Event('input', { bubbles: true });
  propmptTextArea.dispatchEvent(fakeInputEvent);

  let sendButton = document.querySelector('button.absolute');
  sendButton.click();

  setTimeout(() => {
    sendButton.click();
  }, 100);
})();

I'm sure someone can make another view where after selecting text, you can hit a trigger, a view pops up that allows you to select between 4 (premade) prompts before sending selected text into the webview. Shouldn't be too difficult since BTT can already incorporate all this. Anyways, thanks for the update!

1 Like