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

Unified Diff: background.js

Issue 5749582424178688: Ported over anti-adblock message notification (Closed)
Patch Set: Created Feb. 28, 2014, 6:41 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 | « no previous file | chrome/ext/background.js » ('j') | popup.html » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: background.js
===================================================================
--- a/background.js
+++ b/background.js
@@ -18,6 +18,7 @@
with(require("filterClasses"))
{
this.Filter = Filter;
+ this.ActiveFilter = ActiveFilter;
this.RegExpFilter = RegExpFilter;
this.BlockingFilter = BlockingFilter;
this.WhitelistFilter = WhitelistFilter;
@@ -62,6 +63,8 @@
if (!importingOldData)
addSubscription(prevVersion);
}
+
+ initAntiAdblockNotification();
}
// update browser actions when whitelisting might have changed,
@@ -201,6 +204,16 @@
addAcceptable = false;
}
+ // Add "anti-adblock messages" subscription
+ var 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;
@@ -239,6 +252,60 @@
notifyUser();
}
+function initAntiAdblockNotification()
Felix Dahlke 2014/03/18 14:21:41 This is pretty much the same code we have in Firef
Thomas Greiner 2014/03/19 13:45:40 Done.
+{
+ var notification = {
+ id: "antiadblock",
+ type: "question",
+ title: ext.i18n.getMessage("global_notification_antiadblock_title"),
+ message: ext.i18n.getMessage("global_notification_antiadblock_message"),
+ urlFilters: []
+ };
+
+ function notificationListener(approved)
+ {
+ var subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
+ if (subscription.url in FilterStorage.knownSubscriptions)
+ subscription.disabled = !approved;
+ }
+
+ function addAntiAdblockNotification(subscription)
+ {
+ var urlFilters = [];
+ subscription.filters.forEach(function(filter)
+ {
+ if (filter instanceof ActiveFilter)
+ for (var 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);
+ }
+
+ var 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();
+ });
+}
+
function setContextMenu()
{
if (Prefs.shouldShowBlockElementMenu)
@@ -300,7 +367,7 @@
iconAnimation.stop();
activeNotification = null;
};
- iconAnimation.update(activeNotification.severity);
+ iconAnimation.update(activeNotification.type);
}
function openNotificationLinks()
@@ -319,7 +386,13 @@
function notificationButtonClick(id, index)
{
- if (activeNotification.links && activeNotification.links[index])
+ if (activeNotification.type === "question")
+ {
+ Notification.triggerQuestionListeners(activeNotification.id, index === 0);
+ Notification.markAsShown(activeNotification.id);
+ activeNotification.onClicked();
+ }
+ else if (activeNotification.links && activeNotification.links[index])
{
ext.windows.getLastFocused(function(win)
{
@@ -330,15 +403,18 @@
function showNotification(notification)
{
+ if (activeNotification && activeNotification.id === notification.id)
+ return;
+
activeNotification = notification;
- if (activeNotification.severity === "critical")
+ if (activeNotification.type === "critical" || activeNotification.type === "question")
{
var hasWebkitNotifications = typeof webkitNotifications !== "undefined";
if (hasWebkitNotifications && "createHTMLNotification" in webkitNotifications)
{
var notification = webkitNotifications.createHTMLNotification("notification.html");
notification.show();
- notification.addEventListener("close", prepareNotificationIconAndPopup, false);
+ prepareNotificationIconAndPopup();
return;
}
@@ -347,34 +423,43 @@
var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
var iconUrl = ext.getURL("icons/abp-128.png");
var hasLinks = activeNotification.links && activeNotification.links.length > 0;
- if ("browserNotifications" in ext)
+
+ // Chrome on Linux does not fully support chrome.notifications yet
+ // https://code.google.com/p/chromium/issues/detail?id=291485
+ if (chrome && "notifications" in chrome && navigator.platform.indexOf("Linux") === -1)
{
var opts = {
type: "basic",
title: title,
message: message,
iconUrl: iconUrl,
- buttons: []
+ buttons: [],
+ priority: 2 // We use the highest priority to prevent the notification from closing automatically
};
- var regex = /<a>(.*?)<\/a>/g;
- var plainMessage = texts.message || "";
- var match;
- while (match = regex.exec(plainMessage))
- opts.buttons.push({title: match[1]});
+ if (activeNotification.type === "question")
+ {
+ opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_button_yes")});
+ opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_button_no")});
+ }
+ else
+ {
+ var regex = /<a>(.*?)<\/a>/g;
+ var plainMessage = texts.message || "";
+ var match;
+ while (match = regex.exec(plainMessage))
+ opts.buttons.push({title: match[1]});
+ }
- var notification = ext.browserNotifications;
- notification.create("", opts, function() {});
- notification.onClosed.addListener(prepareNotificationIconAndPopup);
- notification.onButtonClicked.addListener(notificationButtonClick);
+ chrome.notifications.create("", opts, function() {});
+ chrome.notifications.onButtonClicked.addListener(notificationButtonClick);
}
- else if (hasWebkitNotifications && "createNotification" in webkitNotifications)
+ else if (hasWebkitNotifications && "createNotification" in webkitNotifications && activeNotification.type !== "question")
{
if (hasLinks)
message += " " + ext.i18n.getMessage("notification_without_buttons");
var notification = webkitNotifications.createNotification(iconUrl, title, message);
notification.show();
- notification.addEventListener("close", prepareNotificationIconAndPopup, false);
notification.addEventListener("click", openNotificationLinks, false);
}
else
@@ -383,13 +468,14 @@
if (hasLinks)
message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
- if (confirm(message))
+ var approved = confirm(message);
+ if (activeNotification.type === "question")
+ notificationButtonClick(null, approved ? 0 : 1);
+ else if (approved)
openNotificationLinks();
- prepareNotificationIconAndPopup();
}
}
- else
- prepareNotificationIconAndPopup();
+ prepareNotificationIconAndPopup();
}
ext.onMessage.addListener(function (msg, sender, sendResponse)
« no previous file with comments | « no previous file | chrome/ext/background.js » ('j') | popup.html » ('J')

Powered by Google App Engine
This is Rietveld