Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
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 Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /* globals getDocLink */ | 18 /* globals getDocLink */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 { | 22 { |
23 const {getMessage} = ext.i18n; | 23 const {getMessage} = ext.i18n; |
24 | 24 |
25 const dialogSubscribe = "subscribe"; | 25 const dialogSubscribe = "subscribe"; |
26 const idAcceptableAds = "acceptableAds"; | |
27 const idRecommended = "subscriptions-recommended"; | |
26 let whitelistFilter = null; | 28 let whitelistFilter = null; |
27 let promisedAcceptableAdsUrl = getAcceptableAdsUrl(); | 29 let promisedAcceptableAdsUrl = getAcceptableAdsUrl(); |
28 | 30 |
29 /* Utility functions */ | 31 /* Utility functions */ |
30 | 32 |
31 function get(selector, origin) | 33 function get(selector, origin) |
32 { | 34 { |
33 return (origin || document).querySelector(selector); | 35 return (origin || document).querySelector(selector); |
34 } | 36 } |
35 | 37 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 | 129 |
128 function setSubscription({disabled, title, url}, shouldAdd) | 130 function setSubscription({disabled, title, url}, shouldAdd) |
129 { | 131 { |
130 if (disabled) | 132 if (disabled) |
131 return; | 133 return; |
132 | 134 |
133 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => | 135 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => |
134 { | 136 { |
135 if (url == acceptableAdsUrl) | 137 if (url == acceptableAdsUrl) |
136 { | 138 { |
137 get("#acceptableAds").checked = true; | 139 get(`#${idAcceptableAds}`).checked = true; |
138 return; | 140 return; |
139 } | 141 } |
140 | 142 |
141 let listInstalled = get("#subscriptions-installed"); | 143 let listInstalled = get("#subscriptions-installed"); |
142 let installed = get(`[data-url="${url}"]`, listInstalled); | 144 let installed = get(`[data-url="${url}"]`, listInstalled); |
143 | 145 |
144 if (installed) | 146 if (installed) |
145 { | 147 { |
146 let titleElement = get("span", installed); | 148 let titleElement = get("span", installed); |
147 titleElement.textContent = title || url; | 149 titleElement.textContent = title || url; |
148 } | 150 } |
149 else if (shouldAdd) | 151 else if (shouldAdd) |
150 { | 152 { |
151 let element = create(listInstalled, "li", null, {"data-url": url}); | 153 let element = create(listInstalled, "li", null, {"data-url": url}); |
152 create(element, "span", title || url); | 154 create(element, "span", title || url); |
153 create(element, "button", null, {class: "remove"}, | 155 create(element, "button", null, {class: "remove"}, |
154 () => uninstallSubscription(url) | 156 () => uninstallSubscription(url) |
155 ); | 157 ); |
156 | 158 |
157 let recommended = get(`#subscriptions-recommended [data-url="${url}"]`); | 159 let recommended = get(`#${idRecommended} [data-url="${url}"]`); |
158 if (recommended) | 160 if (recommended) |
159 { | 161 { |
160 recommended.classList.add("installed"); | 162 recommended.classList.add("installed"); |
161 } | 163 } |
162 } | 164 } |
163 }); | 165 }); |
164 } | 166 } |
165 | 167 |
166 function removeSubscription(url) | 168 function removeSubscription(url) |
167 { | 169 { |
168 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => | 170 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => |
169 { | 171 { |
170 if (url == acceptableAdsUrl) | 172 if (url == acceptableAdsUrl) |
171 { | 173 { |
172 get("#acceptableAds").checked = false; | 174 get(`#${idAcceptableAds}`).checked = false; |
173 return; | 175 return; |
174 } | 176 } |
175 | 177 |
176 let installed = get(`#subscriptions-installed [data-url="${url}"]`); | 178 let installed = get(`#subscriptions-installed [data-url="${url}"]`); |
177 if (installed) | 179 if (installed) |
178 { | 180 { |
179 installed.parentNode.removeChild(installed); | 181 installed.parentNode.removeChild(installed); |
180 } | 182 } |
181 | 183 |
182 let recommended = get(`#subscriptions-recommended [data-url="${url}"]`); | 184 let recommended = get(`#${idRecommended} [data-url="${url}"]`); |
183 if (recommended) | 185 if (recommended) |
184 { | 186 { |
185 recommended.classList.remove("installed"); | 187 recommended.classList.remove("installed"); |
186 } | 188 } |
187 }); | 189 }); |
188 } | 190 } |
189 | 191 |
190 function setDialog(id, options) | 192 function setDialog(id, options) |
191 { | 193 { |
192 if (!id) | 194 if (!id) |
193 { | 195 { |
194 delete document.body.dataset.dialog; | 196 delete document.body.dataset.dialog; |
195 return; | 197 return; |
196 } | 198 } |
197 | 199 |
198 let fields = getAll(`#dialog-${id} input`); | 200 let fields = getAll(`#dialog-${id} input`); |
199 for (let field of fields) | 201 for (let field of fields) |
200 { | 202 { |
201 field.value = (options && field.name in options) ? options[field.name] : " "; | 203 let {name} = field; |
204 field.value = (options && name in options) ? options[name] : ""; | |
202 } | 205 } |
203 setError(id, null); | 206 setError(id, null); |
204 | 207 |
205 document.body.dataset.dialog = id; | 208 document.body.dataset.dialog = id; |
206 } | 209 } |
207 | 210 |
208 function setError(dialogId, fieldName) | 211 function setError(dialogId, fieldName) |
209 { | 212 { |
210 let dialog = get(`#dialog-${dialogId}`); | 213 let dialog = get(`#dialog-${dialogId}`); |
211 if (fieldName) | 214 if (fieldName) |
212 { | 215 { |
213 dialog.dataset.error = fieldName; | 216 dialog.dataset.error = fieldName; |
214 } | 217 } |
215 else | 218 else |
216 { | 219 { |
217 delete dialog.dataset.error; | 220 delete dialog.dataset.error; |
218 } | 221 } |
219 } | 222 } |
220 | 223 |
221 function populateLists() | 224 function populateLists() |
222 { | 225 { |
223 Promise.all([getInstalled(), getRecommendedAds()]) | 226 Promise.all([getInstalled(), getRecommendedAds()]) |
224 .then(([installed, recommended]) => | 227 .then(([installed, recommended]) => |
225 { | 228 { |
226 let listRecommended = get("#subscriptions-recommended"); | 229 let listRecommended = get(`#${idRecommended}`); |
227 for (let {title, url} of recommended) | 230 for (let {title, url} of recommended) |
228 { | 231 { |
229 create(listRecommended, "li", title, {"data-url": url}, | 232 create(listRecommended, "li", title, {"data-url": url}, |
230 (ev) => | 233 (ev) => |
231 { | 234 { |
232 if (ev.target.classList.contains("installed")) | 235 if (ev.target.classList.contains("installed")) |
233 return; | 236 return; |
234 | 237 |
235 setDialog(dialogSubscribe, {title, url}); | 238 setDialog(dialogSubscribe, {title, url}); |
236 } | 239 } |
237 ); | 240 ); |
238 } | 241 } |
239 | 242 |
240 for (let subscription of installed) | 243 for (let subscription of installed) |
241 { | 244 { |
242 if (subscription.disabled) | 245 if (subscription.disabled) |
243 continue; | 246 continue; |
244 | 247 |
245 setSubscription(subscription, true); | 248 setSubscription(subscription, true); |
246 } | 249 } |
247 }) | 250 }) |
248 .catch((err) => console.error(err)); | 251 .catch((err) => console.error(err)); |
249 } | 252 } |
250 | 253 |
251 /* Listeners */ | 254 /* Listeners */ |
252 | 255 |
253 function onChange(ev) | 256 function onChange(ev) |
254 { | 257 { |
255 if (ev.target.id != "acceptableAds") | 258 if (ev.target.id != idAcceptableAds) |
256 return; | 259 return; |
257 | 260 |
258 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => | 261 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => |
259 { | 262 { |
260 if (ev.target.checked) | 263 if (ev.target.checked) |
261 { | 264 { |
262 installSubscription(acceptableAdsUrl, null); | 265 installSubscription(acceptableAdsUrl, null); |
263 } | 266 } |
264 else | 267 else |
265 { | 268 { |
266 uninstallSubscription(acceptableAdsUrl); | 269 uninstallSubscription(acceptableAdsUrl); |
267 } | 270 } |
268 }); | 271 }); |
269 } | 272 } |
270 document.addEventListener("change", onChange); | 273 document.addEventListener("change", onChange); |
271 | 274 |
272 function toggleWhitelistFilter(toggle) | 275 function toggleWhitelistFilter(toggle) |
273 { | 276 { |
274 if (whitelistFilter) | 277 if (whitelistFilter) |
275 { | 278 { |
276 ext.backgroundPage.sendMessage( | 279 ext.backgroundPage.sendMessage( |
277 { | 280 { |
278 type: (toggle.checked) ? "filters.remove" : "filters.add", | 281 type: (toggle.checked) ? "filters.remove" : "filters.add", |
279 text: whitelistFilter | 282 text: whitelistFilter |
280 }, (errors) => | 283 }, |
284 (errors) => | |
281 { | 285 { |
282 if (errors.length < 1) | 286 if (errors.length < 1) |
283 return; | 287 return; |
284 | 288 |
285 console.error(errors); | 289 console.error(errors); |
286 toggle.checked = !toggle.checked; | 290 toggle.checked = !toggle.checked; |
287 } | 291 } |
288 ); | 292 ); |
289 } | 293 } |
290 else | 294 else |
291 { | 295 { |
292 console.error("Whitelist filter hasn't been initialized yet"); | 296 console.error("Whitelist filter hasn't been initialized yet"); |
293 } | 297 } |
294 ev.preventDefault(); | |
saroyanm
2017/07/31 15:50:40
"ev" is not defined.
Thomas Greiner
2017/08/01 12:50:03
Well spotted. It turned out to be a regression int
| |
295 } | 298 } |
296 | 299 |
297 function onClick(ev) | 300 function onClick(ev) |
298 { | 301 { |
299 switch (ev.target.dataset.action) | 302 switch (ev.target.dataset.action) |
300 { | 303 { |
301 case "close-dialog": | 304 case "close-dialog": |
302 setDialog(null); | 305 setDialog(null); |
303 break; | 306 break; |
304 case "open-dialog": | 307 case "open-dialog": |
305 setDialog(ev.target.dataset.dialog); | 308 setDialog(ev.target.dataset.dialog); |
306 break; | 309 break; |
307 case "toggle-enabled": | 310 case "toggle-enabled": |
308 toggleWhitelistFilter(ev.target); | 311 toggleWhitelistFilter(ev.target); |
312 ev.preventDefault(); | |
309 break; | 313 break; |
310 } | 314 } |
311 } | 315 } |
312 document.addEventListener("click", onClick); | 316 document.addEventListener("click", onClick); |
313 | 317 |
314 function onSubmit(ev) | 318 function onSubmit(ev) |
315 { | 319 { |
316 let fields = ev.target.elements; | 320 let fields = ev.target.elements; |
317 let title = fields.title.value; | 321 let title = fields.title.value; |
318 let url = fields.url.value; | 322 let url = fields.url.value; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
389 } | 393 } |
390 else | 394 else |
391 { | 395 { |
392 setSubscription(subscription, true); | 396 setSubscription(subscription, true); |
393 } | 397 } |
394 break; | 398 break; |
395 case "removed": | 399 case "removed": |
396 removeSubscription(subscription.url); | 400 removeSubscription(subscription.url); |
397 break; | 401 break; |
398 case "title": | 402 case "title": |
399 // We're also receiving these messages for subscriptions that are no t | 403 // We're also receiving these messages for subscriptions that are |
400 // installed so we shouldn't add those by accident | 404 // not installed so we shouldn't add those by accident |
401 setSubscription(subscription, false); | 405 setSubscription(subscription, false); |
402 break; | 406 break; |
403 } | 407 } |
404 break; | 408 break; |
405 } | 409 } |
406 } | 410 } |
407 } | 411 } |
408 ext.onMessage.addListener(onMessage); | 412 ext.onMessage.addListener(onMessage); |
409 | 413 |
410 ext.backgroundPage.sendMessage({ | 414 ext.backgroundPage.sendMessage({ |
(...skipping 23 matching lines...) Expand all Loading... | |
434 get("#dialog-subscribe [name='title']").setAttribute( | 438 get("#dialog-subscribe [name='title']").setAttribute( |
435 "placeholder", | 439 "placeholder", |
436 getMessage("mops_subscribe_title") | 440 getMessage("mops_subscribe_title") |
437 ); | 441 ); |
438 | 442 |
439 get("#dialog-subscribe [name='url']").setAttribute( | 443 get("#dialog-subscribe [name='url']").setAttribute( |
440 "placeholder", | 444 "placeholder", |
441 getMessage("mops_subscribe_url") | 445 getMessage("mops_subscribe_url") |
442 ); | 446 ); |
443 } | 447 } |
LEFT | RIGHT |