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. 11, 2014, 4:51 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
« no previous file with comments | « 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
Felix Dahlke 2014/02/11 17:57:17 I think all the anti adblock message specific code
Thomas Greiner 2014/02/12 11:58:49 Done.
+ function getURLFiltersFromSubscription(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);
+ return urlFilters;
+ }
+
+ let antiadblockNotification = {
+ id: "antiadblock",
+ severity: "question",
+ title: Utils.getString("notification_antiadblock_title"),
+ message: Utils.getString("notification_antiadblock_message"),
+ urlFilters: [],
+ onApproved: function()
Felix Dahlke 2014/02/11 17:57:17 I really don't think we should be able to put code
Thomas Greiner 2014/02/12 11:58:49 You cannot include functions in JSON so they can o
Felix Dahlke 2014/02/12 15:09:27 Oh that's right of course, wouldn't work anyway.
+ {
+ let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
+ if (subscription.url in FilterStorage.knownSubscriptions)
+ 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.urlFilters = getURLFiltersFromSubscription(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.urlFilters = getURLFiltersFromSubscription(value);
Felix Dahlke 2014/02/11 17:57:17 This is repeated above. How about having a createA
Thomas Greiner 2014/02/12 11:58:49 Done.
+ Notification.addNotification(antiadblockNotification);
+ }
+ else if (action == "subscription.removed" || (action == "subscription.disabled" && !value.disabled))
+ {
+ Notification.removeNotification(antiadblockNotification);
+ }
+ });
+
+ var httpRequestObserver = {
Felix Dahlke 2014/02/11 17:57:17 Shouldn't it be called documentCreatingObserver or
Thomas Greiner 2014/02/12 11:58:49 Done.
+ 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 17:57:17 I think we should do this for everyone, no matter
Thomas Greiner 2014/02/12 11:58:49 Done.
+ {
+ 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")
Felix Dahlke 2014/02/11 17:57:17 Shouldn't this be an "if"? Anyway, why aren't we
Thomas Greiner 2014/02/12 11:58:49 Done. The bubbling lead to the link opening no ma
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(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.severity);
+ panel.setAttribute("noautohide", notification.severity === "question");
panel.openPopup(button, "bottomcenter topcenter", 0, 0, false, false, null);
}
};
« no previous file with comments | « lib/notification.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld