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 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 * Returns the default filter subscriptions to add. | |
Sebastian Noack
2017/10/03 00:24:40
I wouldn't insist on adding comments for functions
wspee
2017/10/03 09:26:54
Done.
| |
85 * | |
86 * @param {HTMLCollection} subscriptions | |
87 * @return {Element} | |
88 */ | |
89 function chooseFilterSubscription(subscriptions) | |
90 { | |
91 let selectedItem = null; | |
92 let selectedPrefix = null; | |
93 let matchCount = 0; | |
94 for (let subscription of subscriptions) | |
95 { | |
96 if (!selectedItem) | |
97 selectedItem = subscription; | |
98 | |
99 let prefix = Utils.checkLocalePrefixMatch( | |
Sebastian Noack
2017/10/03 00:24:40
Since this seems to be the only call to that funct
wspee
2017/10/03 09:26:55
Done.
| |
100 subscription.getAttribute("prefixes") | |
101 ); | |
102 | |
103 let subscriptionType = subscription.getAttribute("type"); | |
104 if (subscriptionType != "ads") | |
Sebastian Noack
2017/10/03 00:24:40
This could be combined with the check below:
if
wspee
2017/10/03 09:26:54
Done.
| |
105 { | |
106 continue; | |
107 } | |
108 | |
109 if (prefix) | |
110 { | |
111 if (!selectedPrefix || selectedPrefix.length < prefix.length) | |
112 { | |
113 selectedItem = subscription; | |
114 selectedPrefix = prefix; | |
115 matchCount = 1; | |
116 } | |
117 else if (selectedPrefix && selectedPrefix.length == prefix.length) | |
118 { | |
119 matchCount++; | |
120 | |
121 // If multiple items have a matching prefix of the same length: | |
122 // Select one of the items randomly, probability should be the same | |
123 // for all items. So we replace the previous match here with | |
124 // probability 1/N (N being the number of matches). | |
125 if (Math.random() * matchCount < 1) | |
126 { | |
127 selectedItem = subscription; | |
128 selectedPrefix = prefix; | |
129 } | |
130 } | |
131 } | |
132 } | |
133 return selectedItem; | |
134 } | |
135 | |
136 /** | |
84 * Gets the filter subscriptions to be added when the extnesion is loaded. | 137 * Gets the filter subscriptions to be added when the extnesion is loaded. |
85 * | 138 * |
86 * @return {Promise|Subscription[]} | 139 * @return {Promise|Subscription[]} |
87 */ | 140 */ |
88 function getSubscriptions() | 141 function getSubscriptions() |
89 { | 142 { |
90 let subscriptions = []; | 143 let subscriptions = []; |
91 | 144 |
92 // Add pre-configured subscriptions | 145 // Add pre-configured subscriptions |
93 for (let url of Prefs.additional_subscriptions) | 146 for (let url of Prefs.additional_subscriptions) |
(...skipping 18 matching lines...) Expand all Loading... | |
112 // Add default ad blocking subscription (e.g. EasyList) | 165 // Add default ad blocking subscription (e.g. EasyList) |
113 if (shouldAddDefaultSubscription()) | 166 if (shouldAddDefaultSubscription()) |
114 { | 167 { |
115 return fetch("subscriptions.xml") | 168 return fetch("subscriptions.xml") |
116 .then(response => response.text()) | 169 .then(response => response.text()) |
117 .then(text => | 170 .then(text => |
118 { | 171 { |
119 let doc = new DOMParser().parseFromString(text, "application/xml"); | 172 let doc = new DOMParser().parseFromString(text, "application/xml"); |
120 let nodes = doc.getElementsByTagName("subscription"); | 173 let nodes = doc.getElementsByTagName("subscription"); |
121 | 174 |
122 let node = Utils.chooseFilterSubscription(nodes); | 175 let node = chooseFilterSubscription(nodes); |
123 if (node) | 176 if (node) |
124 { | 177 { |
125 let url = node.getAttribute("url"); | 178 let url = node.getAttribute("url"); |
126 if (url) | 179 if (url) |
127 { | 180 { |
128 let subscription = Subscription.fromURL(url); | 181 let subscription = Subscription.fromURL(url); |
129 subscription.disabled = false; | 182 subscription.disabled = false; |
130 subscription.title = node.getAttribute("title"); | 183 subscription.title = node.getAttribute("title"); |
131 subscription.homepage = node.getAttribute("homepage"); | 184 subscription.homepage = node.getAttribute("homepage"); |
132 subscriptions.push(subscription); | 185 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 | 230 * 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 | 231 * during initialization. The callback must return an array of subscriptions |
179 * that will effectively be added. | 232 * that will effectively be added. |
180 * | 233 * |
181 * @param {function} callback | 234 * @param {function} callback |
182 */ | 235 */ |
183 exports.setSubscriptionsCallback = callback => | 236 exports.setSubscriptionsCallback = callback => |
184 { | 237 { |
185 subscriptionsCallback = callback; | 238 subscriptionsCallback = callback; |
186 }; | 239 }; |
OLD | NEW |