Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/ui.js

Issue 5538776168267776: Implemented anti-adblock message notification (Closed)
Patch Set: Created Feb. 12, 2014, 11:55 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« lib/notification.js ('K') | « lib/notification.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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,74 @@
}
},
+ initAntiAdblockNotification: function()
Felix Dahlke 2014/02/12 15:09:27 Shouldn't this be "private"?
Thomas Greiner 2014/02/12 18:28:02 Done.
+ {
+ let notification = {
Felix Dahlke 2014/02/12 15:09:27 I think we should move this into addAntiAdblockNot
Thomas Greiner 2014/02/12 18:28:02 No, it's also used to remove the notification (see
Felix Dahlke 2014/02/13 10:22:04 Oh, right.
+ id: "antiadblock",
+ severity: "question",
+ title: Utils.getString("notification_antiadblock_title"),
+ message: Utils.getString("notification_antiadblock_message"),
+ urlFilters: []
+ };
+
+ 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);
+ }
+
+ let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
+ if (subscription.lastDownload && subscription.disabled)
+ {
+ addAntiAdblockNotification(subscription);
+ }
+
+ function notificationListener(id, approved)
+ {
+ if (id !== notification.id)
+ return;
+
+ let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
+ if (subscription.url in FilterStorage.knownSubscriptions)
+ subscription.disabled = !approved;
+ }
+ Notification.addListener(notificationListener);
+
+ 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))
+ {
+ Notification.removeNotification(notification);
+ }
+ });
+
+ 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 +593,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 +846,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 +1851,7 @@
}
},
- _showNextNotification: function()
+ _showNextNotification: function(url)
{
let window = this.currentWindow;
if (!window)
@@ -1776,7 +1862,7 @@
if (!button)
return;
- let notification = Notification.getNextToShow();
+ let notification = Notification.getNextToShow(url);
if (!notification)
return;
@@ -1785,6 +1871,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 +1901,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 +1912,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.severity === "question")
+ {
+ function buttonHandler(approved, event)
+ {
+ event.preventDefault();
+ event.stopPropagation();
+ panel.hidePopup();
+ Notification.triggerListeners(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.severity);
+ panel.setAttribute("noautohide", notification.severity === "question");
panel.openPopup(button, "bottomcenter topcenter", 0, 0, false, false, null);
}
};
« lib/notification.js ('K') | « lib/notification.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld