-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
118 lines (92 loc) · 3.79 KB
/
background.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
118
let googleMeetTabs = {} // Tracks the state of each Google Meet tab
let isAnyAudioMuted = false
let isAnyVideoMuted = false
// Update the global mute state and broadcast it to all tabs
function updateGlobalMuteState() {
isAnyAudioMuted = Object.values(googleMeetTabs).some(tab => tab.audioMuted);
isAnyVideoMuted = Object.values(googleMeetTabs).some(tab => tab.videoMuted);
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, {
action: "updateOverlay",
audioMuted: isAnyAudioMuted,
videoMuted: isAnyVideoMuted,
visible: Object.keys(googleMeetTabs).length > 0 // Overlay is visible if any Google Meet tab is open
});
});
});
}
// Listen for tab updates to track Google Meet sessions
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (tab.url && tab.url.startsWith("https://meet.google.com/")) {
// Mark the tab as a Google Meet session
googleMeetTabs[tabId] = { audioMuted: false, videoMuted: false };
} else if (googleMeetTabs[tabId]) {
// Tab was a Google Meet session but has navigated away
delete googleMeetTabs[tabId]
}
updateGlobalMuteState();
});
// Respond to (State request) from other tabs or (Update State) from Google Meet
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.from === "overlay") {
if (request.query === "getSuperMuteState") {
sendResponse({
action: "updateOverlay",
audioMuted: isAnyAudioMuted,
videoMuted: isAnyVideoMuted,
visible: Object.keys(googleMeetTabs).length > 0
})
return true;
}
}
if (request.from === "content" && sender.tab && googleMeetTabs[sender.tab.id]) {
// State update from a Google Meet tab
const { audioMuted, videoMuted } = request.message;
googleMeetTabs[sender.tab.id] = { audioMuted, videoMuted };
updateGlobalMuteState(); // Reflect this change in all tabs
}
if (request.action === "updatePosition") {
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, { action: "updatePosition", position: request.position });
});
});
}
});
// Handle Google Meet tab closures
chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
if (googleMeetTabs[tabId]) {
delete googleMeetTabs[tabId]; // Remove the closed Google Meet tab from tracking
updateGlobalMuteState(); // Reflect this change in all tabs
}
});
// Inject overlay.js into all tabs on extension installation or update
chrome.runtime.onInstalled.addListener(() => {
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['overlay.js']
}).catch(err => console.log('Error injecting overlay script:', err));;
});
});
});
// Listens for Mic/Video toggle or endCall toggle
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "toggleMic" || request.action === "toggleVideo") {
chrome.tabs.query({ url: "*://meet.google.com/*" }, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, request);
});
});
}
if (request.action === "closeAllGoogleMeetTabs") {
Object.keys(googleMeetTabs).forEach((tabId) => {
chrome.tabs.remove(parseInt(tabId));
});
// Clear the googleMeetTabs list after closing the tabs
googleMeetTabs = {};
updateGlobalMuteState();
}
});