-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-script.js
117 lines (99 loc) · 4.12 KB
/
content-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// CloseAssist - Content Script
console.log('Content script loaded');
// Function to inject the permission iframe
function injectPermissionIframe() {
return new Promise((resolve, reject) => {
try {
console.log('Starting permission iframe injection');
// Check if we already have an iframe
let permissionFrame = document.getElementById('tab-audio-recorder-permission-frame');
// If not, create one
if (!permissionFrame) {
console.log('Creating new permission iframe');
permissionFrame = document.createElement('iframe');
permissionFrame.id = 'tab-audio-recorder-permission-frame';
permissionFrame.style.width = '1px';
permissionFrame.style.height = '1px';
permissionFrame.style.position = 'absolute';
permissionFrame.style.top = '-100px';
permissionFrame.style.left = '-100px';
permissionFrame.style.border = 'none';
permissionFrame.style.visibility = 'hidden';
// Set source to our permission page
const frameUrl = chrome.runtime.getURL('permission-frame.html');
console.log('Setting iframe source to:', frameUrl);
permissionFrame.src = frameUrl;
// Add to document
document.body.appendChild(permissionFrame);
console.log('Permission iframe added to document');
} else {
console.log('Using existing permission iframe');
}
// Setup message listener for iframe communication
const messageListener = (event) => {
console.log('Received message from iframe:', event.data);
// Verify origin to ensure it's our iframe
if (event.source !== permissionFrame.contentWindow) {
console.log('Message not from our iframe, ignoring');
return;
}
console.log('Processing response from permission iframe');
// Remove listener to avoid memory leaks
window.removeEventListener('message', messageListener);
// Process the result
if (event.data.success) {
console.log('Permission granted in iframe');
resolve(event.data);
} else {
console.log('Permission denied in iframe:', event.data.error);
reject(new Error(event.data.error || 'Permission denied in iframe'));
}
// Remove the iframe after a short delay
setTimeout(() => {
try {
if (permissionFrame && permissionFrame.parentNode) {
permissionFrame.parentNode.removeChild(permissionFrame);
console.log('Permission iframe removed');
}
} catch (e) {
console.error('Error removing permission iframe:', e);
}
}, 1000);
};
// Add listener for iframe messages
window.addEventListener('message', messageListener);
console.log('Added message listener for iframe');
// Set a timeout to avoid hanging indefinitely
setTimeout(() => {
window.removeEventListener('message', messageListener);
reject(new Error('Permission request timed out'));
}, 30000);
} catch (error) {
console.error('Error in injectPermissionIframe:', error);
reject(error);
}
});
}
// Listen for messages from the background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log('Content script received message:', message);
if (message.action === 'requestMicrophonePermission') {
console.log('Content script starting permission request');
injectPermissionIframe()
.then(result => {
console.log('Permission request successful:', result);
sendResponse(result);
})
.catch(error => {
console.error('Permission request failed:', error);
sendResponse({
success: false,
error: error.message || 'Unknown error in content script'
});
});
// Return true to indicate we will send a response asynchronously
return true;
}
// Log any other messages received
console.log('Content script received unknown message:', message);
});