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

Side by Side Diff: background.js

Issue 5749582424178688: Ported over anti-adblock message notification (Closed)
Patch Set: Created Feb. 28, 2014, 6:41 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/ext/background.js » ('j') | popup.html » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 with(require("filterClasses")) 18 with(require("filterClasses"))
19 { 19 {
20 this.Filter = Filter; 20 this.Filter = Filter;
21 this.ActiveFilter = ActiveFilter;
21 this.RegExpFilter = RegExpFilter; 22 this.RegExpFilter = RegExpFilter;
22 this.BlockingFilter = BlockingFilter; 23 this.BlockingFilter = BlockingFilter;
23 this.WhitelistFilter = WhitelistFilter; 24 this.WhitelistFilter = WhitelistFilter;
24 } 25 }
25 with(require("subscriptionClasses")) 26 with(require("subscriptionClasses"))
26 { 27 {
27 this.Subscription = Subscription; 28 this.Subscription = Subscription;
28 this.DownloadableSubscription = DownloadableSubscription; 29 this.DownloadableSubscription = DownloadableSubscription;
29 } 30 }
30 with(require("whitelisting")) 31 with(require("whitelisting"))
(...skipping 24 matching lines...) Expand all
55 56
56 var addonVersion = require("info").addonVersion; 57 var addonVersion = require("info").addonVersion;
57 var prevVersion = localStorage.currentVersion; 58 var prevVersion = localStorage.currentVersion;
58 if (prevVersion != addonVersion) 59 if (prevVersion != addonVersion)
59 { 60 {
60 isFirstRun = !prevVersion; 61 isFirstRun = !prevVersion;
61 localStorage.currentVersion = addonVersion; 62 localStorage.currentVersion = addonVersion;
62 if (!importingOldData) 63 if (!importingOldData)
63 addSubscription(prevVersion); 64 addSubscription(prevVersion);
64 } 65 }
66
67 initAntiAdblockNotification();
65 } 68 }
66 69
67 // update browser actions when whitelisting might have changed, 70 // update browser actions when whitelisting might have changed,
68 // due to loading filters or saving filter changes 71 // due to loading filters or saving filter changes
69 if (action == "load" || action == "save") 72 if (action == "load" || action == "save")
70 { 73 {
71 ext.windows.getAll(function(windows) 74 ext.windows.getAll(function(windows)
72 { 75 {
73 for (var i = 0; i < windows.length; i++) 76 for (var i = 0; i < windows.length; i++)
74 { 77 {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 { 197 {
195 subscription.title = "Allow non-intrusive advertising"; 198 subscription.title = "Allow non-intrusive advertising";
196 FilterStorage.addSubscription(subscription); 199 FilterStorage.addSubscription(subscription);
197 if (subscription instanceof DownloadableSubscription && !subscription.last Download) 200 if (subscription instanceof DownloadableSubscription && !subscription.last Download)
198 Synchronizer.execute(subscription); 201 Synchronizer.execute(subscription);
199 } 202 }
200 else 203 else
201 addAcceptable = false; 204 addAcceptable = false;
202 } 205 }
203 206
207 // Add "anti-adblock messages" subscription
208 var subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
209 if (subscription)
210 {
211 subscription.disabled = true;
212 FilterStorage.addSubscription(subscription);
213 if (subscription instanceof DownloadableSubscription && !subscription.lastDo wnload)
214 Synchronizer.execute(subscription);
215 }
216
204 if (!addSubscription && !addAcceptable) 217 if (!addSubscription && !addAcceptable)
205 return; 218 return;
206 219
207 function notifyUser() 220 function notifyUser()
208 { 221 {
209 ext.windows.getLastFocused(function(win) 222 ext.windows.getLastFocused(function(win)
210 { 223 {
211 win.openTab(ext.getURL("firstRun.html")); 224 win.openTab(ext.getURL("firstRun.html"));
212 }); 225 });
213 } 226 }
(...skipping 18 matching lines...) Expand all
232 245
233 notifyUser(); 246 notifyUser();
234 } 247 }
235 }, false); 248 }, false);
236 request.send(null); 249 request.send(null);
237 } 250 }
238 else 251 else
239 notifyUser(); 252 notifyUser();
240 } 253 }
241 254
255 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.
256 {
257 var notification = {
258 id: "antiadblock",
259 type: "question",
260 title: ext.i18n.getMessage("global_notification_antiadblock_title"),
261 message: ext.i18n.getMessage("global_notification_antiadblock_message"),
262 urlFilters: []
263 };
264
265 function notificationListener(approved)
266 {
267 var subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
268 if (subscription.url in FilterStorage.knownSubscriptions)
269 subscription.disabled = !approved;
270 }
271
272 function addAntiAdblockNotification(subscription)
273 {
274 var urlFilters = [];
275 subscription.filters.forEach(function(filter)
276 {
277 if (filter instanceof ActiveFilter)
278 for (var domain in filter.domains)
279 if (domain && urlFilters.indexOf(domain) == -1)
280 urlFilters.push(domain);
281 });
282 notification.urlFilters = urlFilters;
283 Notification.addNotification(notification);
284 Notification.addQuestionListener(notification.id, notificationListener);
285 }
286
287 function removeAntiAdblockNotification()
288 {
289 Notification.removeNotification(notification);
290 Notification.removeQuestionListener(notification.id, notificationListener);
291 }
292
293 var subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
294 if (subscription.lastDownload && subscription.disabled)
295 addAntiAdblockNotification(subscription);
296
297 FilterNotifier.addListener(function(action, value, newItem, oldItem)
298 {
299 if (!/^subscription\.(updated|removed|disabled)$/.test(action) || value.url != Prefs.subscriptions_antiadblockurl)
300 return;
301
302 if (action == "subscription.updated")
303 addAntiAdblockNotification(value);
304 else if (action == "subscription.removed" || (action == "subscription.disabl ed" && !value.disabled))
305 removeAntiAdblockNotification();
306 });
307 }
308
242 function setContextMenu() 309 function setContextMenu()
243 { 310 {
244 if (Prefs.shouldShowBlockElementMenu) 311 if (Prefs.shouldShowBlockElementMenu)
245 { 312 {
246 // Register context menu item 313 // Register context menu item
247 ext.contextMenus.addMenuItem(ext.i18n.getMessage("block_element"), ["image", "video", "audio"], function(srcUrl, tab) 314 ext.contextMenus.addMenuItem(ext.i18n.getMessage("block_element"), ["image", "video", "audio"], function(srcUrl, tab)
248 { 315 {
249 if (srcUrl) 316 if (srcUrl)
250 tab.sendMessage({type: "clickhide-new-filter", filter: srcUrl}); 317 tab.sendMessage({type: "clickhide-new-filter", filter: srcUrl});
251 }); 318 });
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 }); 360 });
294 } 361 }
295 362
296 function prepareNotificationIconAndPopup() 363 function prepareNotificationIconAndPopup()
297 { 364 {
298 activeNotification.onClicked = function() 365 activeNotification.onClicked = function()
299 { 366 {
300 iconAnimation.stop(); 367 iconAnimation.stop();
301 activeNotification = null; 368 activeNotification = null;
302 }; 369 };
303 iconAnimation.update(activeNotification.severity); 370 iconAnimation.update(activeNotification.type);
304 } 371 }
305 372
306 function openNotificationLinks() 373 function openNotificationLinks()
307 { 374 {
308 if (activeNotification.links) 375 if (activeNotification.links)
309 { 376 {
310 activeNotification.links.forEach(function(link) 377 activeNotification.links.forEach(function(link)
311 { 378 {
312 ext.windows.getLastFocused(function(win) 379 ext.windows.getLastFocused(function(win)
313 { 380 {
314 win.openTab(Utils.getDocLink(link)); 381 win.openTab(Utils.getDocLink(link));
315 }); 382 });
316 }); 383 });
317 } 384 }
318 } 385 }
319 386
320 function notificationButtonClick(id, index) 387 function notificationButtonClick(id, index)
321 { 388 {
322 if (activeNotification.links && activeNotification.links[index]) 389 if (activeNotification.type === "question")
390 {
391 Notification.triggerQuestionListeners(activeNotification.id, index === 0);
392 Notification.markAsShown(activeNotification.id);
393 activeNotification.onClicked();
394 }
395 else if (activeNotification.links && activeNotification.links[index])
323 { 396 {
324 ext.windows.getLastFocused(function(win) 397 ext.windows.getLastFocused(function(win)
325 { 398 {
326 win.openTab(Utils.getDocLink(activeNotification.links[index])); 399 win.openTab(Utils.getDocLink(activeNotification.links[index]));
327 }); 400 });
328 } 401 }
329 } 402 }
330 403
331 function showNotification(notification) 404 function showNotification(notification)
332 { 405 {
406 if (activeNotification && activeNotification.id === notification.id)
407 return;
408
333 activeNotification = notification; 409 activeNotification = notification;
334 if (activeNotification.severity === "critical") 410 if (activeNotification.type === "critical" || activeNotification.type === "que stion")
335 { 411 {
336 var hasWebkitNotifications = typeof webkitNotifications !== "undefined"; 412 var hasWebkitNotifications = typeof webkitNotifications !== "undefined";
337 if (hasWebkitNotifications && "createHTMLNotification" in webkitNotification s) 413 if (hasWebkitNotifications && "createHTMLNotification" in webkitNotification s)
338 { 414 {
339 var notification = webkitNotifications.createHTMLNotification("notificatio n.html"); 415 var notification = webkitNotifications.createHTMLNotification("notificatio n.html");
340 notification.show(); 416 notification.show();
341 notification.addEventListener("close", prepareNotificationIconAndPopup, fa lse); 417 prepareNotificationIconAndPopup();
342 return; 418 return;
343 } 419 }
344 420
345 var texts = Notification.getLocalizedTexts(notification); 421 var texts = Notification.getLocalizedTexts(notification);
346 var title = texts.title || ""; 422 var title = texts.title || "";
347 var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 423 var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
348 var iconUrl = ext.getURL("icons/abp-128.png"); 424 var iconUrl = ext.getURL("icons/abp-128.png");
349 var hasLinks = activeNotification.links && activeNotification.links.length > 0; 425 var hasLinks = activeNotification.links && activeNotification.links.length > 0;
350 if ("browserNotifications" in ext) 426
427 // Chrome on Linux does not fully support chrome.notifications yet
428 // https://code.google.com/p/chromium/issues/detail?id=291485
429 if (chrome && "notifications" in chrome && navigator.platform.indexOf("Linux ") === -1)
351 { 430 {
352 var opts = { 431 var opts = {
353 type: "basic", 432 type: "basic",
354 title: title, 433 title: title,
355 message: message, 434 message: message,
356 iconUrl: iconUrl, 435 iconUrl: iconUrl,
357 buttons: [] 436 buttons: [],
437 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
358 }; 438 };
359 var regex = /<a>(.*?)<\/a>/g; 439 if (activeNotification.type === "question")
360 var plainMessage = texts.message || ""; 440 {
361 var match; 441 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_yes")});
362 while (match = regex.exec(plainMessage)) 442 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_no")});
363 opts.buttons.push({title: match[1]}); 443 }
444 else
445 {
446 var regex = /<a>(.*?)<\/a>/g;
447 var plainMessage = texts.message || "";
448 var match;
449 while (match = regex.exec(plainMessage))
450 opts.buttons.push({title: match[1]});
451 }
364 452
365 var notification = ext.browserNotifications; 453 chrome.notifications.create("", opts, function() {});
366 notification.create("", opts, function() {}); 454 chrome.notifications.onButtonClicked.addListener(notificationButtonClick);
367 notification.onClosed.addListener(prepareNotificationIconAndPopup);
368 notification.onButtonClicked.addListener(notificationButtonClick);
369 } 455 }
370 else if (hasWebkitNotifications && "createNotification" in webkitNotificatio ns) 456 else if (hasWebkitNotifications && "createNotification" in webkitNotificatio ns && activeNotification.type !== "question")
371 { 457 {
372 if (hasLinks) 458 if (hasLinks)
373 message += " " + ext.i18n.getMessage("notification_without_buttons"); 459 message += " " + ext.i18n.getMessage("notification_without_buttons");
374 460
375 var notification = webkitNotifications.createNotification(iconUrl, title, message); 461 var notification = webkitNotifications.createNotification(iconUrl, title, message);
376 notification.show(); 462 notification.show();
377 notification.addEventListener("close", prepareNotificationIconAndPopup, fa lse);
378 notification.addEventListener("click", openNotificationLinks, false); 463 notification.addEventListener("click", openNotificationLinks, false);
379 } 464 }
380 else 465 else
381 { 466 {
382 var message = title + "\n" + message; 467 var message = title + "\n" + message;
383 if (hasLinks) 468 if (hasLinks)
384 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); 469 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
385 470
386 if (confirm(message)) 471 var approved = confirm(message);
472 if (activeNotification.type === "question")
473 notificationButtonClick(null, approved ? 0 : 1);
474 else if (approved)
387 openNotificationLinks(); 475 openNotificationLinks();
388 prepareNotificationIconAndPopup();
389 } 476 }
390 } 477 }
391 else 478 prepareNotificationIconAndPopup();
392 prepareNotificationIconAndPopup();
393 } 479 }
394 480
395 ext.onMessage.addListener(function (msg, sender, sendResponse) 481 ext.onMessage.addListener(function (msg, sender, sendResponse)
396 { 482 {
397 switch (msg.type) 483 switch (msg.type)
398 { 484 {
399 case "get-selectors": 485 case "get-selectors":
400 var selectors = null; 486 var selectors = null;
401 487
402 if (!isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT") && 488 if (!isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT") &&
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 tab.sendMessage({type: "clickhide-deactivate"}); 578 tab.sendMessage({type: "clickhide-deactivate"});
493 refreshIconAndContextMenu(tab); 579 refreshIconAndContextMenu(tab);
494 }); 580 });
495 581
496 setTimeout(function() 582 setTimeout(function()
497 { 583 {
498 var notificationToShow = Notification.getNextToShow(); 584 var notificationToShow = Notification.getNextToShow();
499 if (notificationToShow) 585 if (notificationToShow)
500 showNotification(notificationToShow); 586 showNotification(notificationToShow);
501 }, 3 * 60 * 1000); 587 }, 3 * 60 * 1000);
OLDNEW
« no previous file with comments | « no previous file | chrome/ext/background.js » ('j') | popup.html » ('J')

Powered by Google App Engine
This is Rietveld