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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 74 |
75 if (subscription instanceof SpecialSubscription && | 75 if (subscription instanceof SpecialSubscription && |
76 subscription.filters.length > 0) | 76 subscription.filters.length > 0) |
77 return false; | 77 return false; |
78 } | 78 } |
79 | 79 |
80 return true; | 80 return true; |
81 } | 81 } |
82 | 82 |
83 /** | 83 /** |
| 84 * Finds the element for the default ad blocking filter subscription based |
| 85 * on the user's locale. |
| 86 * |
| 87 * @param {HTMLCollection} subscriptions |
| 88 * @return {Element} |
| 89 */ |
| 90 function chooseFilterSubscription(subscriptions) |
| 91 { |
| 92 let selectedItem = null; |
| 93 let selectedPrefix = null; |
| 94 let matchCount = 0; |
| 95 for (let subscription of subscriptions) |
| 96 { |
| 97 if (!selectedItem) |
| 98 selectedItem = subscription; |
| 99 |
| 100 let prefixes = subscription.getAttribute("prefixes"); |
| 101 let prefix = prefixes && prefixes.split(",").find( |
| 102 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale) |
| 103 ); |
| 104 |
| 105 let subscriptionType = subscription.getAttribute("type"); |
| 106 |
| 107 if (prefix && subscriptionType == "ads") |
| 108 { |
| 109 if (!selectedPrefix || selectedPrefix.length < prefix.length) |
| 110 { |
| 111 selectedItem = subscription; |
| 112 selectedPrefix = prefix; |
| 113 matchCount = 1; |
| 114 } |
| 115 else if (selectedPrefix && selectedPrefix.length == prefix.length) |
| 116 { |
| 117 matchCount++; |
| 118 |
| 119 // If multiple items have a matching prefix of the same length: |
| 120 // Select one of the items randomly, probability should be the same |
| 121 // for all items. So we replace the previous match here with |
| 122 // probability 1/N (N being the number of matches). |
| 123 if (Math.random() * matchCount < 1) |
| 124 { |
| 125 selectedItem = subscription; |
| 126 selectedPrefix = prefix; |
| 127 } |
| 128 } |
| 129 } |
| 130 } |
| 131 return selectedItem; |
| 132 } |
| 133 |
| 134 /** |
84 * Gets the filter subscriptions to be added when the extnesion is loaded. | 135 * Gets the filter subscriptions to be added when the extnesion is loaded. |
85 * | 136 * |
86 * @return {Promise|Subscription[]} | 137 * @return {Promise|Subscription[]} |
87 */ | 138 */ |
88 function getSubscriptions() | 139 function getSubscriptions() |
89 { | 140 { |
90 let subscriptions = []; | 141 let subscriptions = []; |
91 | 142 |
92 // Add pre-configured subscriptions | 143 // Add pre-configured subscriptions |
93 for (let url of Prefs.additional_subscriptions) | 144 for (let url of Prefs.additional_subscriptions) |
(...skipping 18 matching lines...) Expand all Loading... |
112 // Add default ad blocking subscription (e.g. EasyList) | 163 // Add default ad blocking subscription (e.g. EasyList) |
113 if (shouldAddDefaultSubscription()) | 164 if (shouldAddDefaultSubscription()) |
114 { | 165 { |
115 return fetch("subscriptions.xml") | 166 return fetch("subscriptions.xml") |
116 .then(response => response.text()) | 167 .then(response => response.text()) |
117 .then(text => | 168 .then(text => |
118 { | 169 { |
119 let doc = new DOMParser().parseFromString(text, "application/xml"); | 170 let doc = new DOMParser().parseFromString(text, "application/xml"); |
120 let nodes = doc.getElementsByTagName("subscription"); | 171 let nodes = doc.getElementsByTagName("subscription"); |
121 | 172 |
122 let node = Utils.chooseFilterSubscription(nodes); | 173 let node = chooseFilterSubscription(nodes); |
123 if (node) | 174 if (node) |
124 { | 175 { |
125 let url = node.getAttribute("url"); | 176 let url = node.getAttribute("url"); |
126 if (url) | 177 if (url) |
127 { | 178 { |
128 let subscription = Subscription.fromURL(url); | 179 let subscription = Subscription.fromURL(url); |
129 subscription.disabled = false; | 180 subscription.disabled = false; |
130 subscription.title = node.getAttribute("title"); | 181 subscription.title = node.getAttribute("title"); |
131 subscription.homepage = node.getAttribute("homepage"); | 182 subscription.homepage = node.getAttribute("homepage"); |
132 subscriptions.push(subscription); | 183 subscriptions.push(subscription); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 * Sets a callback that is called with an array of subscriptions to be added | 228 * Sets a callback that is called with an array of subscriptions to be added |
178 * during initialization. The callback must return an array of subscriptions | 229 * during initialization. The callback must return an array of subscriptions |
179 * that will effectively be added. | 230 * that will effectively be added. |
180 * | 231 * |
181 * @param {function} callback | 232 * @param {function} callback |
182 */ | 233 */ |
183 exports.setSubscriptionsCallback = callback => | 234 exports.setSubscriptionsCallback = callback => |
184 { | 235 { |
185 subscriptionsCallback = callback; | 236 subscriptionsCallback = callback; |
186 }; | 237 }; |
OLD | NEW |