-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
158 lines (145 loc) · 5.04 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
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
157
158
chrome.runtime.onInstalled.addListener(function (details) {
// console.log("Google Meet PTT Installed");
switch (details.reason) {
case 'install':
let previousMicState = "ON";
let notifications = true;
chrome.storage.local.set({previousMicState});
chrome.storage.local.set({notifications});
break;
// case 'update':
// case 'chrome_update':
// case 'shared_module_update':
default:
break;
}
});
const bringChromeTabToForeground = (tab) => {
chrome.tabs.sendMessage(tab.id, { checkActiveMeetCall: true }, (response) => {
if(response.shouldSwitchToThisTab){
chrome.tabs.update(tab.id, {selected: true});
chrome.windows.update(tab.windowId,{
focused: true
});
}
});
};
const toggleMicrophone = function () {
chrome.tabs.query({ url: "https://meet.google.com/*" }, function (tabs) {
if (tabs && tabs.length > 0) {
tabs.forEach((tab) => {
bringChromeTabToForeground(tab);
chrome.tabs.sendMessage(tab.id, { toggleMic: true });
});
} else {
chrome.action.setIcon({ path: `/icons/mic-default.png` });
}
});
};
const redirectToMeetHome = () => {
chrome.tabs.query({ url: "https://meet.google.com/*" }, function (tabs) {
if (tabs && tabs.length > 0) {
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, { returnHome: true });
});
}
});
}
const switchToActiveTab = () => {
chrome.tabs.query({ url: "https://meet.google.com/*" }, function (tabs) {
tabs.forEach((tab) => {
bringChromeTabToForeground(tab);
});
});
}
const toogleNotifications = () => {
chrome.storage.local.get('notifications', function (val) {
let currentEnabled = val.notifications === true || val.notifications === undefined;
let notificationText = currentEnabled ? "Disabled" : "Enabled";
chrome.storage.local.set({notifications: !currentEnabled });
chrome.notifications.create({
title: "Mute for Google Meet™",
type: "basic",
iconUrl: `icons/meet_assist_default.png`,
message: `${notificationText} mic notifications`,
silent: true,
});
});
}
chrome.commands.onCommand.addListener(function (command) {
//console.log('Command', command);
let notifications;
switch (command) {
case 'toggle-mic':
toggleMicrophone();
break;
case 'return-home':
redirectToMeetHome();
break;
case 'switch-to-active-tab':
switchToActiveTab();
break;
case 'toogle-notifications':
toogleNotifications();
break;
default:
break;
}
});
// chrome.tabs.onUpdated is called when a tab is updated in chrome
// For meet, when the user joins a call, chrome.tabs.onUpdated is called with the changeInfo as
// { 'audible': 'false' }
// Using that as a trigger send a message to content script, to start listening for click events
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
console.log("Tab updated", tabId, changeInfo, tab);
if ((changeInfo.status === "complete" && tab.url !== "https://meet.google.com/") || changeInfo.audible === false) {
chrome.tabs.query({ url: "https://meet.google.com/*" }, function (tabs) {
if (tabs && tabs.length > 0) {
const firstMeetTab = tabs[0];
// console.log(firstMeetTab);
tabs.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, { listenToMicClick: true }, (response) => {
if (response && response.currentMicStatus) {
chrome.action.setIcon({ path: `/icons/mic-${response.currentMicStatus}.png` });
} else if (!response) {
// console.log("No response - onUpdated", chrome.runtime.lastError);
}
});
});
}
});
}
});
chrome.runtime.onMessage.addListener(function (request, sender) {
// console.log("Request", sender.tab ? "from a content script:" + sender.tab.url : "from the extension", request);
chrome.storage.local.get(['previousMicState', 'notifications'], function (values) {
if (request.micMuted && request.micMuted !== values.previousMicState) {
if (values.notifications) {
chrome.notifications.create({
title: "Mute for Google Meet™",
type: "basic",
iconUrl: chrome.runtime.getURL(`icons/mic-${request.micMuted}.png`), //.svg dont work for me :(
message: `Mic is ${request.micMuted}`,
silent: true,
});
// console.log("Creating notification");
}
//update extension icon
chrome.action.setIcon({ path: `/icons/mic-${request.micMuted}.png` });
} else {
// console.log("No need of creating notification");
}
let previousMicState = request.micMuted;
chrome.storage.local.set({previousMicState});
});
});
chrome.action.onClicked.addListener(function callback() {
toggleMicrophone();
});
chrome.tabs.onRemoved.addListener(function callback(tabId, removeInfo) {
chrome.tabs.query({ url: "https://meet.google.com/*" }, function (tabs) {
if (!tabs || tabs.length == 0) {
chrome.action.setIcon({ path: `/icons/mic-default.png` });
}
});
});