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 URL of the currently viewed tab | ||||
20 | static getCurrentUrl() { | ||||
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) { // can this happen? | ||||
33 | return reject("NO_TAB"); | ||||
34 | } | ||||
35 | | ||||
36 | resolve(tab.url); | ||||
37 | }); | ||||
38 | }); | ||||
39 | } | ||||
40 | | ||||
41 | // Gets the URLs of the currently viewed tab including all of its iframes | ||||
42 | static getCurrentUrls() { | ||||
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 | chrome.tabs.executeScript({ | ||||
45 | allFrames: true, // so we also catch iframe videos | ||||
46 | code: `window.location.href` | ||||
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 | }, (result) => { | ||||
48 | const error = chrome.runtime.lastError; | ||||
49 | if (error) { | ||||
50 | return reject(error.message); | ||||
51 | } | ||||
52 | | ||||
53 | resolve(result); | ||||
54 | }); | ||||
55 | }); | ||||
56 | } | ||||
57 | }; | ||||
58 | | ||||
59 | class MPrisBlocker { | ||||
60 | get() { | ||||
61 | return new Promise((resolve, reject) => { | ||||
62 | | ||||
fvogt: Whitespace? | |||||
broulik: Intentional, for a bit of visual grouping | |||||
63 | Promise.all([ | ||||
64 | SettingsUtils.get(), | ||||
65 | TabUtils.getCurrentUrls() | ||||
66 | ]).then((result) => { | ||||
67 | | ||||
68 | const settings = result[0]; | ||||
69 | const currentUrls = result[1]; | ||||
70 | | ||||
71 | const mprisSettings = settings.mpris; | ||||
72 | if (!mprisSettings.enabled) { | ||||
73 | return reject("MPRIS_DISABLED"); | ||||
74 | } | ||||
75 | | ||||
76 | if (!currentUrls) { // can this happen? | ||||
77 | return reject("NO_URLS"); | ||||
78 | } | ||||
79 | | ||||
80 | const domains = currentUrls.map((url) => { | ||||
81 | try { | ||||
82 | return new URL(url).hostname; | ||||
83 | } catch (e) { | ||||
84 | console.warn("Invalid url", url); | ||||
85 | return ""; | ||||
86 | } | ||||
87 | }).filter((domain) => { | ||||
88 | return !!domain; | ||||
89 | }); | ||||
90 | | ||||
91 | if (domains.length === 0) { | ||||
92 | return reject("NO_DOMAINS"); | ||||
93 | } | ||||
94 | | ||||
95 | const uniqueDomains = [...new Set(domains)]; | ||||
96 | | ||||
97 | const whitelist = mprisSettings.whitelistedDomains || []; | ||||
98 | const blacklist = mprisSettings.blacklistedDomains || []; | ||||
99 | | ||||
100 | let response = { | ||||
101 | domains: {}, | ||||
102 | mprisSettings | ||||
103 | }; | ||||
104 | | ||||
105 | for (const domain of uniqueDomains) { | ||||
106 | const blocked = (blacklist.includes(domain) || (BLACKLISTED_MPRIS_DOMAINS.includes(domain) && !whitelist.includes(domain))); | ||||
107 | | ||||
108 | response.domains[domain] = {blocked}; | ||||
109 | } | ||||
110 | | ||||
111 | resolve(response); | ||||
112 | | ||||
113 | }, reject); | ||||
114 | | ||||
115 | }); | ||||
116 | } | ||||
117 | | ||||
118 | set(domain, block) { | ||||
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 | return this.get().then((blockInfo) => { | ||||
fvogt: Whitespace? | |||||
121 | | ||||
122 | let whitelist = blockInfo.mprisSettings.whitelistedDomains; | ||||
123 | let blacklist = blockInfo.mprisSettings.blacklistedDomains; | ||||
124 | | ||||
125 | if (block) { | ||||
126 | const whitelistIdx = whitelist.indexOf(domain); | ||||
127 | if (whitelistIdx > -1) { | ||||
128 | whitelist.splice(whitelistIdx, 1); | ||||
129 | } else if (!blacklist.includes(domain)) { | ||||
130 | blacklist.push(domain); | ||||
131 | } | ||||
132 | } else { | ||||
133 | if (BLACKLISTED_MPRIS_DOMAINS.includes(domain)) { | ||||
134 | whitelist.push(domain); | ||||
135 | } else { | ||||
136 | const blacklistIdx = blacklist.indexOf(domain); | ||||
137 | if (blacklistIdx > -1) { | ||||
138 | blacklist.splice(blacklistIdx, 1); | ||||
139 | } | ||||
140 | } | ||||
141 | } | ||||
142 | | ||||
143 | blockInfo.mprisSettings.whitelistedDomains = whitelist; | ||||
144 | blockInfo.mprisSettings.blacklistedDomains = blacklist; | ||||
145 | | ||||
146 | return { | ||||
147 | mprisSettings: blockInfo.mprisSettings | ||||
148 | }; | ||||
149 | | ||||
150 | }).then((result) => { | ||||
151 | | ||||
152 | return SettingsUtils.set({ | ||||
153 | mpris: result.mprisSettings | ||||
154 | }); | ||||
155 | | ||||
156 | }); | ||||
157 | | ||||
158 | } | ||||
159 | }; | ||||
160 | | ||||
18 | document.addEventListener("DOMContentLoaded", () => { | 161 | document.addEventListener("DOMContentLoaded", () => { | ||
19 | 162 | | |||
20 | sendMessage("browserAction", "getStatus").then((status) => { | 163 | sendMessage("browserAction", "getStatus").then((status) => { | ||
21 | 164 | | |||
22 | switch (status.portStatus) { | 165 | switch (status.portStatus) { | ||
23 | case "UNSUPPORTED_OS": | 166 | case "UNSUPPORTED_OS": | ||
24 | document.getElementById("unsupported_os_error").classList.remove("hidden"); | 167 | document.getElementById("unsupported_os_error").classList.remove("hidden"); | ||
25 | break; | 168 | break; | ||
Show All 24 Lines | 192 | if (port.name !== "browserActionPort") { | |||
50 | return; | 193 | return; | ||
51 | } | 194 | } | ||
52 | 195 | | |||
53 | // do we need to do something with the port here? | 196 | // do we need to do something with the port here? | ||
54 | }); | 197 | }); | ||
55 | sendMessage("browserAction", "ready"); | 198 | sendMessage("browserAction", "ready"); | ||
56 | }); | 199 | }); | ||
57 | 200 | | |||
201 | // MPris blocker checkboxes | ||||
202 | const blocker = new MPrisBlocker(); | ||||
203 | blocker.get().then((result) => { | ||||
204 | | ||||
205 | const domains = result.domains; | ||||
206 | | ||||
207 | if (Object.entries(domains).length === 0) { // "isEmpty" | ||||
208 | return; | ||||
209 | } | ||||
210 | | ||||
211 | let blacklistInfoElement = document.querySelector(".mpris-blacklist-info"); | ||||
212 | blacklistInfoElement.classList.remove("hidden"); | ||||
213 | | ||||
214 | let domainsListElement = blacklistInfoElement.querySelector("ul.mpris-blacklist-domains"); | ||||
215 | | ||||
216 | for (const domain in domains) { | ||||
217 | const domainSettings = domains[domain]; | ||||
218 | | ||||
219 | let blockListElement = document.createElement("li"); | ||||
220 | | ||||
221 | let labelElement = document.createElement("label"); | ||||
222 | labelElement.innerText = domain; | ||||
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… | |||||
223 | | ||||
224 | let checkboxElement = document.createElement("input"); | ||||
225 | checkboxElement.type = "checkbox"; | ||||
226 | checkboxElement.checked = !domainSettings.blocked; | ||||
227 | checkboxElement.addEventListener("click", (e) => { | ||||
228 | blocker.set(domain, !checkboxElement.checked); | ||||
229 | }); | ||||
230 | | ||||
231 | labelElement.insertBefore(checkboxElement, labelElement.firstChild); | ||||
232 | | ||||
233 | blockListElement.appendChild(labelElement); | ||||
234 | | ||||
235 | domainsListElement.appendChild(blockListElement); | ||||
236 | } | ||||
237 | | ||||
238 | }, (err) => { | ||||
239 | console.warn("Failed to check for whether MPRIS is blocked", err); | ||||
240 | }); | ||||
241 | | ||||
58 | }); | 242 | }); |
getCurrentTabUrl()