Changeset View
Standalone View
extension/action_popup.js
Show All 9 Lines | 1 | /* | |||
---|---|---|---|---|---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | 12 | GNU General Public License for more details. | ||
13 | 13 | | |||
14 | You should have received a copy of the GNU General Public License | 14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | 16 | */ | ||
17 | 17 | | |||
18 | class TabUtils { | ||||
19 | // Gets the currently viewed tab | ||||
20 | static getCurrentTab() { | ||||
fvogt: `getCurrentTabUrl()` | |||||
The class is named TabUtils but I can change the method if you want.. broulik: The class is named `TabUtils` but I can change the method if you want.. | |||||
21 | return new Promise((resolve, reject) => { | ||||
22 | chrome.tabs.query({ | ||||
23 | active: true, | ||||
24 | currentWindow: true | ||||
25 | }, (tabs) => { | ||||
26 | const error = chrome.runtime.lastError; | ||||
27 | if (error) { | ||||
28 | return reject(error.message); | ||||
29 | } | ||||
30 | | ||||
31 | const tab = tabs[0]; | ||||
32 | if (!tab) { | ||||
33 | return reject("NO_TAB"); | ||||
34 | } | ||||
35 | | ||||
36 | resolve(tab); | ||||
37 | }); | ||||
38 | }); | ||||
39 | } | ||||
40 | | ||||
41 | // Gets the URLs of the currently viewed tab including all of its iframes | ||||
42 | static getCurrentTabFramesUrls() { | ||||
Currently the function name implies that it returns all tabs, so maybe rename to getCurrentTabFramesUrls() (or better)? fvogt: Currently the function name implies that it returns all tabs, so maybe rename to… | |||||
43 | return new Promise((resolve, reject) => { | ||||
44 | TabUtils.getCurrentTab().then((tab) => { | ||||
45 | chrome.tabs.executeScript({ | ||||
46 | allFrames: true, // so we also catch iframe videos | ||||
Maybe runAt: "document_start" to speed it up a bit? I'm not sure about the implications. fvogt: Maybe `runAt: "document_start"` to speed it up a bit? I'm not sure about the implications. | |||||
Don't think this will change much, given it is only executed when you click the toolbar button, at which point the page is probably already loaded, but I'll give it a try broulik: Don't think this will change much, given it is only executed when you click the toolbar button… | |||||
47 | code: `window.location.href`, | ||||
48 | runAt: "document_start" | ||||
49 | }, (result) => { | ||||
50 | const error = chrome.runtime.lastError; | ||||
51 | if (error) { | ||||
52 | return reject(error.message); | ||||
53 | } | ||||
54 | | ||||
55 | resolve(result); | ||||
56 | }); | ||||
57 | }); | ||||
58 | }); | ||||
59 | } | ||||
60 | }; | ||||
61 | | ||||
62 | class MPrisBlocker { | ||||
fvogt: Whitespace? | |||||
broulik: Intentional, for a bit of visual grouping | |||||
63 | getAllowed() { | ||||
64 | return new Promise((resolve, reject) => { | ||||
65 | Promise.all([ | ||||
66 | SettingsUtils.get(), | ||||
67 | TabUtils.getCurrentTabFramesUrls() | ||||
68 | ]).then((result) => { | ||||
69 | | ||||
70 | const settings = result[0]; | ||||
71 | const currentUrls = result[1]; | ||||
72 | | ||||
73 | const mprisSettings = settings.mpris; | ||||
74 | if (!mprisSettings.enabled) { | ||||
75 | return reject("MPRIS_DISABLED"); | ||||
76 | } | ||||
77 | | ||||
78 | if (!currentUrls) { // can this happen? | ||||
79 | return reject("NO_URLS"); | ||||
80 | } | ||||
81 | | ||||
82 | const origins = currentUrls.map((url) => { | ||||
83 | try { | ||||
84 | return new URL(url).origin; | ||||
85 | } catch (e) { | ||||
86 | console.warn("Invalid url", url); | ||||
87 | return ""; | ||||
88 | } | ||||
89 | }).filter((origin) => { | ||||
90 | return !!origin; | ||||
91 | }); | ||||
92 | | ||||
93 | if (origins.length === 0) { | ||||
94 | return reject("NO_ORIGINS"); | ||||
95 | } | ||||
96 | | ||||
97 | const uniqueOrigins = [...new Set(origins)]; | ||||
98 | | ||||
99 | const websiteSettings = mprisSettings.websiteSettings || {}; | ||||
100 | | ||||
101 | let response = { | ||||
102 | origins: {}, | ||||
103 | mprisSettings | ||||
104 | }; | ||||
105 | | ||||
106 | for (const origin of uniqueOrigins) { | ||||
107 | let allowed = true; | ||||
108 | if (typeof MPRIS_WEBSITE_SETTINGS[origin] === "boolean") { | ||||
109 | allowed = MPRIS_WEBSITE_SETTINGS[origin]; | ||||
110 | } | ||||
111 | if (typeof websiteSettings[origin] === "boolean") { | ||||
112 | allowed = websiteSettings[origin]; | ||||
113 | } | ||||
114 | | ||||
115 | response.origins[origin] = allowed; | ||||
116 | } | ||||
117 | | ||||
118 | resolve(response); | ||||
Currently calling set(domain, false); twice has a different result from set(domain, false); once, so maybe split into whitelist(domain) and blacklist(domain))? fvogt: Currently calling `set(domain, false);` twice has a different result from `set(domain, false);`… | |||||
Imho the caller shouldn't have to care about whether it needs to be whitelisted or removed from the blacklist. The whitelist is only so that the user can opt-in to websites which we by default blacklist. broulik: Imho the caller shouldn't have to care about whether it needs to be whitelisted or removed from… | |||||
119 | | ||||
120 | }, reject); | ||||
fvogt: Whitespace? | |||||
121 | }); | ||||
122 | } | ||||
123 | | ||||
124 | setAllowed(origin, allowed) { | ||||
125 | return SettingsUtils.get().then((settings) => { | ||||
126 | const mprisSettings = settings.mpris; | ||||
127 | if (!mprisSettings.enabled) { | ||||
128 | return reject("MPRIS_DISABLED"); | ||||
129 | } | ||||
130 | | ||||
131 | let websiteSettings = mprisSettings.websiteSettings || {}; | ||||
132 | | ||||
133 | let implicitAllowed = true; | ||||
134 | if (typeof MPRIS_WEBSITE_SETTINGS[origin] === "boolean") { | ||||
135 | implicitAllowed = MPRIS_WEBSITE_SETTINGS[origin]; | ||||
136 | } | ||||
137 | | ||||
138 | if (allowed !== implicitAllowed) { | ||||
139 | websiteSettings[origin] = allowed; | ||||
140 | } else { | ||||
141 | delete websiteSettings[origin]; | ||||
142 | } | ||||
143 | | ||||
144 | mprisSettings.websiteSettings = websiteSettings; | ||||
145 | | ||||
146 | return SettingsUtils.set({ | ||||
147 | mpris: mprisSettings | ||||
148 | }); | ||||
149 | }); | ||||
150 | } | ||||
151 | }; | ||||
152 | | ||||
18 | document.addEventListener("DOMContentLoaded", () => { | 153 | document.addEventListener("DOMContentLoaded", () => { | ||
19 | 154 | | |||
20 | sendMessage("browserAction", "getStatus").then((status) => { | 155 | sendMessage("browserAction", "getStatus").then((status) => { | ||
21 | 156 | | |||
22 | switch (status.portStatus) { | 157 | switch (status.portStatus) { | ||
23 | case "UNSUPPORTED_OS": | 158 | case "UNSUPPORTED_OS": | ||
24 | document.getElementById("unsupported_os_error").classList.remove("hidden"); | 159 | document.getElementById("unsupported_os_error").classList.remove("hidden"); | ||
25 | break; | 160 | break; | ||
26 | 161 | | |||
27 | case "STARTUP_FAILED": | 162 | case "STARTUP_FAILED": | ||
28 | document.getElementById("startup_error").classList.remove("hidden"); | 163 | document.getElementById("startup_error").classList.remove("hidden"); | ||
29 | break; | 164 | break; | ||
30 | 165 | | |||
31 | default: | 166 | default: | ||
32 | document.getElementById("main").classList.remove("hidden"); | 167 | document.getElementById("main").classList.remove("hidden"); | ||
33 | 168 | | |||
34 | let errorText = status.portLastErrorMessage; | 169 | let errorText = status.portLastErrorMessage; | ||
35 | if (errorText === "UNKNOWN") { | 170 | if (errorText === "UNKNOWN") { | ||
36 | errorText = chrome.i18n.getMessage("general_error_unknown"); | 171 | errorText = chrome.i18n.getMessage("general_error_unknown"); | ||
37 | } | 172 | } | ||
38 | 173 | | |||
39 | if (errorText) { | 174 | if (errorText) { | ||
40 | document.getElementById("runtime_error_text").innerText = errorText; | 175 | document.getElementById("runtime_error_text").innerText = errorText; | ||
41 | document.getElementById("runtime_error").classList.remove("hidden"); | 176 | document.getElementById("runtime_error").classList.remove("hidden"); | ||
177 | | ||||
178 | // There's some content, hide dummy placeholder | ||||
179 | document.getElementById("dummy-main").classList.add("hidden"); | ||||
42 | } | 180 | } | ||
43 | 181 | | |||
44 | break; | 182 | break; | ||
45 | } | 183 | } | ||
46 | 184 | | |||
47 | // HACK so the extension can tell we closed, see "browserAction" "ready" callback in extension.js | 185 | // HACK so the extension can tell we closed, see "browserAction" "ready" callback in extension.js | ||
48 | chrome.runtime.onConnect.addListener((port) => { | 186 | chrome.runtime.onConnect.addListener((port) => { | ||
49 | if (port.name !== "browserActionPort") { | 187 | if (port.name !== "browserActionPort") { | ||
50 | return; | 188 | return; | ||
51 | } | 189 | } | ||
52 | 190 | | |||
53 | // do we need to do something with the port here? | 191 | // do we need to do something with the port here? | ||
54 | }); | 192 | }); | ||
55 | sendMessage("browserAction", "ready"); | 193 | sendMessage("browserAction", "ready"); | ||
56 | }); | 194 | }); | ||
57 | 195 | | |||
196 | // MPris blocker checkboxes | ||||
197 | const blocker = new MPrisBlocker(); | ||||
198 | blocker.getAllowed().then((result) => { | ||||
199 | const origins = result.origins; | ||||
200 | | ||||
201 | if (Object.entries(origins).length === 0) { // "isEmpty" | ||||
202 | return; | ||||
203 | } | ||||
204 | | ||||
205 | // To keep media controls setting from always showing up, only show them, if: | ||||
206 | // - There is actually a player anywhere on this tab | ||||
207 | // or, since when mpris is disabled, there are never any players | ||||
208 | // - when media controls are disabled for any origin on this tab | ||||
209 | new Promise((resolve, reject) => { | ||||
210 | for (let origin in origins) { | ||||
211 | if (origins[origin] === false) { | ||||
212 | return resolve("HAS_BLOCKED"); | ||||
213 | } | ||||
214 | } | ||||
215 | | ||||
216 | TabUtils.getCurrentTab().then((tab) => { | ||||
217 | return sendMessage("mpris", "hasTabPlayer", { | ||||
fvogt: Does this also hit all the frames inside? | |||||
Effectively, yes. It asks the extension.js whether it knows any players on the given tab, which will include all frames, see the actual implementation of "hasTabPlayer" which checks playerIds containing all of them. broulik: Effectively, yes. It asks the extension.js whether it knows any players on the given tab, which… | |||||
218 | tabId: tab.id | ||||
219 | }); | ||||
220 | }).then((playerIds) => { | ||||
221 | if (playerIds.length > 0) { | ||||
222 | return resolve("HAS_PLAYER"); | ||||
223 | } | ||||
224 | | ||||
225 | reject("NO_PLAYER_NO_BLOCKED"); | ||||
226 | }); | ||||
227 | }).then(() => { | ||||
228 | // There's some content, hide dummy placeholder | ||||
229 | document.getElementById("dummy-main").classList.add("hidden"); | ||||
230 | | ||||
231 | let blacklistInfoElement = document.querySelector(".mpris-blacklist-info"); | ||||
232 | blacklistInfoElement.classList.remove("hidden"); | ||||
233 | | ||||
234 | let originsListElement = blacklistInfoElement.querySelector("ul.mpris-blacklist-origins"); | ||||
235 | | ||||
236 | for (const origin in origins) { | ||||
237 | const originAllowed = origins[origin]; | ||||
238 | | ||||
239 | let blockListElement = document.createElement("li"); | ||||
240 | | ||||
241 | let labelElement = document.createElement("label"); | ||||
242 | labelElement.innerText = origin; | ||||
243 | | ||||
244 | let checkboxElement = document.createElement("input"); | ||||
245 | checkboxElement.type = "checkbox"; | ||||
246 | checkboxElement.checked = (originAllowed === true); | ||||
247 | checkboxElement.addEventListener("click", (e) => { | ||||
248 | // Let us handle (un)checking the checkbox when setAllowed succeeds | ||||
249 | e.preventDefault(); | ||||
250 | | ||||
251 | const allowed = checkboxElement.checked; | ||||
252 | blocker.setAllowed(origin, allowed).then(() => { | ||||
253 | checkboxElement.checked = allowed; | ||||
254 | }, (err) => { | ||||
255 | console.warn("Failed to change media controls settings:", err); | ||||
256 | }); | ||||
257 | }); | ||||
258 | | ||||
259 | labelElement.insertBefore(checkboxElement, labelElement.firstChild); | ||||
260 | | ||||
261 | blockListElement.appendChild(labelElement); | ||||
262 | | ||||
263 | originsListElement.appendChild(blockListElement); | ||||
264 | } | ||||
265 | }, (err) => { | ||||
266 | console.log("Not showing media controls settings because", err); | ||||
267 | }); | ||||
268 | }, (err) => { | ||||
269 | console.warn("Failed to check for whether media controls are blocked", err); | ||||
270 | }); | ||||
58 | }); | 271 | }); |
getCurrentTabUrl()