-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
79 lines (55 loc) · 2.42 KB
/
content.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
console.log('Content script loaded on Google Meet');
function sendMessageToBackgroundWorker(message) {
chrome.runtime.sendMessage({ from: "content", message })
}
function attachObserver(button) {
let isAudioMuted = false;
let isVideoMuted = false;
const observer = new MutationObserver(() => {
const audioButton = document.querySelector(buttonSelectors.audio);
const videoButton = document.querySelector(buttonSelectors.video);
if (audioButton) {
isAudioMuted = audioButton.getAttribute('data-is-muted') === 'true';
}
if (videoButton) {
isVideoMuted = videoButton.getAttribute('data-is-muted') === 'true';
}
sendMessageToBackgroundWorker({ audioMuted: isAudioMuted, videoMuted: isVideoMuted })
});
observer.observe(button, { attributes: true, subtree: true, childList: true });
}
const buttonSelectors = {
audio: `div[aria-label*="microphone"][data-is-muted], button[aria-label*="microphone"][data-is-muted]`,
video: `div[aria-label*="camera"][data-is-muted], button[aria-label*="camera"][data-is-muted]`
};
// Function to check nodes for audio or video buttons and attach obervers
function checkAndAttachObservers() {
const audioButton = document.querySelector(buttonSelectors.audio)
const videoButton = document.querySelector(buttonSelectors.video)
if (audioButton && !audioButton.hasAttribute('supermute-observed')) {
attachObserver(audioButton, 'audio')
audioButton.setAttribute('supermute-observed', 'true');
}
if (videoButton && !videoButton.hasAttribute('supermute-observed')) {
attachObserver(videoButton, 'video')
videoButton.setAttribute('supermute-observed', 'true');
}
}
function startDOMObserver() {
const observer = new MutationObserver(() => {
checkAndAttachObservers();
});
// Observe changes in the entire document
observer.observe(document.body, { subtree: true, childList: true });
}
startDOMObserver();
// Toggle mute/unmute
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "toggleMic") {
const micButton = document.querySelector(buttonSelectors.audio);
if (micButton) micButton.click();
} else if (request.action === "toggleVideo") {
const videoButton = document.querySelector(buttonSelectors.video);
if (videoButton) videoButton.click();
}
});