Index: lib/subscriptionInit.js |
=================================================================== |
--- a/lib/subscriptionInit.js |
+++ b/lib/subscriptionInit.js |
@@ -82,61 +82,75 @@ |
subscription.filters.length > 0) |
return false; |
} |
return true; |
} |
/** |
- * Finds the element for the default ad blocking filter subscription based |
+ * @typedef {object} DefaultSubscriptions |
+ * @property {?Element} ads |
+ * @property {?Element} circumvention |
+ */ |
+/** |
+ * Finds the elements for the default ad blocking filter subscriptions based |
* on the user's locale. |
* |
* @param {HTMLCollection} subscriptions |
- * @return {Element} |
+ * @return {DefaultSubscriptions} |
*/ |
-function chooseFilterSubscription(subscriptions) |
+function chooseFilterSubscriptions(subscriptions) |
{ |
- let selectedItem = null; |
+ let selectedItem = {}; |
let selectedPrefix = null; |
let matchCount = 0; |
for (let subscription of subscriptions) |
{ |
- if (!selectedItem) |
- selectedItem = subscription; |
- |
let prefixes = subscription.getAttribute("prefixes"); |
let prefix = prefixes && prefixes.split(",").find( |
lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale) |
); |
let subscriptionType = subscription.getAttribute("type"); |
- if (prefix && subscriptionType == "ads") |
+ if ((subscriptionType == "ads" || subscriptionType == "circumvention") && |
+ !selectedItem[subscriptionType]) |
+ selectedItem[subscriptionType] = subscription; |
+ |
+ if (prefix) |
{ |
- if (!selectedPrefix || selectedPrefix.length < prefix.length) |
- { |
- selectedItem = subscription; |
- selectedPrefix = prefix; |
- matchCount = 1; |
- } |
- else if (selectedPrefix && selectedPrefix.length == prefix.length) |
+ // The "ads" subscription is the one driving the selection. |
+ if (subscriptionType == "ads") |
{ |
- matchCount++; |
+ if (!selectedPrefix || selectedPrefix.length < prefix.length) |
+ { |
+ selectedItem[subscriptionType] = subscription; |
+ selectedPrefix = prefix; |
+ matchCount = 1; |
+ } |
+ else if (selectedPrefix && selectedPrefix.length == prefix.length) |
+ { |
+ matchCount++; |
- // If multiple items have a matching prefix of the same length: |
- // Select one of the items randomly, probability should be the same |
- // for all items. So we replace the previous match here with |
- // probability 1/N (N being the number of matches). |
- if (Math.random() * matchCount < 1) |
- { |
- selectedItem = subscription; |
- selectedPrefix = prefix; |
+ // If multiple items have a matching prefix of the same length: |
+ // Select one of the items randomly, probability should be the same |
+ // for all items. So we replace the previous match here with |
+ // probability 1/N (N being the number of matches). |
+ if (Math.random() * matchCount < 1) |
+ { |
+ selectedItem[subscriptionType] = subscription; |
+ selectedPrefix = prefix; |
+ } |
} |
} |
+ else if (subscriptionType == "circumvention") |
+ { |
+ selectedItem[subscriptionType] = subscription; |
+ } |
} |
} |
return selectedItem; |
} |
function supportsNotificationsWithButtons() |
{ |
// Microsoft Edge (as of EdgeHTML 16) doesn't have the notifications API. |
@@ -201,27 +215,35 @@ |
{ |
return fetch("subscriptions.xml") |
.then(response => response.text()) |
.then(text => |
{ |
let doc = new DOMParser().parseFromString(text, "application/xml"); |
let nodes = doc.getElementsByTagName("subscription"); |
- let node = chooseFilterSubscription(nodes); |
- if (node) |
+ let defaultSubscriptions = chooseFilterSubscriptions(nodes); |
+ if (defaultSubscriptions) |
{ |
- let url = node.getAttribute("url"); |
- if (url) |
+ for (let name in defaultSubscriptions) |
{ |
- let subscription = Subscription.fromURL(url); |
- subscription.disabled = false; |
- subscription.title = node.getAttribute("title"); |
- subscription.homepage = node.getAttribute("homepage"); |
- subscriptions.push(subscription); |
+ let node = defaultSubscriptions[name]; |
+ if (!node) |
+ continue; |
+ |
+ let url = node.getAttribute("url"); |
+ if (url) |
+ { |
+ let subscription = Subscription.fromURL(url); |
+ subscription.disabled = false; |
+ subscription.title = node.getAttribute("title"); |
+ subscription.homepage = node.getAttribute("homepage"); |
+ subscription.type = node.getAttribute("type"); |
+ subscriptions.push(subscription); |
+ } |
} |
} |
return subscriptions; |
}); |
} |
return subscriptions; |
@@ -301,8 +323,11 @@ |
* that will effectively be added. |
* |
* @param {function} callback |
*/ |
exports.setSubscriptionsCallback = callback => |
{ |
subscriptionsCallback = callback; |
}; |
+ |
+// Exports for tests only |
+exports.chooseFilterSubscriptions = chooseFilterSubscriptions; |