-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
164 lines (136 loc) · 4.69 KB
/
main.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
159
160
161
162
163
164
"use strict";
const _ = browser.i18n.getMessage;
let menusCreated = false;
async function createMenus () {
const { options } = await browser.storage.sync.get("options");
if (!options || menusCreated) {
return;
}
browser.menus.create({
id: "copyExtensionMutedState"
, title: _("copyExtensionMutedStateTitle")
, type: "checkbox"
, checked: options.copyExtensionMutedState
, contexts: [ "tab", "tools_menu" ]
});
browser.menus.create({
id: "linkMutedState"
, title: _("linkMutedStateTitle")
, type: "checkbox"
, checked: options.linkMutedState
, contexts: [ "tab", "tools_menu" ]
});
browser.menus.create({
id: "linkMutedStateRemute"
, title: _("linkMutedStateRemuteTitle")
, type: "checkbox"
, checked: options.linkMutedStateRemute
, contexts: [ "tab", "tools_menu" ]
});
menusCreated = true;
browser.menus.onClicked.addListener((info, tab) => {
options[info.menuItemId] = info.checked;
browser.storage.sync.set({ options });
});
}
createMenus();
const defaultOptions = {
copyExtensionMutedState: true
, linkMutedState: false
, linkMutedStateRemute: false
};
browser.runtime.onInstalled.addListener(async details => {
switch (details.reason) {
// Set default options
case "install": {
await browser.storage.sync.set({
options: defaultOptions
});
break;
};
// Set newly added options
case "update": {
const { options: existingOptions }
= await browser.storage.sync.get("options");
const newOptions = {};
// Find options not already in storage
for (const [ key, val ] of Object.entries(defaultOptions)) {
if (!existingOptions.hasOwnProperty(key)) {
newOptions[key] = val;
}
}
// Update storage with default values of new options
await browser.storage.sync.set({
options: {
...existingOptions
, ...newOptions
}
});
break;
};
}
createMenus();
});
browser.webNavigation.onCreatedNavigationTarget.addListener(async details => {
const { options } = await browser.storage.sync.get("options");
const openerTab = await browser.tabs.get(details.sourceTabId);
const { mutedInfo } = openerTab;
if (!mutedInfo.muted) {
return;
}
switch (mutedInfo.reason) {
case "extension": {
if (!options.copyExtensionMutedState) {
break;
}
};
case "user": {
// Set muted
browser.tabs.update(details.tabId, {
muted: true
});
/**
* Update muted state of descendant tab if opener tab is
* muted/unmuted.
*/
browser.tabs.onUpdated.addListener(
async function onUpdated (tabId, changeInfo) {
const { options } = await browser.storage.sync.get("options");
switch (changeInfo.mutedInfo.reason) {
case "extension": {
if (!options.copyExtensionMutedState) {
break;
}
};
case "user": {
if (options.linkMutedState) {
try {
/**
* If user has modified descendant tab's muted
* state, don't update it.
*/
const tab = await browser.tabs.get(details.tabId);
if (tab.mutedInfo.reason === "extension") {
await browser.tabs.update(tab.id, {
muted: changeInfo.mutedInfo.muted
});
}
} catch (err) {}
// Disable after unmuting descendant tabs
if (!options.linkMutedStateRemute) {
browser.tabs.onUpdated.removeListener(
onUpdated);
}
}
break;
};
}
}, {
// Filters
tabId: openerTab.id
, properties: [ "mutedInfo" ]
});
break;
};
}
});