forked from joeywatts/disable-autogain-control-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
102 lines (94 loc) · 2.81 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
// Show context menu that allows enabling/disabling on a per-domain basis.
chrome.browserAction.onClicked.addListener(tab => {
const { origin } = new URL(tab.url);
chrome.permissions.contains({
origins: [origin + "/*"],
}, (hasPermission) => {
if (hasPermission) {
chrome.permissions.remove({
origins: [origin + "/*"]
}, () => chrome.tabs.reload(tab.id));
} else {
chrome.permissions.request({
origins: [origin + "/*"]
}, () => chrome.tabs.reload(tab.id));
}
});
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
injectScriptIfNecessary(tab);
});
/**
* @param {chrome.tabs.Tab} tab
*/
function injectScriptIfNecessary(tab) {
if (tab.status !== "loading" || !tab.url) {
return;
}
try {
const { origin, protocol } = new URL(tab.url);
if (protocol !== "https:" && protocol !== "http:") {
return;
}
chrome.permissions.contains({
origins: [origin + "/*"]
}, (hasPermission) => {
if (hasPermission) {
chrome.tabs.executeScript(tab.id, {
runAt: "document_start",
allFrames: true,
file: "installDisableAutogain.js",
});
}
chrome.browserAction.setTitle({
title: hasPermission
? "Disable Automatic Gain Control"
: "Enable Automatic Gain Control",
tabId: tab.id,
});
chrome.browserAction.setBadgeText({
text: hasPermission ? "On" : "",
tabId: tab.id,
});
});
} catch (e) {
console.error("Failed to inject script", e);
}
}
function showUsage() {
chrome.tabs.create({
url: chrome.extension.getURL("usage.html")
});
}
function showUpgradeNotice() {
chrome.tabs.create({
url: chrome.extension.getURL("upgradeFromV1.0.html")
});
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (typeof message === "object" && message["type"] === "enable-meet-hangouts") {
chrome.permissions.request({
origins: [
"https://meet.google.com/*",
"https://hangouts.google.com/*"
]
}, (granted) => {
sendResponse(granted);
});
return true;
}
});
chrome.contextMenus.create({
title: "Usage",
contexts: ["browser_action"],
onclick: () => {
showUsage();
}
});
chrome.runtime.onInstalled.addListener(({ reason, previousVersion }) => {
if (reason === "update" && previousVersion === "1.0") {
showUpgradeNotice();
} else if (reason === "install") {
showUsage();
}
});