| 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 |
|
saroyanm
2017/07/18 12:46:28
I'll suggest to encapsulate this file with own blo
Thomas Greiner
2017/07/18 17:42:32
Done.
| |
| 22 const {getMessage} = ext.i18n; | |
| 23 | |
| 24 let whitelistFilter = null; | |
| 25 let promisedAcceptableAdsUrl = getAcceptableAdsUrl(); | |
| 26 | |
| 27 /* Utility functions */ | |
| 28 | |
| 29 function get(selector, origin) | |
|
saroyanm
2017/07/18 12:46:28
Detail: Calling function "get" is too generic, as
Thomas Greiner
2017/07/18 17:42:31
Acknowledged.
Note that the inspiration for this
saroyanm
2017/07/31 11:08:54
Now I see your intention, but in that case I think
| |
| 30 { | 22 { |
| 31 return (origin || document).querySelector(selector); | 23 const {getMessage} = ext.i18n; |
| 24 | |
| 25 const dialogSubscribe = "subscribe"; | |
| 26 const idAcceptableAds = "acceptableAds"; | |
| 27 const idRecommended = "subscriptions-recommended"; | |
| 28 let whitelistFilter = null; | |
| 29 let promisedAcceptableAdsUrl = getAcceptableAdsUrl(); | |
| 30 | |
| 31 /* Utility functions */ | |
| 32 | |
| 33 function get(selector, origin) | |
| 34 { | |
| 35 return (origin || document).querySelector(selector); | |
| 36 } | |
| 37 | |
| 38 function getAll(selector, origin) | |
| 39 { | |
| 40 return (origin || document).querySelectorAll(selector); | |
| 41 } | |
| 42 | |
| 43 function create(parent, tagName, content, attributes, onclick) | |
| 44 { | |
| 45 let element = document.createElement(tagName); | |
| 46 | |
| 47 if (typeof content == "string") | |
| 48 { | |
| 49 element.textContent = content; | |
| 50 } | |
| 51 | |
| 52 if (attributes) | |
| 53 { | |
| 54 for (let name in attributes) | |
| 55 { | |
| 56 element.setAttribute(name, attributes[name]); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 if (onclick) | |
| 61 { | |
| 62 element.addEventListener("click", (ev) => | |
| 63 { | |
| 64 onclick(ev); | |
| 65 ev.stopPropagation(); | |
| 66 }); | |
| 67 } | |
| 68 | |
| 69 parent.appendChild(element); | |
| 70 return element; | |
| 71 } | |
| 72 | |
| 73 /* Extension interactions */ | |
| 74 | |
| 75 function getInstalled() | |
| 76 { | |
| 77 return new Promise((resolve, reject) => | |
| 78 { | |
| 79 ext.backgroundPage.sendMessage( | |
| 80 {type: "subscriptions.get", downloadable: true}, | |
| 81 resolve | |
| 82 ); | |
| 83 }); | |
| 84 } | |
| 85 | |
| 86 function getAcceptableAdsUrl() | |
| 87 { | |
| 88 return new Promise((resolve, reject) => | |
| 89 { | |
| 90 ext.backgroundPage.sendMessage( | |
| 91 {type: "prefs.get", key: "subscriptions_exceptionsurl"}, | |
| 92 resolve | |
| 93 ); | |
| 94 }); | |
| 95 } | |
| 96 | |
| 97 function getRecommendedAds() | |
| 98 { | |
| 99 return fetch("subscriptions.xml") | |
| 100 .then((response) => response.text()) | |
| 101 .then((text) => | |
| 102 { | |
| 103 let doc = new DOMParser().parseFromString(text, "application/xml"); | |
| 104 let elements = Array.from(doc.getElementsByTagName("subscription")); | |
| 105 | |
| 106 return elements | |
| 107 .filter((element) => element.getAttribute("type") == "ads") | |
| 108 .map((element) => | |
| 109 { | |
| 110 return { | |
| 111 title: element.getAttribute("title"), | |
| 112 url: element.getAttribute("url") | |
| 113 }; | |
| 114 }); | |
| 115 }); | |
| 116 } | |
| 117 | |
| 118 function installSubscription(url, title) | |
| 119 { | |
| 120 ext.backgroundPage.sendMessage({type: "subscriptions.add", url, title}); | |
| 121 } | |
| 122 | |
| 123 function uninstallSubscription(url) | |
| 124 { | |
| 125 ext.backgroundPage.sendMessage({type: "subscriptions.remove", url}); | |
| 126 } | |
| 127 | |
| 128 /* Actions */ | |
| 129 | |
| 130 function setSubscription({disabled, title, url}, shouldAdd) | |
| 131 { | |
| 132 if (disabled) | |
| 133 return; | |
| 134 | |
| 135 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => | |
| 136 { | |
| 137 if (url == acceptableAdsUrl) | |
| 138 { | |
| 139 get(`#${idAcceptableAds}`).checked = true; | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 let listInstalled = get("#subscriptions-installed"); | |
| 144 let installed = get(`[data-url="${url}"]`, listInstalled); | |
| 145 | |
| 146 if (installed) | |
| 147 { | |
| 148 let titleElement = get("span", installed); | |
| 149 titleElement.textContent = title || url; | |
| 150 } | |
| 151 else if (shouldAdd) | |
| 152 { | |
| 153 let element = create(listInstalled, "li", null, {"data-url": url}); | |
| 154 create(element, "span", title || url); | |
| 155 create(element, "button", null, {class: "remove"}, | |
| 156 () => uninstallSubscription(url) | |
| 157 ); | |
| 158 | |
| 159 let recommended = get(`#${idRecommended} [data-url="${url}"]`); | |
| 160 if (recommended) | |
| 161 { | |
| 162 recommended.classList.add("installed"); | |
| 163 } | |
| 164 } | |
| 165 }); | |
| 166 } | |
| 167 | |
| 168 function removeSubscription(url) | |
| 169 { | |
| 170 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => | |
| 171 { | |
| 172 if (url == acceptableAdsUrl) | |
| 173 { | |
| 174 get(`#${idAcceptableAds}`).checked = false; | |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 let installed = get(`#subscriptions-installed [data-url="${url}"]`); | |
| 179 if (installed) | |
| 180 { | |
| 181 installed.parentNode.removeChild(installed); | |
| 182 } | |
| 183 | |
| 184 let recommended = get(`#${idRecommended} [data-url="${url}"]`); | |
| 185 if (recommended) | |
| 186 { | |
| 187 recommended.classList.remove("installed"); | |
| 188 } | |
| 189 }); | |
| 190 } | |
| 191 | |
| 192 function setDialog(id, options) | |
| 193 { | |
| 194 if (!id) | |
| 195 { | |
| 196 delete document.body.dataset.dialog; | |
| 197 return; | |
| 198 } | |
| 199 | |
| 200 let fields = getAll(`#dialog-${id} input`); | |
| 201 for (let field of fields) | |
| 202 { | |
| 203 let {name} = field; | |
| 204 field.value = (options && name in options) ? options[name] : ""; | |
| 205 } | |
| 206 setError(id, null); | |
| 207 | |
| 208 document.body.dataset.dialog = id; | |
| 209 } | |
| 210 | |
| 211 function setError(dialogId, fieldName) | |
| 212 { | |
| 213 let dialog = get(`#dialog-${dialogId}`); | |
| 214 if (fieldName) | |
| 215 { | |
| 216 dialog.dataset.error = fieldName; | |
| 217 } | |
| 218 else | |
| 219 { | |
| 220 delete dialog.dataset.error; | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 function populateLists() | |
| 225 { | |
| 226 Promise.all([getInstalled(), getRecommendedAds()]) | |
| 227 .then(([installed, recommended]) => | |
| 228 { | |
| 229 let listRecommended = get(`#${idRecommended}`); | |
| 230 for (let {title, url} of recommended) | |
| 231 { | |
| 232 create(listRecommended, "li", title, {"data-url": url}, | |
| 233 (ev) => | |
| 234 { | |
| 235 if (ev.target.classList.contains("installed")) | |
| 236 return; | |
| 237 | |
| 238 setDialog(dialogSubscribe, {title, url}); | |
| 239 } | |
| 240 ); | |
| 241 } | |
| 242 | |
| 243 for (let subscription of installed) | |
| 244 { | |
| 245 if (subscription.disabled) | |
| 246 continue; | |
| 247 | |
| 248 setSubscription(subscription, true); | |
| 249 } | |
| 250 }) | |
| 251 .catch((err) => console.error(err)); | |
| 252 } | |
| 253 | |
| 254 /* Listeners */ | |
| 255 | |
| 256 function onChange(ev) | |
| 257 { | |
| 258 if (ev.target.id != idAcceptableAds) | |
| 259 return; | |
| 260 | |
| 261 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => | |
| 262 { | |
| 263 if (ev.target.checked) | |
| 264 { | |
| 265 installSubscription(acceptableAdsUrl, null); | |
| 266 } | |
| 267 else | |
| 268 { | |
| 269 uninstallSubscription(acceptableAdsUrl); | |
| 270 } | |
| 271 }); | |
| 272 } | |
| 273 document.addEventListener("change", onChange); | |
| 274 | |
| 275 function toggleWhitelistFilter(toggle) | |
| 276 { | |
| 277 if (whitelistFilter) | |
| 278 { | |
| 279 ext.backgroundPage.sendMessage( | |
| 280 { | |
| 281 type: (toggle.checked) ? "filters.remove" : "filters.add", | |
| 282 text: whitelistFilter | |
| 283 }, | |
| 284 (errors) => | |
| 285 { | |
| 286 if (errors.length < 1) | |
| 287 return; | |
| 288 | |
| 289 console.error(errors); | |
| 290 toggle.checked = !toggle.checked; | |
| 291 } | |
| 292 ); | |
| 293 } | |
| 294 else | |
| 295 { | |
| 296 console.error("Whitelist filter hasn't been initialized yet"); | |
| 297 } | |
| 298 } | |
| 299 | |
| 300 function onClick(ev) | |
| 301 { | |
| 302 switch (ev.target.dataset.action) | |
| 303 { | |
| 304 case "close-dialog": | |
| 305 setDialog(null); | |
| 306 break; | |
| 307 case "open-dialog": | |
| 308 setDialog(ev.target.dataset.dialog); | |
| 309 break; | |
| 310 case "toggle-enabled": | |
| 311 toggleWhitelistFilter(ev.target); | |
| 312 ev.preventDefault(); | |
| 313 break; | |
| 314 } | |
| 315 } | |
| 316 document.addEventListener("click", onClick); | |
| 317 | |
| 318 function onSubmit(ev) | |
| 319 { | |
| 320 let fields = ev.target.elements; | |
| 321 let title = fields.title.value; | |
| 322 let url = fields.url.value; | |
| 323 | |
| 324 if (!title) | |
| 325 { | |
| 326 setError(dialogSubscribe, "title"); | |
| 327 } | |
| 328 else if (!url) | |
| 329 { | |
| 330 setError(dialogSubscribe, "url"); | |
| 331 } | |
| 332 else | |
| 333 { | |
| 334 installSubscription(url, title); | |
| 335 setDialog(null); | |
| 336 } | |
| 337 | |
| 338 ev.preventDefault(); | |
| 339 } | |
| 340 document.addEventListener("submit", onSubmit); | |
| 341 | |
| 342 function onMessage(msg) | |
| 343 { | |
| 344 switch (msg.type) | |
| 345 { | |
| 346 case "app.respond": { | |
| 347 switch (msg.action) | |
| 348 { | |
| 349 case "addSubscription": | |
| 350 let [subscription] = msg.args; | |
| 351 setDialog(dialogSubscribe, { | |
| 352 title: subscription.title, | |
| 353 url: subscription.url | |
| 354 }); | |
| 355 break; | |
| 356 case "showPageOptions": | |
| 357 let [{host, whitelisted}] = msg.args; | |
| 358 whitelistFilter = `@@||${host}^$document`; | |
| 359 | |
| 360 ext.i18n.setElementText( | |
| 361 get("#enabled-label"), | |
| 362 "mops_enabled_label", | |
| 363 [host] | |
| 364 ); | |
| 365 | |
| 366 let toggle = get("#enabled"); | |
| 367 toggle.checked = !whitelisted; | |
| 368 | |
| 369 get("#enabled-container").hidden = false; | |
| 370 break; | |
| 371 } | |
| 372 break; | |
| 373 } | |
| 374 case "filters.respond": { | |
| 375 let [filter] = msg.args; | |
| 376 if (!whitelistFilter || filter.text != whitelistFilter) | |
| 377 break; | |
| 378 | |
| 379 get("#enabled").checked = (msg.action == "removed"); | |
| 380 break; | |
| 381 } | |
| 382 case "subscriptions.respond": { | |
| 383 let [subscription] = msg.args; | |
| 384 switch (msg.action) | |
| 385 { | |
| 386 case "added": | |
| 387 setSubscription(subscription, true); | |
| 388 break; | |
| 389 case "disabled": | |
| 390 if (subscription.disabled) | |
| 391 { | |
| 392 removeSubscription(subscription.url); | |
| 393 } | |
| 394 else | |
| 395 { | |
| 396 setSubscription(subscription, true); | |
| 397 } | |
| 398 break; | |
| 399 case "removed": | |
| 400 removeSubscription(subscription.url); | |
| 401 break; | |
| 402 case "title": | |
| 403 // We're also receiving these messages for subscriptions that are | |
| 404 // not installed so we shouldn't add those by accident | |
| 405 setSubscription(subscription, false); | |
| 406 break; | |
| 407 } | |
| 408 break; | |
| 409 } | |
| 410 } | |
| 411 } | |
| 412 ext.onMessage.addListener(onMessage); | |
| 413 | |
| 414 ext.backgroundPage.sendMessage({ | |
| 415 type: "app.listen", | |
| 416 filter: ["addSubscription", "showPageOptions"] | |
| 417 }); | |
| 418 | |
| 419 ext.backgroundPage.sendMessage({ | |
| 420 type: "filters.listen", | |
| 421 filter: ["added", "removed"] | |
| 422 }); | |
| 423 | |
| 424 ext.backgroundPage.sendMessage({ | |
| 425 type: "subscriptions.listen", | |
| 426 filter: ["added", "disabled", "removed", "title"] | |
| 427 }); | |
| 428 | |
| 429 /* Initialization */ | |
| 430 | |
| 431 populateLists(); | |
| 432 | |
| 433 getDocLink("acceptable_ads", (link) => | |
| 434 { | |
| 435 get("#acceptableAds-more").href = link; | |
| 436 }); | |
| 437 | |
| 438 get("#dialog-subscribe [name='title']").setAttribute( | |
| 439 "placeholder", | |
| 440 getMessage("mops_subscribe_title") | |
| 441 ); | |
| 442 | |
| 443 get("#dialog-subscribe [name='url']").setAttribute( | |
| 444 "placeholder", | |
| 445 getMessage("mops_subscribe_url") | |
| 446 ); | |
| 32 } | 447 } |
| 33 | |
| 34 function getAll(selector, origin) | |
| 35 { | |
| 36 return (origin || document).querySelectorAll(selector); | |
| 37 } | |
| 38 | |
| 39 function create(parent, tagName, content, attributes, onclick) | |
| 40 { | |
| 41 let element = document.createElement(tagName); | |
| 42 | |
| 43 if (typeof content == "string") | |
| 44 { | |
| 45 element.textContent = content; | |
| 46 } | |
| 47 | |
| 48 if (attributes) | |
| 49 { | |
| 50 for (let name in attributes) | |
| 51 { | |
| 52 element.setAttribute(name, attributes[name]); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 if (onclick) | |
| 57 { | |
| 58 element.addEventListener("click", (ev) => | |
| 59 { | |
| 60 onclick(ev); | |
| 61 ev.stopPropagation(); | |
| 62 }); | |
|
saroyanm
2017/07/18 12:46:28
Note: I do remember before it was required to spec
Thomas Greiner
2017/07/18 17:42:32
We dropped that requirement a while ago. From what
| |
| 63 } | |
| 64 | |
| 65 parent.appendChild(element); | |
| 66 return element; | |
| 67 } | |
| 68 | |
| 69 /* Extension interactions */ | |
| 70 | |
| 71 function getInstalled() | |
| 72 { | |
| 73 return new Promise((resolve, reject) => | |
| 74 { | |
| 75 ext.backgroundPage.sendMessage( | |
| 76 {type: "subscriptions.get", downloadable: true}, | |
| 77 resolve | |
| 78 ); | |
| 79 }); | |
| 80 } | |
| 81 | |
| 82 function getAcceptableAdsUrl() | |
| 83 { | |
| 84 return new Promise((resolve, reject) => | |
| 85 { | |
| 86 ext.backgroundPage.sendMessage( | |
| 87 {type: "prefs.get", key: "subscriptions_exceptionsurl"}, | |
| 88 resolve | |
| 89 ); | |
| 90 }); | |
| 91 } | |
| 92 | |
| 93 function getRecommended() | |
|
saroyanm
2017/07/18 12:46:28
This function only return recommended subscription
Thomas Greiner
2017/07/18 17:42:33
Good point. Done.
| |
| 94 { | |
| 95 return fetch("subscriptions.xml") | |
| 96 .then((resp) => resp.text()) | |
|
saroyanm
2017/07/18 12:46:27
Detail: we usually are not using short form of the
saroyanm
2017/07/18 12:46:28
Shouldn't this be returned (return response.text()
Thomas Greiner
2017/07/18 17:42:31
No, this is one of the features of arrow functions
Thomas Greiner
2017/07/18 17:42:31
I agree that it makes sense for "response", so I'l
| |
| 97 .then((text) => | |
| 98 { | |
| 99 let doc = new DOMParser().parseFromString(text, "application/xml"); | |
| 100 let elements = Array.from(doc.getElementsByTagName("subscription")); | |
| 101 | |
| 102 return elements | |
| 103 .filter((element) => element.getAttribute("type") == "ads") | |
| 104 .map((element) => | |
| 105 { | |
| 106 return { | |
|
saroyanm
2017/07/18 12:46:27
Detail: the brace should go to the next row for co
Thomas Greiner
2017/07/18 17:42:31
Moving this brace to the next row would be interpr
| |
| 107 title: element.getAttribute("title"), | |
|
saroyanm
2017/07/18 12:46:28
Shouldn't this titles be translated ?
I think we
Thomas Greiner
2017/07/18 17:42:31
We don't translate filter list titles (e.g. "EasyL
| |
| 108 url: element.getAttribute("url") | |
| 109 }; | |
| 110 }); | |
| 111 }); | |
| 112 } | |
| 113 | |
| 114 function installSubscription(url, title) | |
| 115 { | |
| 116 ext.backgroundPage.sendMessage({type: "subscriptions.add", url, title}); | |
| 117 } | |
| 118 | |
| 119 function uninstallSubscription(url) | |
| 120 { | |
| 121 ext.backgroundPage.sendMessage({type: "subscriptions.remove", url}); | |
| 122 } | |
| 123 | |
| 124 /* Actions */ | |
| 125 | |
| 126 function setSubscription({disabled, title, url}, shouldAdd) | |
| 127 { | |
| 128 if (disabled) | |
| 129 return; | |
| 130 | |
| 131 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => | |
| 132 { | |
| 133 if (url == acceptableAdsUrl) | |
| 134 { | |
| 135 get("#acceptableAds").checked = true; | |
| 136 return; | |
| 137 } | |
| 138 | |
| 139 let listInstalled = get("#subscriptions-installed"); | |
| 140 let installed = get(`[data-url="${url}"]`, listInstalled); | |
|
saroyanm
2017/07/18 12:46:28
Detail: I think we are using single quotes "'", ra
Thomas Greiner
2017/07/18 17:42:32
This is a template literal, not a string literal.
| |
| 141 | |
| 142 if (installed) | |
| 143 { | |
| 144 let titleElement = get("span", installed); | |
| 145 titleElement.textContent = title || url; | |
| 146 } | |
| 147 else if (shouldAdd) | |
|
saroyanm
2017/07/18 12:46:28
Why are you using "shouldAdd" parameter ?
If the
Thomas Greiner
2017/07/18 17:42:31
Why should we show subscriptions that are not inst
saroyanm
2017/07/31 11:08:54
I think I meant "installed", nevermind, I don't re
| |
| 148 { | |
| 149 let element = create(listInstalled, "li", null, {"data-url": url}); | |
| 150 create(element, "span", title || url); | |
| 151 create(element, "button", null, {class: "remove"}, | |
| 152 () => uninstallSubscription(url) | |
| 153 ); | |
| 154 | |
| 155 let recommended = get(`#subscriptions-recommended [data-url="${url}"]`); | |
| 156 if (recommended) | |
| 157 { | |
| 158 recommended.classList.add("installed"); | |
| 159 } | |
| 160 } | |
| 161 }); | |
| 162 } | |
| 163 | |
| 164 function removeSubscription(url) | |
| 165 { | |
| 166 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => | |
| 167 { | |
| 168 if (url == acceptableAdsUrl) | |
| 169 { | |
| 170 get("#acceptable-ads").checked = false; | |
|
saroyanm
2017/07/18 12:46:27
I think you forget to change all camelCase IDs.
Thomas Greiner
2017/07/18 17:42:32
Yep, seems so. Done.
| |
| 171 return; | |
| 172 } | |
| 173 | |
| 174 let installed = get(`#subscriptions-installed [data-url="${url}"]`); | |
| 175 if (installed) | |
| 176 { | |
| 177 installed.parentNode.removeChild(installed); | |
| 178 } | |
| 179 | |
| 180 let recommended = get(`#subscriptions-recommended [data-url="${url}"]`); | |
| 181 if (recommended) | |
| 182 { | |
| 183 recommended.classList.remove("installed"); | |
| 184 } | |
| 185 }); | |
| 186 } | |
| 187 | |
| 188 function setDialog(id, options) | |
| 189 { | |
| 190 if (!id) | |
| 191 { | |
| 192 delete document.body.dataset.dialog; | |
| 193 return; | |
| 194 } | |
| 195 | |
| 196 let fields = getAll(`#dialog-${id} input`); | |
| 197 for (let field of fields) | |
| 198 { | |
| 199 field.value = (options && field.name in options) ? options[field.name] : ""; | |
| 200 } | |
| 201 setError(id, null); | |
| 202 | |
| 203 document.body.dataset.dialog = id; | |
| 204 } | |
| 205 | |
| 206 function setError(dialogId, message) | |
|
saroyanm
2017/07/18 12:46:28
Detail: Parameter name "message" is misleading, in
Thomas Greiner
2017/07/18 17:42:33
Looks like I forgot to adapt that. Done.
| |
| 207 { | |
| 208 let dialog = get(`#dialog-${dialogId}`); | |
| 209 if (message) | |
| 210 { | |
| 211 dialog.dataset.error = message; | |
| 212 } | |
| 213 else | |
| 214 { | |
| 215 delete dialog.dataset.error; | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 function populateLists() | |
| 220 { | |
| 221 Promise.all([getInstalled(), getRecommended()]) | |
| 222 .then(([installed, recommended]) => | |
| 223 { | |
| 224 let listRecommended = get("#subscriptions-recommended"); | |
| 225 for (let {title, url} of recommended) | |
| 226 { | |
| 227 create(listRecommended, "li", title, {"data-url": url}, | |
| 228 (ev) => | |
| 229 { | |
| 230 if (ev.target.classList.contains("installed")) | |
| 231 return; | |
| 232 | |
| 233 setDialog("subscribe", {title, url}); | |
|
saroyanm
2017/07/18 12:46:27
Detail: reference to "subscribe" dialog is used qu
Thomas Greiner
2017/07/18 17:42:32
Done.
| |
| 234 } | |
| 235 ); | |
| 236 } | |
| 237 | |
| 238 for (let subscription of installed) | |
| 239 { | |
| 240 if (subscription.disabled) | |
| 241 continue; | |
| 242 | |
| 243 setSubscription(subscription, true); | |
| 244 } | |
| 245 }) | |
| 246 .catch((err) => console.error(err)); | |
|
saroyanm
2017/07/18 12:46:28
I think you have mentioned this, so I'll ignore th
Thomas Greiner
2017/07/18 17:42:32
Acknowledged.
| |
| 247 } | |
| 248 | |
| 249 /* Listeners */ | |
| 250 | |
| 251 function onChange(ev) | |
| 252 { | |
| 253 if (ev.target.id != "acceptable-ads") | |
|
saroyanm
2017/07/18 12:46:27
Same as mentioned above, there is no element with
Thomas Greiner
2017/07/18 17:42:33
Done.
| |
| 254 return; | |
| 255 | |
| 256 promisedAcceptableAdsUrl.then((acceptableAdsUrl) => | |
| 257 { | |
| 258 if (ev.target.checked) | |
| 259 { | |
| 260 installSubscription(acceptableAdsUrl, null); | |
| 261 } | |
| 262 else | |
| 263 { | |
| 264 uninstallSubscription(acceptableAdsUrl); | |
| 265 } | |
| 266 }); | |
| 267 } | |
| 268 document.addEventListener("change", onChange); | |
| 269 | |
| 270 function onClick(ev) | |
| 271 { | |
| 272 switch (ev.target.dataset.action) | |
| 273 { | |
| 274 case "close-dialog": | |
|
saroyanm
2017/07/18 12:46:27
Note: Why not to handle all other click events her
Thomas Greiner
2017/07/18 17:42:32
Done. I'm also using it now for the "enabled" togg
| |
| 275 setDialog(null); | |
| 276 break; | |
| 277 case "open-dialog": | |
| 278 setDialog(ev.target.dataset.dialog); | |
| 279 break; | |
| 280 } | |
| 281 } | |
| 282 document.addEventListener("click", onClick); | |
| 283 | |
| 284 function onSubmit(ev) | |
| 285 { | |
| 286 let fields = ev.target.elements; | |
| 287 let title = fields.title.value; | |
| 288 let url = fields.url.value; | |
| 289 | |
| 290 if (!title) | |
| 291 { | |
| 292 setError("subscribe", "title"); | |
| 293 } | |
| 294 else if (!url) | |
| 295 { | |
| 296 setError("subscribe", "url"); | |
| 297 } | |
| 298 else | |
| 299 { | |
| 300 installSubscription(url, title); | |
| 301 setDialog(null); | |
| 302 } | |
| 303 | |
| 304 ev.preventDefault(); | |
| 305 } | |
| 306 document.addEventListener("submit", onSubmit); | |
| 307 | |
| 308 function onToggleWhitelistFilter(ev) | |
| 309 { | |
| 310 let checkbox = ev.target; | |
|
saroyanm
2017/07/18 12:46:28
Suggestion: We can avoid using element name as a v
Thomas Greiner
2017/07/18 17:42:32
In this case the implementation depends on it bein
| |
| 311 ext.backgroundPage.sendMessage( | |
| 312 { | |
| 313 type: (checkbox.checked) ? "filters.remove" : "filters.add", | |
| 314 text: whitelistFilter | |
| 315 }, (errors) => | |
| 316 { | |
| 317 if (errors.length < 1) | |
| 318 return; | |
| 319 | |
| 320 console.error(errors); | |
| 321 checkbox.checked = !checkbox.checked; | |
| 322 } | |
| 323 ); | |
| 324 ev.preventDefault(); | |
| 325 } | |
| 326 | |
| 327 function onMessage(msg) | |
| 328 { | |
| 329 switch (msg.type) | |
| 330 { | |
| 331 case "app.respond": { | |
| 332 switch (msg.action) | |
| 333 { | |
| 334 case "addSubscription": | |
| 335 let [subscription] = msg.args; | |
| 336 setDialog("subscribe", { | |
| 337 title: subscription.title, | |
| 338 url: subscription.url | |
| 339 }); | |
| 340 break; | |
| 341 case "showPageOptions": | |
| 342 let [{host, whitelisted}] = msg.args; | |
| 343 whitelistFilter = `@@||${host}^$document`; | |
|
saroyanm
2017/07/18 12:46:28
Shouldn't this be promise as well, similar to the
Thomas Greiner
2017/07/18 17:42:32
Even though it's not necessary because the UI usin
| |
| 344 | |
| 345 ext.i18n.setElementText( | |
| 346 get("#enabled-label"), | |
| 347 "mops_enabled_label", | |
| 348 [host] | |
| 349 ); | |
| 350 | |
| 351 let checkbox = get("#enabled"); | |
| 352 checkbox.checked = !whitelisted; | |
| 353 checkbox.addEventListener("click", onToggleWhitelistFilter); | |
| 354 | |
| 355 get("#enabled-container").hidden = false; | |
| 356 break; | |
| 357 } | |
| 358 break; | |
| 359 } | |
| 360 case "filters.respond": { | |
| 361 let [filter] = msg.args; | |
| 362 if (!whitelistFilter || filter.text != whitelistFilter) | |
|
saroyanm
2017/07/18 12:46:28
First check for "!whitelistFilter" seems to redund
Thomas Greiner
2017/07/18 17:42:31
While I don't expect `filter.text` to be `null`, i
| |
| 363 break; | |
| 364 | |
| 365 get("#enabled").checked = (msg.action == "removed"); | |
| 366 break; | |
| 367 } | |
| 368 case "subscriptions.respond": { | |
| 369 let [subscription] = msg.args; | |
| 370 switch (msg.action) | |
| 371 { | |
| 372 case "added": | |
| 373 setSubscription(subscription, true); | |
| 374 break; | |
| 375 case "disabled": | |
| 376 if (subscription.disabled) | |
| 377 { | |
| 378 removeSubscription(subscription.url); | |
| 379 } | |
| 380 else | |
| 381 { | |
| 382 setSubscription(subscription, true); | |
| 383 } | |
| 384 break; | |
| 385 case "removed": | |
| 386 removeSubscription(subscription.url); | |
| 387 break; | |
| 388 case "title": | |
| 389 // We're also receiving these messages for subscriptions that are not | |
| 390 // installed so we shouldn't add those by accident | |
| 391 setSubscription(subscription, false); | |
| 392 break; | |
| 393 } | |
| 394 break; | |
| 395 } | |
| 396 } | |
| 397 } | |
| 398 ext.onMessage.addListener(onMessage); | |
| 399 | |
| 400 ext.backgroundPage.sendMessage({ | |
| 401 type: "app.listen", | |
| 402 filter: ["addSubscription", "showPageOptions"] | |
| 403 }); | |
| 404 | |
| 405 ext.backgroundPage.sendMessage({ | |
| 406 type: "filters.listen", | |
| 407 filter: ["added", "removed"] | |
| 408 }); | |
| 409 | |
| 410 ext.backgroundPage.sendMessage({ | |
| 411 type: "subscriptions.listen", | |
| 412 filter: ["added", "disabled", "removed", "title"] | |
| 413 }); | |
| 414 | |
| 415 /* Initialization */ | |
| 416 | |
| 417 populateLists(); | |
| 418 | |
| 419 getDocLink("acceptable_ads", (link) => | |
| 420 { | |
| 421 get("#acceptableAds-more").href = link; | |
| 422 }); | |
| 423 | |
| 424 get("#dialog-subscribe [name='title']").setAttribute( | |
| 425 "placeholder", | |
| 426 getMessage("mops_subscribe_title") | |
| 427 ); | |
| 428 | |
| 429 get("#dialog-subscribe [name='url']").setAttribute( | |
| 430 "placeholder", | |
| 431 getMessage("mops_subscribe_url") | |
| 432 ); | |
| LEFT | RIGHT |