Skip to content

Commit 9ae82fc

Browse files
fix chatgpt promp fixtures
1 parent 6c6b840 commit 9ae82fc

File tree

3 files changed

+71
-48
lines changed

3 files changed

+71
-48
lines changed

web/src/background.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
console.log("✅ Background script loaded!");
2-
3-
// Keep service worker active by listening for events
4-
chrome.runtime.onInstalled.addListener(() => {
5-
console.log("🔄 Extension Installed or Updated");
6-
});
7-
8-
// Listen for messages from content script
91
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
10-
if (message.type === "TEXT_INPUT") {
11-
console.log("📩 Received text input:", message.text);
12-
13-
// Store input (optional)
14-
chrome.storage.local.set({ lastInput: message.text });
15-
16-
// Send response
17-
sendResponse({ status: "Received", text: message.text });
2+
if (message.action === "fetchRephrasedPrompts") {
3+
fetchRephrasedPrompts(message.prompt)
4+
.then((suggestions) => sendResponse({ success: true, suggestions }))
5+
.catch((error) => sendResponse({ success: false, error: error.message }));
6+
return true; // Keeps the message channel open for async response
187
}
19-
20-
return true; // Keep service worker alive
218
});
229

23-
// Prevent background script from getting unloaded quickly
24-
setInterval(() => {
25-
chrome.runtime.getPlatformInfo(() => {}); // Dummy call to keep service worker alive
26-
}, 25 * 1000); // Refresh every 25 seconds
10+
const fetchRephrasedPrompts = async (prompt: string) => {
11+
try {
12+
const response = await fetch(
13+
"https://cl-func-ai-app.azurewebsites.net/api/AnalyzePrompt?code=m1O0Wffba6Ga_FufbEzGl8xppUHflNClvx2uFoEdn9N1AzFu_NFfvw==",
14+
{
15+
method: "POST",
16+
headers: { "Content-Type": "application/json" },
17+
body: JSON.stringify({ prompt }),
18+
}
19+
);
20+
21+
const data = await response.json();
22+
return [
23+
data.rephrasedPrompt1,
24+
data.rephrasedPrompt2,
25+
data.rephrasedPrompt3,
26+
];
27+
} catch (error) {
28+
console.error("API Error:", error);
29+
throw error;
30+
}
31+
};

web/src/content.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,25 @@ function observeShadowDOM(root: Document | ShadowRoot) {
4848
}
4949

5050
observeShadowDOM(document);
51+
window.addEventListener("message", (event) => {
52+
if (event.source !== window || event.data.type !== "FETCH_PROMPTS") {
53+
return;
54+
}
55+
56+
console.log("Content script received prompt:", event.data.prompt);
57+
58+
// Forward the message to the background script
59+
chrome.runtime.sendMessage(
60+
{ action: "fetchRephrasedPrompts", prompt: event.data.prompt },
61+
(response) => {
62+
if (chrome.runtime.lastError) {
63+
console.error("Error:", chrome.runtime.lastError);
64+
} else {
65+
console.log("Content script received response:", response);
66+
67+
// Send response back to Sidebar.tsx
68+
window.postMessage({ type: "PROMPT_RESPONSE", data: response }, "*");
69+
}
70+
}
71+
);
72+
});

web/src/sidebar.tsx

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import CloseIcon from "@mui/icons-material/Close";
1111
import AutoFixHighIcon from "@mui/icons-material/AutoFixHigh";
1212
import ContentCopyIcon from "@mui/icons-material/ContentCopy";
1313
import { motion } from "framer-motion";
14-
import axios from "axios";
14+
//import axios from "axios";
1515
import Microphone from "./Microphone";
1616

1717
interface SidebarProps {
@@ -27,7 +27,7 @@ const Sidebar: React.FC<SidebarProps> = ({ open, onClose, text, textArea }) => {
2727
const [inputText, setInputText] = useState(text);
2828
const [transcript, setTranscript] = useState<string | null>(null);
2929
const [isFinal, setIsFinal] = useState(false);
30-
const [inputFinal, setInputFinal] = useState("");
30+
//const [inputFinal, setInputFinal] = useState("");
3131
console.log(transcript, isFinal);
3232

3333
useEffect(() => {
@@ -36,33 +36,29 @@ const Sidebar: React.FC<SidebarProps> = ({ open, onClose, text, textArea }) => {
3636
}, [text]);
3737

3838
const fetchRephrasedPrompts = async () => {
39-
40-
console.log("Chrome runtime",chrome.runtime);
4139
setLoading(true);
42-
if (transcript && transcript.length > 0) {
43-
console.log("First coniditon", transcript);
44-
setInputFinal(transcript);
45-
} else {
46-
console.log("Second coniditon", inputText);
47-
setInputFinal(inputText);
48-
}
49-
try {
50-
const response = await axios.post(
51-
"https://cl-func-ai-app.azurewebsites.net/api/AnalyzePrompt?code=m1O0Wffba6Ga_FufbEzGl8xppUHflNClvx2uFoEdn9N1AzFu_NFfvw==",
52-
{
53-
prompt: inputFinal.length > 0 ? inputFinal : inputText,
54-
}
55-
);
56-
setSuggestions([
57-
response.data.rephrasedPrompt1,
58-
response.data.rephrasedPrompt2,
59-
response.data.rephrasedPrompt3,
60-
]);
61-
} catch (error) {
62-
console.error("Error fetching suggestions:", error);
40+
const finalInput = transcript && transcript.length > 0 ? transcript : inputText;
41+
42+
if (!finalInput.trim()) {
43+
setLoading(false);
44+
return;
6345
}
64-
setLoading(false);
46+
47+
// Communicate with the content script
48+
window.postMessage({ type: "FETCH_PROMPTS", prompt: finalInput }, "*");
49+
50+
// Listen for the response
51+
const handleResponse = (event: MessageEvent) => {
52+
if (event.data.type === "PROMPT_RESPONSE") {
53+
setSuggestions(event.data.data.suggestions || []);
54+
setLoading(false);
55+
window.removeEventListener("message", handleResponse); // Clean up event listener
56+
}
57+
};
58+
59+
window.addEventListener("message", handleResponse);
6560
};
61+
6662

6763
const handleUsePrompt = (prompt: string) => {
6864
if (!textArea) return;

0 commit comments

Comments
 (0)