-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
156 lines (133 loc) · 4.61 KB
/
Copy pathbackground.js
File metadata and controls
156 lines (133 loc) · 4.61 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// background.js — manages audio capture mode and fallback tab muting
self.addEventListener('install', () => {});
self.addEventListener('activate', () => {});
let muteTimers = {};
let captureTabId = null;
let captureActive = false;
// ---- Offscreen document management ----
async function ensureOffscreen() {
const contexts = await chrome.runtime.getContexts({ contextTypes: ['OFFSCREEN_DOCUMENT'] });
if (contexts.length > 0) return;
await chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: ['USER_MEDIA'],
justification: 'Audio processing for profanity filter'
});
}
// ---- Start audio capture mode ----
async function startCapture(tabId) {
if (captureActive && captureTabId === tabId) return { ok: true, mode: 'capture' };
try {
await stopCapture();
const streamId = await chrome.tabCapture.getMediaStreamId({ targetTabId: tabId });
await ensureOffscreen();
const data = await chrome.storage.sync.get({ blockedWords: [] });
let words = data.blockedWords;
if (!words.length) {
// Load defaults from blockedWords.json
try {
const resp = await fetch(chrome.runtime.getURL('blockedWords.json'));
words = await resp.json();
} catch (e) {}
}
chrome.runtime.sendMessage({
action: 'offscreen-start',
streamId: streamId,
blockedWords: words
}, () => { void chrome.runtime.lastError; });
captureTabId = tabId;
captureActive = true;
// Mute the tab so user only hears our delayed audio
chrome.tabs.update(tabId, { muted: true });
return { ok: true, mode: 'capture' };
} catch (e) {
captureActive = false;
return { ok: false, error: e.message };
}
}
async function stopCapture() {
if (captureTabId) {
try { chrome.tabs.update(captureTabId, { muted: false }); } catch (e) {}
}
try {
const contexts = await chrome.runtime.getContexts({ contextTypes: ['OFFSCREEN_DOCUMENT'] });
if (contexts.length > 0) {
chrome.runtime.sendMessage({ action: 'offscreen-stop' }, () => { void chrome.runtime.lastError; });
await chrome.offscreen.closeDocument();
}
} catch (e) {}
captureActive = false;
captureTabId = null;
}
// ---- Fallback: tab muting ----
function fallbackMute(tabId, duration) {
console.log('AudioFilter BG: muting tab', tabId, 'for', duration, 'ms');
if (muteTimers[tabId]) {
clearTimeout(muteTimers[tabId].restoreId);
}
chrome.tabs.update(tabId, { muted: true }, (tab) => {
if (chrome.runtime.lastError) {
console.log('AudioFilter BG: mute FAILED:', chrome.runtime.lastError.message);
} else {
console.log('AudioFilter BG: muted OK, tab muted:', tab && tab.mutedInfo && tab.mutedInfo.muted);
}
});
const restoreId = setTimeout(() => {
chrome.tabs.update(tabId, { muted: false }, () => {
console.log('AudioFilter BG: unmuted tab', tabId);
});
delete muteTimers[tabId];
}, duration);
muteTimers[tabId] = { restoreId };
}
// ---- Message handling ----
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (!msg || !msg.action) { sendResponse({ ok: true }); return; }
// Content script requesting tab mute (fallback mode)
if (msg.action === 'muteTab') {
const tabId = sender.tab && sender.tab.id;
console.log('AudioFilter BG: received muteTab from tab', tabId);
if (!tabId) { sendResponse({ ok: false }); return; }
fallbackMute(tabId, msg.duration || 700);
sendResponse({ ok: true, mode: 'fallback' });
return;
}
// Popup requesting capture mode start
if (msg.action === 'startCapture') {
const tabId = msg.tabId;
if (!tabId) { sendResponse({ ok: false }); return; }
startCapture(tabId).then(r => sendResponse(r));
return true; // async
}
// Popup requesting capture mode stop
if (msg.action === 'stopCapture') {
stopCapture().then(() => sendResponse({ ok: true }));
return true;
}
// Offscreen reporting status
if (msg.action === 'offscreen-ready') {
sendResponse({ ok: true });
return;
}
if (msg.action === 'offscreen-error') {
// Capture failed — unmute tab and fall back
if (captureTabId) chrome.tabs.update(captureTabId, { muted: false });
captureActive = false;
sendResponse({ ok: true });
return;
}
// Status query
if (msg.action === 'getCaptureStatus') {
sendResponse({ active: captureActive, tabId: captureTabId });
return;
}
sendResponse({ ok: true });
});
// Clean up on tab close
chrome.tabs.onRemoved.addListener((tabId) => {
if (tabId === captureTabId) stopCapture();
if (muteTimers[tabId]) {
clearTimeout(muteTimers[tabId].restoreId);
delete muteTimers[tabId];
}
});