Skip to content

Commit

Permalink
Add fixes for other browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Oct 27, 2024
1 parent f864718 commit d27f0c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
19 changes: 13 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ interface Note {
versions: NoteVersion[];
}

// Check if running in Chrome extension context
const isExtension = typeof chrome !== 'undefined' && chrome.runtime && chrome.runtime.sendMessage;

function App() {
const transcriber = useTranscriber();
const [notes, setNotes] = useState<Note[]>([]);
Expand Down Expand Up @@ -47,14 +50,18 @@ function App() {

loadNotes();

window.addEventListener('message', (event) => {
if (event.data.type === 'MICROPHONE_PERMISSION_GRANTED') {
setHasMicrophonePermission(true);
}
});
// Only set up Chrome extension specific listeners if in extension context
if (isExtension) {
window.addEventListener('message', (event) => {
if (event.data.type === 'MICROPHONE_PERMISSION_GRANTED') {
setHasMicrophonePermission(true);
}
});

if (chrome.runtime && chrome.runtime.sendMessage) {
chrome.runtime.sendMessage({ type: 'REQUEST_MICROPHONE_PERMISSION' });
} else {
// In browser context, assume microphone permission will be handled by browser
setHasMicrophonePermission(true);
}
}, []);

Expand Down
39 changes: 24 additions & 15 deletions src/contentScript.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
// Check if running in Chrome extension context
const isExtension = typeof chrome !== 'undefined' && chrome.runtime && chrome.runtime.getURL;

export const injectMicrophonePermissionIframe = () => {
const iframe = document.createElement("iframe");
iframe.setAttribute("hidden", "hidden");
iframe.setAttribute("id", "permissionsIFrame");
iframe.setAttribute("allow", "microphone");
iframe.src = chrome.runtime.getURL("/src/pages/permission/index.html");
document.body.appendChild(iframe);
// Only inject the iframe if we're in a Chrome extension context
if (isExtension) {
const iframe = document.createElement("iframe");
iframe.setAttribute("hidden", "hidden");
iframe.setAttribute("id", "permissionsIFrame");
iframe.setAttribute("allow", "microphone");
iframe.src = chrome.runtime.getURL("/src/pages/permission/index.html");
document.body.appendChild(iframe);
}
};

// Inject the iframe when the content script runs
injectMicrophonePermissionIframe();
// Only run extension-specific code if we're in a Chrome extension context
if (isExtension) {
// Inject the iframe when the content script runs
injectMicrophonePermissionIframe();

// Listen for messages from the iframe
window.addEventListener('message', (event) => {
if (event.data.type === 'MICROPHONE_PERMISSION_GRANTED') {
console.log('Microphone permission granted');
// You can add any additional logic here when permission is granted
}
});
// Listen for messages from the iframe
window.addEventListener('message', (event) => {
if (event.data.type === 'MICROPHONE_PERMISSION_GRANTED') {
console.log('Microphone permission granted');
// You can add any additional logic here when permission is granted
}
});
}

0 comments on commit d27f0c3

Please sign in to comment.