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. 7, 2014, 5:08 p.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");
@@ -525,6 +525,79 @@
}.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
+ function getDomainsFromSubscription(subscription)
+ {
+ let domains = [];
+ for each (let filter in subscription.filters)
+ if (filter instanceof ActiveFilter)
+ for (let domain in filter.domains)
+ if (domain && domains.indexOf(domain) == -1)
+ domains.push(domain);
+ return domains;
+ }
+
+ let antiadblockNotification = {
+ id: "antiadblock",
+ type: "question",
+ title: Utils.getString("notification_antiadblock_title"),
+ message: Utils.getString("notification_antiadblock_message"),
+ domains: [],
+ onApproved: function()
+ {
+ let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
+ if (subscription.url in FilterStorage.knownSubscriptions)
Felix Dahlke 2014/02/11 17:19:44 Shouldn't we add it here if it's not in the knownS
Felix Dahlke 2014/02/11 17:57:17 Actually not, it's fine :)
+ subscription.disabled = false;
+ },
+ onDeclined: function()
+ {
+ let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
+ if (subscription.url in FilterStorage.knownSubscriptions)
+ subscription.disabled = true;
+ }
+ };
+ let antiadblockSubscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
+ if (antiadblockSubscription.lastDownload && antiadblockSubscription.disabled)
+ {
+ antiadblockNotification.domains = getDomainsFromSubscription(antiadblockSubscription);
+ Notification.addNotification(antiadblockNotification);
+ }
+ FilterNotifier.addListener(function(action, value, newItem, oldItem)
+ {
+ if (!/^subscription\.(updated|removed|disabled)$/.test(action) || value.url != Prefs.subscriptions_antiadblockurl)
+ return;
+
+ if (action == "subscription.updated")
+ {
+ antiadblockNotification.domains = getDomainsFromSubscription(value);
+ Notification.addNotification(antiadblockNotification);
+ }
+ else if (action == "subscription.removed" || (action == "subscription.disabled" && !value.disabled))
+ {
+ Notification.removeNotification(antiadblockNotification);
+ }
+ });
+
+ var httpRequestObserver = {
+ observe: function(subject, topic, data)
+ {
+ if (!(subject instanceof Ci.nsIDOMWindow))
+ return;
+
+ this._showNextNotification(subject.location.href);
+ }.bind(this)
+ };
+ Services.obs.addObserver(httpRequestObserver, "content-document-global-created", false);
+ onShutdown.add(function()
+ {
+ Services.obs.removeObserver(httpRequestObserver, "content-document-global-created", false);
+ });
+
// First-run actions?
if (!this.firstRunDone)
{
@@ -770,6 +843,19 @@
addAcceptable = false;
}
+ // Add "anti-adblock messages" subscription for new users and users updating from old ABP versions.
+ if (Services.vc.compare(prevVersion, "2.5") < 0)
Felix Dahlke 2014/02/11 10:35:27 I think we should show them to every user once act
Thomas Greiner 2014/02/11 16:53:31 This is just the initial download of the filterlis
+ {
+ 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));
@@ -1819,16 +1910,32 @@
messageElement.addEventListener("click", function(event)
{
let link = event.target;
- while (link && link !== messageElement && link.localName !== "a")
- link = link.parentNode;
- if (!link)
+ while (!link || link === messageElement || 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(event)
+ {
+ event.preventDefault();
+ event.stopPropagation();
+ panel.hidePopup();
+
+ let action = notification[event.target.getAttribute("action")];
+ if (typeof action === "function")
+ action();
+ Notification.markAsShown(notification.id);
+ }
+ window.document.getElementById("abp-notification-yes").oncommand = buttonHandler;
+ window.document.getElementById("abp-notification-no").oncommand = buttonHandler;
+ }
+
+ panel.setAttribute("class", "abp-" + notification.type);
+ panel.setAttribute("noautohide", notification.type === "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