Auto-deploy: 2026-06-02 18:48:35
This commit is contained in:
27
popup.js
27
popup.js
@@ -294,13 +294,13 @@ function initDictation() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen to messages from the offscreen document via the background
|
// Listen to messages from the injected script
|
||||||
chrome.runtime.onMessage.addListener((message) => {
|
chrome.runtime.onMessage.addListener((message) => {
|
||||||
if (message.type === 'OFFSCREEN_RECORDING_RESULT') {
|
if (message.type === 'SPEECH_RESULT') {
|
||||||
interimTranscript = message.payload.interimText || '';
|
interimTranscript = message.payload.interimText || '';
|
||||||
finalTranscript = message.payload.finalText || '';
|
finalTranscript = message.payload.finalText || '';
|
||||||
resultArea.value = (finalTranscript + interimTranscript).trim();
|
resultArea.value = (finalTranscript + interimTranscript).trim();
|
||||||
} else if (message.type === 'OFFSCREEN_RECORDING_ERROR') {
|
} else if (message.type === 'SPEECH_ERROR') {
|
||||||
console.error('Speech recognition error', message.payload.error);
|
console.error('Speech recognition error', message.payload.error);
|
||||||
if (message.payload.error === 'not-allowed') {
|
if (message.payload.error === 'not-allowed') {
|
||||||
statusEl.textContent = '❌ يرجى السماح للميكروفون من إعدادات المتصفح';
|
statusEl.textContent = '❌ يرجى السماح للميكروفون من إعدادات المتصفح';
|
||||||
@@ -309,7 +309,7 @@ function initDictation() {
|
|||||||
statusEl.textContent = '❌ خطأ: ' + message.payload.error;
|
statusEl.textContent = '❌ خطأ: ' + message.payload.error;
|
||||||
}
|
}
|
||||||
stopRecording(false);
|
stopRecording(false);
|
||||||
} else if (message.type === 'OFFSCREEN_RECORDING_END') {
|
} else if (message.type === 'SPEECH_END') {
|
||||||
if (isRecording) {
|
if (isRecording) {
|
||||||
stopRecording(true);
|
stopRecording(true);
|
||||||
}
|
}
|
||||||
@@ -355,19 +355,34 @@ function initDictation() {
|
|||||||
iframe.style.left = '0';
|
iframe.style.left = '0';
|
||||||
iframe.style.width = '1px';
|
iframe.style.width = '1px';
|
||||||
iframe.style.height = '1px';
|
iframe.style.height = '1px';
|
||||||
iframe.style.opacity = '0';
|
iframe.style.opacity = '0.01';
|
||||||
iframe.style.pointerEvents = 'none';
|
iframe.style.pointerEvents = 'none';
|
||||||
iframe.style.zIndex = '-9999';
|
iframe.style.zIndex = '-9999';
|
||||||
iframe.style.border = 'none';
|
iframe.style.border = 'none';
|
||||||
iframe.setAttribute('allow', 'microphone');
|
iframe.setAttribute('allow', 'microphone');
|
||||||
document.body.appendChild(iframe);
|
document.body.appendChild(iframe);
|
||||||
|
|
||||||
|
// Relay messages from iframe to popup
|
||||||
|
window.addEventListener('message', (event) => {
|
||||||
|
if (event.data && event.data.type && event.data.type.startsWith('SPEECH_')) {
|
||||||
|
chrome.runtime.sendMessage(event.data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Listen for messages from popup to iframe
|
||||||
|
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
||||||
|
if (msg.type === 'START_RECORDING_FROM_POPUP' || msg.type === 'STOP_RECORDING_FROM_POPUP') {
|
||||||
|
iframe.contentWindow.postMessage(msg, '*');
|
||||||
|
sendResponse({ success: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
args: [extensionUrl]
|
args: [extensionUrl]
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
// Small delay to ensure iframe has loaded and registered listener
|
// Small delay to ensure iframe has loaded and registered listener
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
chrome.runtime.sendMessage({
|
chrome.tabs.sendMessage(activeTab.id, {
|
||||||
type: 'START_RECORDING_FROM_POPUP',
|
type: 'START_RECORDING_FROM_POPUP',
|
||||||
payload: { language: 'ar-SA' }
|
payload: { language: 'ar-SA' }
|
||||||
});
|
});
|
||||||
|
|||||||
27
speech.js
27
speech.js
@@ -29,7 +29,7 @@ function initRecognition(language) {
|
|||||||
let interimText = '';
|
let interimText = '';
|
||||||
let finalText = '';
|
let finalText = '';
|
||||||
|
|
||||||
for (let i = event.resultIndex; i < event.results.length; i++) {
|
for (let i = 0; i < event.results.length; i++) {
|
||||||
const result = event.results[i];
|
const result = event.results[i];
|
||||||
if (result.isFinal) {
|
if (result.isFinal) {
|
||||||
finalText += result[0].transcript + ' ';
|
finalText += result[0].transcript + ' ';
|
||||||
@@ -38,10 +38,10 @@ function initRecognition(language) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.runtime.sendMessage({
|
window.parent.postMessage({
|
||||||
type: 'OFFSCREEN_RECORDING_RESULT',
|
type: 'SPEECH_RESULT',
|
||||||
payload: { interimText, finalText }
|
payload: { interimText, finalText }
|
||||||
});
|
}, '*');
|
||||||
};
|
};
|
||||||
|
|
||||||
recognition.onerror = (event) => {
|
recognition.onerror = (event) => {
|
||||||
@@ -52,10 +52,10 @@ function initRecognition(language) {
|
|||||||
stopMediaTracks();
|
stopMediaTracks();
|
||||||
}
|
}
|
||||||
|
|
||||||
chrome.runtime.sendMessage({
|
window.parent.postMessage({
|
||||||
type: 'OFFSCREEN_RECORDING_ERROR',
|
type: 'SPEECH_ERROR',
|
||||||
payload: { error: event.error }
|
payload: { error: event.error }
|
||||||
});
|
}, '*');
|
||||||
};
|
};
|
||||||
|
|
||||||
recognition.onend = () => {
|
recognition.onend = () => {
|
||||||
@@ -67,7 +67,7 @@ function initRecognition(language) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
stopMediaTracks();
|
stopMediaTracks();
|
||||||
chrome.runtime.sendMessage({ type: 'OFFSCREEN_RECORDING_END' });
|
window.parent.postMessage({ type: 'SPEECH_END' }, '*');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -75,7 +75,10 @@ function initRecognition(language) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Listen for messages broadcasted across the extension
|
// Listen for messages broadcasted across the extension
|
||||||
chrome.runtime.onMessage.addListener((message) => {
|
window.addEventListener('message', (event) => {
|
||||||
|
const message = event.data;
|
||||||
|
if (!message || !message.type) return;
|
||||||
|
|
||||||
if (message.type === 'START_RECORDING_FROM_POPUP') {
|
if (message.type === 'START_RECORDING_FROM_POPUP') {
|
||||||
const lang = message.payload?.language || 'ar-SA';
|
const lang = message.payload?.language || 'ar-SA';
|
||||||
|
|
||||||
@@ -90,16 +93,16 @@ chrome.runtime.onMessage.addListener((message) => {
|
|||||||
recognition.start();
|
recognition.start();
|
||||||
isRecording = true;
|
isRecording = true;
|
||||||
// Tell popup it started successfully
|
// Tell popup it started successfully
|
||||||
chrome.runtime.sendMessage({ type: 'OFFSCREEN_RECORDING_START_SUCCESS' });
|
window.parent.postMessage({ type: 'SPEECH_START_SUCCESS' }, '*');
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('[Speech Iframe] Failed to start:', e);
|
console.error('[Speech Iframe] Failed to start:', e);
|
||||||
chrome.runtime.sendMessage({ type: 'OFFSCREEN_RECORDING_ERROR', payload: { error: e.message } });
|
window.parent.postMessage({ type: 'SPEECH_ERROR', payload: { error: e.message } }, '*');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error('[Speech Iframe] getUserMedia failed:', err);
|
console.error('[Speech Iframe] getUserMedia failed:', err);
|
||||||
chrome.runtime.sendMessage({ type: 'OFFSCREEN_RECORDING_ERROR', payload: { error: 'not-allowed' } });
|
window.parent.postMessage({ type: 'SPEECH_ERROR', payload: { error: 'not-allowed' } }, '*');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user