|
1 | 1 | // Combined content script
|
2 |
| - |
3 | 2 | if (!window.hasOpenAIScriptInjected) {
|
4 | 3 | window.hasOpenAIScriptInjected = true;
|
5 | 4 |
|
6 |
| - // Function to set the user's input in the textarea and click the send button |
| 5 | + // Function to set the user's input in the textarea and trigger the Enter key |
7 | 6 | function setAndSend(userInput) {
|
8 |
| - const textarea = document.getElementById("prompt-textarea"); |
9 |
| - textarea.value = userInput; |
10 |
| - // Dispatch an input event to simulate user typing |
11 |
| - const event = new Event("input", { |
12 |
| - bubbles: true, |
13 |
| - cancelable: true, |
14 |
| - }); |
15 |
| - textarea.dispatchEvent(event); |
| 7 | + const textarea = document.getElementById("prompt-textarea"); |
| 8 | + if (!textarea) { |
| 9 | + console.error("Textarea element not found"); |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + // Set the content of the contenteditable element |
| 14 | + textarea.textContent = userInput; |
| 15 | + |
| 16 | + // Dispatch an input event to simulate typing |
| 17 | + const inputEvent = new Event("input", { bubbles: true, cancelable: true }); |
| 18 | + textarea.dispatchEvent(inputEvent); |
16 | 19 |
|
17 |
| - // Simulate a click on the send button |
18 |
| - document.querySelector('[data-testid="send-button"]').click(); |
| 20 | + // Simulate Enter key press |
| 21 | + const enterKeyEvent = new KeyboardEvent("keydown", { |
| 22 | + key: "Enter", |
| 23 | + code: "Enter", |
| 24 | + bubbles: true, |
| 25 | + cancelable: true, |
| 26 | + }); |
| 27 | + textarea.dispatchEvent(enterKeyEvent); |
19 | 28 | }
|
20 | 29 |
|
| 30 | + // Function to process received SSE data |
21 | 31 | function processReceivedData(data) {
|
22 |
| - // Process the received SSE data |
23 |
| - console.log("Processed data:", data); |
24 |
| - chrome.runtime.sendMessage({ |
25 |
| - action: "receivedResponse", |
26 |
| - responseText: data, |
27 |
| - }); |
| 32 | + console.log("Processed data:", data); |
| 33 | + chrome.runtime.sendMessage({ |
| 34 | + action: "receivedResponse", |
| 35 | + responseText: data, |
| 36 | + }); |
28 | 37 | }
|
29 | 38 |
|
30 | 39 | // Function to create and inject the script
|
31 | 40 | async function injectScript() {
|
32 |
| - const script = document.createElement("script"); |
33 |
| - script.src = chrome.runtime.getURL('js/island.js'); |
34 |
| - document.documentElement.appendChild(script); |
35 |
| - script.remove(); |
| 41 | + const script = document.createElement("script"); |
| 42 | + script.src = chrome.runtime.getURL('js/island.js'); |
| 43 | + document.documentElement.appendChild(script); |
| 44 | + script.remove(); |
36 | 45 | }
|
37 | 46 |
|
38 | 47 | // Content script listener for messages from the injected script
|
39 | 48 | window.addEventListener("message", (event) => {
|
40 |
| - if(typeof event.data == 'string') { |
41 |
| - event.data = JSON.parse(event.data); |
42 |
| - } |
43 |
| - if (event.data && event.data.type === "FROM_PAGE_STREAM") { |
44 |
| - console.log("Content script received stream message :", event.data); |
45 |
| - chrome.runtime.sendMessage(event.data, response => { |
46 |
| - if (chrome.runtime.lastError) { |
47 |
| - console.error(chrome.runtime.lastError.message); |
48 |
| - } else { |
49 |
| - console.log("Content script received response:", response) |
50 |
| - } |
51 |
| - }); |
52 |
| - } |
| 49 | + if (typeof event.data === "string") { |
| 50 | + event.data = JSON.parse(event.data); |
| 51 | + } |
| 52 | + if (event.data && event.data.type === "FROM_PAGE_STREAM") { |
| 53 | + console.log("Content script received stream message:", event.data); |
| 54 | + chrome.runtime.sendMessage(event.data, (response) => { |
| 55 | + if (chrome.runtime.lastError) { |
| 56 | + console.error(chrome.runtime.lastError.message); |
| 57 | + } else { |
| 58 | + console.log("Content script received response:", response); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
53 | 62 | });
|
54 | 63 |
|
55 |
| - // Listen for messages from the background script |
| 64 | + // Listener for messages from the background script |
56 | 65 | chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
|
57 |
| - if (request.action === "setAndSend") { |
58 |
| - setAndSend(request.userInput); |
59 |
| - await injectScript(); |
60 |
| - sendResponse({ status: "Script injected" }); |
61 |
| - } |
| 66 | + if (request.action === "setAndSend") { |
| 67 | + setAndSend(request.userInput); // Update the text and simulate Enter key press |
| 68 | + await injectScript(); |
| 69 | + sendResponse({ status: "Script injected" }); |
| 70 | + } |
62 | 71 | });
|
63 | 72 |
|
64 | 73 | console.log("openai content script loaded");
|
|
0 commit comments