| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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-present 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 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 | 80 |
| 81 if (subscription instanceof SpecialSubscription && | 81 if (subscription instanceof SpecialSubscription && |
| 82 subscription.filters.length > 0) | 82 subscription.filters.length > 0) |
| 83 return false; | 83 return false; |
| 84 } | 84 } |
| 85 | 85 |
| 86 return true; | 86 return true; |
| 87 } | 87 } |
| 88 | 88 |
| 89 /** | 89 /** |
| 90 * Finds the element for the default ad blocking filter subscription based | 90 * @typedef {object} DefaultSubscriptions |
| 91 * @property {?Element} ads | |
| 92 * @property {?Element} cv | |
| 93 */ | |
| 94 /** | |
| 95 * Finds the elements for the default ad blocking filter subscriptions based | |
| 91 * on the user's locale. | 96 * on the user's locale. |
| 92 * | 97 * |
| 93 * @param {HTMLCollection} subscriptions | 98 * @param {HTMLCollection} subscriptions |
| 94 * @return {Element} | 99 * @return {DefaultSubscriptions} |
| 95 */ | 100 */ |
| 96 function chooseFilterSubscription(subscriptions) | 101 function chooseFilterSubscriptions(subscriptions) |
| 97 { | 102 { |
| 98 let selectedItem = null; | 103 let selectedItem = {}; |
| 99 let selectedPrefix = null; | 104 let selectedPrefix = null; |
| 100 let matchCount = 0; | 105 let matchCount = 0; |
| 101 for (let subscription of subscriptions) | 106 for (let subscription of subscriptions) |
| 102 { | 107 { |
| 103 if (!selectedItem) | |
| 104 selectedItem = subscription; | |
| 105 | |
| 106 let prefixes = subscription.getAttribute("prefixes"); | 108 let prefixes = subscription.getAttribute("prefixes"); |
| 107 let prefix = prefixes && prefixes.split(",").find( | 109 let prefix = prefixes && prefixes.split(",").find( |
| 108 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale) | 110 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale) |
| 109 ); | 111 ); |
| 110 | 112 |
| 111 let subscriptionType = subscription.getAttribute("type"); | 113 let subscriptionType = subscription.getAttribute("type"); |
| 112 | 114 |
| 113 if (prefix && subscriptionType == "ads") | 115 if (subscriptionType in ["ads", "cv"] && !selectedItem[subscriptionType]) |
| 116 selectedItem[subscriptionType] = subscription; | |
| 117 | |
| 118 if (prefix) | |
| 114 { | 119 { |
| 115 if (!selectedPrefix || selectedPrefix.length < prefix.length) | 120 // The "ads" subscription is the one driving the selection. |
| 121 if (subscriptionType == "ads") | |
| 116 { | 122 { |
| 117 selectedItem = subscription; | 123 if (!selectedPrefix || selectedPrefix.length < prefix.length) |
| 118 selectedPrefix = prefix; | 124 { |
| 119 matchCount = 1; | 125 selectedItem[subscriptionType] = subscription; |
| 126 selectedPrefix = prefix; | |
| 127 matchCount = 1; | |
| 128 } | |
| 129 else if (selectedPrefix && selectedPrefix.length == prefix.length) | |
| 130 { | |
| 131 matchCount++; | |
| 132 | |
| 133 // If multiple items have a matching prefix of the same length: | |
| 134 // Select one of the items randomly, probability should be the same | |
| 135 // for all items. So we replace the previous match here with | |
| 136 // probability 1/N (N being the number of matches). | |
| 137 if (Math.random() * matchCount < 1) | |
| 138 { | |
| 139 selectedItem[subscriptionType] = subscription; | |
| 140 selectedPrefix = prefix; | |
| 141 } | |
| 142 } | |
| 120 } | 143 } |
| 121 else if (selectedPrefix && selectedPrefix.length == prefix.length) | 144 else if (subscriptionType == "cv") |
| 122 { | 145 { |
| 123 matchCount++; | 146 selectedItem[subscriptionType] = subscription; |
| 124 | |
| 125 // If multiple items have a matching prefix of the same length: | |
| 126 // Select one of the items randomly, probability should be the same | |
| 127 // for all items. So we replace the previous match here with | |
| 128 // probability 1/N (N being the number of matches). | |
| 129 if (Math.random() * matchCount < 1) | |
| 130 { | |
| 131 selectedItem = subscription; | |
| 132 selectedPrefix = prefix; | |
| 133 } | |
| 134 } | 147 } |
| 135 } | 148 } |
| 136 } | 149 } |
| 137 return selectedItem; | 150 return selectedItem; |
| 138 } | 151 } |
| 139 | 152 |
| 140 function supportsNotificationsWithButtons() | 153 function supportsNotificationsWithButtons() |
| 141 { | 154 { |
| 142 // Microsoft Edge (as of EdgeHTML 16) doesn't have the notifications API. | 155 // Microsoft Edge (as of EdgeHTML 16) doesn't have the notifications API. |
| 143 // Opera gives an asynchronous error when buttons are provided (we cannot | 156 // Opera gives an asynchronous error when buttons are provided (we cannot |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 // Add default ad blocking subscription (e.g. EasyList) | 212 // Add default ad blocking subscription (e.g. EasyList) |
| 200 if (shouldAddDefaultSubscription()) | 213 if (shouldAddDefaultSubscription()) |
| 201 { | 214 { |
| 202 return fetch("subscriptions.xml") | 215 return fetch("subscriptions.xml") |
| 203 .then(response => response.text()) | 216 .then(response => response.text()) |
| 204 .then(text => | 217 .then(text => |
| 205 { | 218 { |
| 206 let doc = new DOMParser().parseFromString(text, "application/xml"); | 219 let doc = new DOMParser().parseFromString(text, "application/xml"); |
| 207 let nodes = doc.getElementsByTagName("subscription"); | 220 let nodes = doc.getElementsByTagName("subscription"); |
| 208 | 221 |
| 209 let node = chooseFilterSubscription(nodes); | 222 let subs = chooseFilterSubscriptions(nodes); |
| 210 if (node) | 223 if (subs) |
| 211 { | 224 { |
| 212 let url = node.getAttribute("url"); | 225 for (let name in subs) |
|
Manish Jethani
2018/06/13 16:58:12
By the way, if you want to keep the return type a
hub
2018/06/13 21:58:19
There are at most two properties.
Manish Jethani
2018/06/14 05:07:34
Acknowledged.
Since the calling code is now assum
Manish Jethani
2018/06/14 05:42:56
OK, forget this, it makes it worse.
Like you said
hub
2018/06/14 19:59:57
Acknowledged.
| |
| 213 if (url) | |
| 214 { | 226 { |
| 215 let subscription = Subscription.fromURL(url); | 227 let node = subs[name]; |
| 216 subscription.disabled = false; | 228 if (!node) |
| 217 subscription.title = node.getAttribute("title"); | 229 continue; |
| 218 subscription.homepage = node.getAttribute("homepage"); | 230 |
| 219 subscriptions.push(subscription); | 231 let url = node.getAttribute("url"); |
| 232 if (url) | |
| 233 { | |
| 234 let subscription = Subscription.fromURL(url); | |
| 235 subscription.disabled = false; | |
| 236 subscription.title = node.getAttribute("title"); | |
| 237 subscription.homepage = node.getAttribute("homepage"); | |
| 238 subscription.type = node.getAttribute("type"); | |
| 239 subscriptions.push(subscription); | |
| 240 } | |
| 220 } | 241 } |
| 221 } | 242 } |
| 222 | 243 |
| 223 return subscriptions; | 244 return subscriptions; |
| 224 }); | 245 }); |
| 225 } | 246 } |
| 226 | 247 |
| 227 return subscriptions; | 248 return subscriptions; |
| 228 } | 249 } |
| 229 | 250 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 * Sets a callback that is called with an array of subscriptions to be added | 320 * Sets a callback that is called with an array of subscriptions to be added |
| 300 * during initialization. The callback must return an array of subscriptions | 321 * during initialization. The callback must return an array of subscriptions |
| 301 * that will effectively be added. | 322 * that will effectively be added. |
| 302 * | 323 * |
| 303 * @param {function} callback | 324 * @param {function} callback |
| 304 */ | 325 */ |
| 305 exports.setSubscriptionsCallback = callback => | 326 exports.setSubscriptionsCallback = callback => |
| 306 { | 327 { |
| 307 subscriptionsCallback = callback; | 328 subscriptionsCallback = callback; |
| 308 }; | 329 }; |
| 330 | |
| 331 // Exports for tests only | |
| 332 exports.chooseFilterSubscriptions = chooseFilterSubscriptions; | |
| OLD | NEW |