🐛 Fix: Use direct fetch only for Gemini (remove chrome.runtime.sendMessage which was failing)

This commit is contained in:
Hamza-Ayed
2026-06-02 17:56:32 +03:00
parent 873b965f90
commit d5919fbf01

View File

@@ -374,35 +374,14 @@
isGeminiProcessing = true;
try {
// Try via background service worker first
let processedText = null;
// Try direct fetch to server (more reliable than chrome.runtime.sendMessage)
try {
const response = await chrome.runtime.sendMessage({
type: 'GEMINI_PROCESS_VOICE',
payload: {
apiKey: settings.geminiApiKey,
model: settings.geminiModel,
text: text,
language: settings.language
}
});
const isArabic = /[\u0600-\u06FF]/.test(text);
const langName = isArabic ? 'Arabic' : 'the detected language';
if (response && response.success) {
processedText = response.data.text;
} else {
console.warn('[ClaudeVoice] Background response not successful:', response?.error);
}
} catch (bgErr) {
console.warn('[ClaudeVoice] Background message failed, trying direct fetch:', bgErr);
}
// Fallback: direct fetch to server
if (!processedText) {
try {
const isArabic = /[\u0600-\u06FF]/.test(text);
const langName = isArabic ? 'Arabic' : 'the detected language';
const prompt = `You are a text refinement assistant. Your task is to:
const prompt = `You are a text refinement assistant. Your task is to:
1. Correct any speech recognition errors in the following text
2. Fix punctuation, capitalization, and formatting
@@ -417,36 +396,35 @@ ${text}
CORRECTED TEXT:`;
const directResponse = await fetch('https://cv.intaleqapp.com/cv/server/generate_cv.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
action: 'generateText',
apiKey: settings.geminiApiKey,
prompt: prompt
})
});
const directResponse = await fetch('https://cv.intaleqapp.com/cv/server/generate_cv.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
action: 'generateText',
apiKey: settings.geminiApiKey,
prompt: prompt
})
});
if (directResponse.ok) {
const rawText = await directResponse.text();
let data;
try {
data = JSON.parse(rawText);
} catch (e) {
data = rawText;
}
if (data && data.candidates && data.candidates[0]) {
processedText = data.candidates[0].content?.parts?.[0]?.text;
} else if (typeof data === 'string') {
processedText = data;
}
} else {
console.warn('[ClaudeVoice] Direct fetch failed:', directResponse.status);
if (directResponse.ok) {
const rawText = await directResponse.text();
let data;
try {
data = JSON.parse(rawText);
} catch (e) {
data = rawText;
}
} catch (fetchErr) {
console.warn('[ClaudeVoice] Direct fetch error:', fetchErr);
if (data && data.candidates && data.candidates[0]) {
processedText = data.candidates[0].content?.parts?.[0]?.text;
} else if (typeof data === 'string') {
processedText = data;
}
} else {
console.warn('[ClaudeVoice] Server fetch failed:', directResponse.status);
}
} catch (fetchErr) {
console.warn('[ClaudeVoice] Server fetch error:', fetchErr);
}
if (processedText) {