| Index: lib/ui.js |
| =================================================================== |
| --- a/lib/ui.js |
| +++ b/lib/ui.js |
| @@ -25,7 +25,7 @@ |
| let {FilterStorage} = require("filterStorage"); |
| let {FilterNotifier} = require("filterNotifier"); |
| let {RequestNotifier} = require("requestNotifier"); |
| -let {Filter} = require("filterClasses"); |
| +let {Filter, ActiveFilter} = require("filterClasses"); |
| let {Subscription, SpecialSubscription, DownloadableSubscription} = require("subscriptionClasses"); |
| let {Synchronizer} = require("synchronizer"); |
| let {KeySelector} = require("keySelector"); |
| @@ -398,6 +398,73 @@ |
| } |
| }, |
| + _initAntiAdblockNotification: function() |
| + { |
| + let notification = { |
| + id: "antiadblock", |
| + type: "question", |
| + title: Utils.getString("notification_antiadblock_title"), |
| + message: Utils.getString("notification_antiadblock_message"), |
| + urlFilters: [] |
| + }; |
| + |
| + function notificationListener(approved) |
| + { |
| + let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl); |
| + if (subscription.url in FilterStorage.knownSubscriptions) |
| + subscription.disabled = !approved; |
| + } |
| + |
| + function addAntiAdblockNotification(subscription) |
| + { |
| + let urlFilters = []; |
| + for each (let filter in subscription.filters) |
| + if (filter instanceof ActiveFilter) |
| + for (let domain in filter.domains) |
| + if (domain && urlFilters.indexOf(domain) == -1) |
| + urlFilters.push(domain); |
| + notification.urlFilters = urlFilters; |
| + Notification.addNotification(notification); |
| + Notification.addQuestionListener(notification.id, notificationListener); |
| + } |
| + |
| + function removeAntiAdblockNotification() |
| + { |
| + Notification.removeNotification(notification); |
| + Notification.removeQuestionListener(notification.id, notificationListener); |
| + } |
| + |
| + let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl); |
| + if (subscription.lastDownload && subscription.disabled) |
| + addAntiAdblockNotification(subscription); |
| + |
| + FilterNotifier.addListener(function(action, value, newItem, oldItem) |
| + { |
| + if (!/^subscription\.(updated|removed|disabled)$/.test(action) || value.url != Prefs.subscriptions_antiadblockurl) |
| + return; |
| + |
| + if (action == "subscription.updated") |
| + addAntiAdblockNotification(value); |
| + else if (action == "subscription.removed" || (action == "subscription.disabled" && !value.disabled)) |
| + removeAntiAdblockNotification(); |
| + }); |
| + |
| + var documentCreationObserver = { |
| + observe: function(subject, topic, data) |
| + { |
| + if (!(subject instanceof Ci.nsIDOMWindow)) |
| + return; |
| + |
| + this._showNextNotification(subject.location.href); |
| + }.bind(this) |
| + }; |
| + Services.obs.addObserver(documentCreationObserver, "content-document-global-created", false); |
| + onShutdown.add(function() |
| + { |
| + Services.obs.removeObserver(documentCreationObserver, "content-document-global-created", false); |
| + }); |
| + }, |
| + |
| /** |
| * Gets called once the initialization is finished and Adblock Plus elements |
| * can be added to the UI. |
| @@ -525,6 +592,14 @@ |
| }.bind(this)); |
| addBrowserClickListener(window, this.onBrowserClick.bind(this, window)); |
| + window.document.getElementById("abp-notification-close").addEventListener("command", function(event) |
| + { |
| + window.document.getElementById("abp-notification").hidePopup(); |
| + }, false); |
| + |
| + // Add "anti-adblock messages" notification |
| + this._initAntiAdblockNotification(); |
| + |
| // First-run actions? |
| if (!this.firstRunDone) |
| { |
| @@ -770,6 +845,16 @@ |
| addAcceptable = false; |
| } |
| + // Add "anti-adblock messages" subscription |
| + let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl); |
| + if (subscription) |
| + { |
| + subscription.disabled = true; |
| + FilterStorage.addSubscription(subscription); |
| + if (subscription instanceof DownloadableSubscription && !subscription.lastDownload) |
| + Synchronizer.execute(subscription); |
| + } |
| + |
| if (!addSubscription && !addAcceptable) |
| return; |
| @@ -1765,7 +1850,7 @@ |
| } |
| }, |
| - _showNextNotification: function() |
| + _showNextNotification: function(url) |
| { |
| let window = this.currentWindow; |
| if (!window) |
| @@ -1776,7 +1861,7 @@ |
| if (!button) |
| return; |
| - let notification = Notification.getNextToShow(); |
| + let notification = Notification.getNextToShow(url); |
| if (!notification) |
| return; |
| @@ -1785,6 +1870,10 @@ |
| _showNotification: function(window, button, notification) |
| { |
| + let panel = window.document.getElementById("abp-notification"); |
| + if (panel.state !== "closed") |
| + return; |
| + |
| function insertMessage(element, text, links) |
| { |
| let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text); |
| @@ -1811,6 +1900,7 @@ |
| let titleElement = window.document.getElementById("abp-notification-title"); |
| titleElement.setAttribute("value", texts.title); |
| let messageElement = window.document.getElementById("abp-notification-message"); |
| + messageElement.innerHTML = ""; |
| let docLinks = []; |
| for each (let link in notification.links) |
| docLinks.push(Utils.getDocLink(link)); |
| @@ -1821,14 +1911,29 @@ |
| let link = event.target; |
| while (link && link !== messageElement && link.localName !== "a") |
| link = link.parentNode; |
| - if (!link) |
| + if (!link || link.localName !== "a") |
| return; |
| event.preventDefault(); |
| event.stopPropagation(); |
| this.loadInBrowser(link.href, window); |
| }.bind(this)); |
| - let panel = window.document.getElementById("abp-notification"); |
| + if (notification.type === "question") |
| + { |
| + function buttonHandler(approved, event) |
| + { |
| + event.preventDefault(); |
| + event.stopPropagation(); |
| + panel.hidePopup(); |
| + Notification.triggerQuestionListeners(notification.id, approved) |
| + Notification.markAsShown(notification.id); |
| + } |
| + window.document.getElementById("abp-notification-yes").onclick = buttonHandler.bind(null, true); |
| + window.document.getElementById("abp-notification-no").onclick = buttonHandler.bind(null, false); |
| + } |
| + |
| + panel.setAttribute("class", "abp-" + notification.type); |
| + panel.setAttribute("noautohide", notification.type === "question"); |
| panel.openPopup(button, "bottomcenter topcenter", 0, 0, false, false, null); |
| } |
| }; |