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-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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 * @typedef {object} DefaultSubscriptions | 90 * @typedef {object} DefaultSubscriptions |
91 * @property {?Element} ads | 91 * @property {?Element} ads |
92 * @property {?Element} cv | 92 * @property {?Element} circumvention |
93 */ | 93 */ |
94 /** | 94 /** |
95 * Finds the elements for the default ad blocking filter subscriptions based | 95 * Finds the elements for the default ad blocking filter subscriptions based |
96 * on the user's locale. | 96 * on the user's locale. |
97 * | 97 * |
98 * @param {HTMLCollection} subscriptions | 98 * @param {HTMLCollection} subscriptions |
99 * @return {DefaultSubscriptions} | 99 * @return {DefaultSubscriptions} |
100 */ | 100 */ |
101 function chooseFilterSubscriptions(subscriptions) | 101 function chooseFilterSubscriptions(subscriptions) |
102 { | 102 { |
103 let selectedItem = {}; | 103 let selectedItem = {}; |
104 let selectedPrefix = null; | 104 let selectedPrefix = null; |
105 let matchCount = 0; | 105 let matchCount = 0; |
106 for (let subscription of subscriptions) | 106 for (let subscription of subscriptions) |
107 { | 107 { |
108 let prefixes = subscription.getAttribute("prefixes"); | 108 let prefixes = subscription.getAttribute("prefixes"); |
109 let prefix = prefixes && prefixes.split(",").find( | 109 let prefix = prefixes && prefixes.split(",").find( |
110 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale) | 110 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale) |
111 ); | 111 ); |
112 | 112 |
113 let subscriptionType = subscription.getAttribute("type"); | 113 let subscriptionType = subscription.getAttribute("type"); |
114 | 114 |
115 if (subscriptionType in ["ads", "cv"] && !selectedItem[subscriptionType]) | 115 if ((subscriptionType == "ads" || subscriptionType == "circumvention") && |
116 !selectedItem[subscriptionType]) | |
116 selectedItem[subscriptionType] = subscription; | 117 selectedItem[subscriptionType] = subscription; |
117 | 118 |
118 if (prefix) | 119 if (prefix) |
119 { | 120 { |
120 // The "ads" subscription is the one driving the selection. | 121 // The "ads" subscription is the one driving the selection. |
121 if (subscriptionType == "ads") | 122 if (subscriptionType == "ads") |
122 { | 123 { |
123 if (!selectedPrefix || selectedPrefix.length < prefix.length) | 124 if (!selectedPrefix || selectedPrefix.length < prefix.length) |
124 { | 125 { |
125 selectedItem[subscriptionType] = subscription; | 126 selectedItem[subscriptionType] = subscription; |
126 selectedPrefix = prefix; | 127 selectedPrefix = prefix; |
127 matchCount = 1; | 128 matchCount = 1; |
128 } | 129 } |
129 else if (selectedPrefix && selectedPrefix.length == prefix.length) | 130 else if (selectedPrefix && selectedPrefix.length == prefix.length) |
130 { | 131 { |
131 matchCount++; | 132 matchCount++; |
132 | 133 |
133 // If multiple items have a matching prefix of the same length: | 134 // If multiple items have a matching prefix of the same length: |
134 // Select one of the items randomly, probability should be the same | 135 // Select one of the items randomly, probability should be the same |
135 // for all items. So we replace the previous match here with | 136 // for all items. So we replace the previous match here with |
136 // probability 1/N (N being the number of matches). | 137 // probability 1/N (N being the number of matches). |
137 if (Math.random() * matchCount < 1) | 138 if (Math.random() * matchCount < 1) |
138 { | 139 { |
139 selectedItem[subscriptionType] = subscription; | 140 selectedItem[subscriptionType] = subscription; |
140 selectedPrefix = prefix; | 141 selectedPrefix = prefix; |
141 } | 142 } |
142 } | 143 } |
143 } | 144 } |
144 else if (subscriptionType == "cv") | 145 else if (subscriptionType == "circumvention") |
145 { | 146 { |
146 selectedItem[subscriptionType] = subscription; | 147 selectedItem[subscriptionType] = subscription; |
147 } | 148 } |
148 } | 149 } |
149 } | 150 } |
150 return selectedItem; | 151 return selectedItem; |
151 } | 152 } |
152 | 153 |
153 function supportsNotificationsWithButtons() | 154 function supportsNotificationsWithButtons() |
154 { | 155 { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 // Add default ad blocking subscription (e.g. EasyList) | 213 // Add default ad blocking subscription (e.g. EasyList) |
213 if (shouldAddDefaultSubscription()) | 214 if (shouldAddDefaultSubscription()) |
214 { | 215 { |
215 return fetch("subscriptions.xml") | 216 return fetch("subscriptions.xml") |
216 .then(response => response.text()) | 217 .then(response => response.text()) |
217 .then(text => | 218 .then(text => |
218 { | 219 { |
219 let doc = new DOMParser().parseFromString(text, "application/xml"); | 220 let doc = new DOMParser().parseFromString(text, "application/xml"); |
220 let nodes = doc.getElementsByTagName("subscription"); | 221 let nodes = doc.getElementsByTagName("subscription"); |
221 | 222 |
222 let subs = chooseFilterSubscriptions(nodes); | 223 let defaultSubscriptions = chooseFilterSubscriptions(nodes); |
223 if (subs) | 224 if (defaultSubscriptions) |
224 { | 225 { |
225 for (let name in subs) | 226 for (let name in defaultSubscriptions) |
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.
| |
226 { | 227 { |
227 let node = subs[name]; | 228 let node = defaultSubscriptions[name]; |
228 if (!node) | 229 if (!node) |
229 continue; | 230 continue; |
230 | 231 |
231 let url = node.getAttribute("url"); | 232 let url = node.getAttribute("url"); |
232 if (url) | 233 if (url) |
233 { | 234 { |
234 let subscription = Subscription.fromURL(url); | 235 let subscription = Subscription.fromURL(url); |
235 subscription.disabled = false; | 236 subscription.disabled = false; |
236 subscription.title = node.getAttribute("title"); | 237 subscription.title = node.getAttribute("title"); |
237 subscription.homepage = node.getAttribute("homepage"); | 238 subscription.homepage = node.getAttribute("homepage"); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 * | 324 * |
324 * @param {function} callback | 325 * @param {function} callback |
325 */ | 326 */ |
326 exports.setSubscriptionsCallback = callback => | 327 exports.setSubscriptionsCallback = callback => |
327 { | 328 { |
328 subscriptionsCallback = callback; | 329 subscriptionsCallback = callback; |
329 }; | 330 }; |
330 | 331 |
331 // Exports for tests only | 332 // Exports for tests only |
332 exports.chooseFilterSubscriptions = chooseFilterSubscriptions; | 333 exports.chooseFilterSubscriptions = chooseFilterSubscriptions; |
LEFT | RIGHT |