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 March 21, 2014, 1:29 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') | no next file with comments »
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"))
31 { 32 {
32 this.isWhitelisted = isWhitelisted; 33 this.isWhitelisted = isWhitelisted;
33 this.isFrameWhitelisted = isFrameWhitelisted; 34 this.isFrameWhitelisted = isFrameWhitelisted;
34 this.processKeyException = processKeyException; 35 this.processKeyException = processKeyException;
35 } 36 }
36 var FilterStorage = require("filterStorage").FilterStorage; 37 var FilterStorage = require("filterStorage").FilterStorage;
37 var ElemHide = require("elemHide").ElemHide; 38 var ElemHide = require("elemHide").ElemHide;
38 var defaultMatcher = require("matcher").defaultMatcher; 39 var defaultMatcher = require("matcher").defaultMatcher;
39 var Prefs = require("prefs").Prefs; 40 var Prefs = require("prefs").Prefs;
40 var Synchronizer = require("synchronizer").Synchronizer; 41 var Synchronizer = require("synchronizer").Synchronizer;
41 var Utils = require("utils").Utils; 42 var Utils = require("utils").Utils;
42 var Notification = require("notification").Notification; 43 var Notification = require("notification").Notification;
44 var initAntiAdblockNotification = require("antiadblockInit").initAntiAdblockNoti fication;
43 45
44 // Some types cannot be distinguished 46 // Some types cannot be distinguished
45 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; 47 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT;
46 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER; 48 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER;
47 49
48 var isFirstRun = false; 50 var isFirstRun = false;
49 var seenDataCorruption = false; 51 var seenDataCorruption = false;
50 require("filterNotifier").FilterNotifier.addListener(function(action) 52 require("filterNotifier").FilterNotifier.addListener(function(action)
51 { 53 {
52 if (action == "load") 54 if (action == "load")
53 { 55 {
54 var importingOldData = importOldData(); 56 var importingOldData = importOldData();
55 57
56 var addonVersion = require("info").addonVersion; 58 var addonVersion = require("info").addonVersion;
57 var prevVersion = localStorage.currentVersion; 59 var prevVersion = localStorage.currentVersion;
58 if (prevVersion != addonVersion) 60 if (prevVersion != addonVersion)
59 { 61 {
60 isFirstRun = !prevVersion; 62 isFirstRun = !prevVersion;
61 localStorage.currentVersion = addonVersion; 63 localStorage.currentVersion = addonVersion;
62 if (!importingOldData) 64 if (!importingOldData)
63 addSubscription(prevVersion); 65 addSubscription(prevVersion);
64 } 66 }
67
68 initAntiAdblockNotification();
65 } 69 }
66 70
67 // update browser actions when whitelisting might have changed, 71 // update browser actions when whitelisting might have changed,
68 // due to loading filters or saving filter changes 72 // due to loading filters or saving filter changes
69 if (action == "load" || action == "save") 73 if (action == "load" || action == "save")
70 { 74 {
71 ext.windows.getAll(function(windows) 75 ext.windows.getAll(function(windows)
72 { 76 {
73 for (var i = 0; i < windows.length; i++) 77 for (var i = 0; i < windows.length; i++)
74 { 78 {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 { 198 {
195 subscription.title = "Allow non-intrusive advertising"; 199 subscription.title = "Allow non-intrusive advertising";
196 FilterStorage.addSubscription(subscription); 200 FilterStorage.addSubscription(subscription);
197 if (subscription instanceof DownloadableSubscription && !subscription.last Download) 201 if (subscription instanceof DownloadableSubscription && !subscription.last Download)
198 Synchronizer.execute(subscription); 202 Synchronizer.execute(subscription);
199 } 203 }
200 else 204 else
201 addAcceptable = false; 205 addAcceptable = false;
202 } 206 }
203 207
208 // Add "anti-adblock messages" subscription
209 var subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
210 if (subscription)
211 {
212 subscription.disabled = true;
213 FilterStorage.addSubscription(subscription);
214 if (subscription instanceof DownloadableSubscription && !subscription.lastDo wnload)
215 Synchronizer.execute(subscription);
216 }
217
204 if (!addSubscription && !addAcceptable) 218 if (!addSubscription && !addAcceptable)
205 return; 219 return;
206 220
207 function notifyUser() 221 function notifyUser()
208 { 222 {
209 ext.windows.getLastFocused(function(win) 223 ext.windows.getLastFocused(function(win)
210 { 224 {
211 win.openTab(ext.getURL("firstRun.html")); 225 win.openTab(ext.getURL("firstRun.html"));
212 }); 226 });
213 } 227 }
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 win.openTab(optionsUrl, callback && function(tab) 302 win.openTab(optionsUrl, callback && function(tab)
289 { 303 {
290 tab.onCompleted.addListener(callback); 304 tab.onCompleted.addListener(callback);
291 }); 305 });
292 }); 306 });
293 }); 307 });
294 } 308 }
295 309
296 function prepareNotificationIconAndPopup() 310 function prepareNotificationIconAndPopup()
297 { 311 {
312 var animateIcon = (activeNotification.type !== "question");
298 activeNotification.onClicked = function() 313 activeNotification.onClicked = function()
299 { 314 {
300 iconAnimation.stop(); 315 if (animateIcon)
316 iconAnimation.stop();
301 activeNotification = null; 317 activeNotification = null;
302 }; 318 };
303 iconAnimation.update(activeNotification.severity); 319 if (animateIcon)
320 iconAnimation.update(activeNotification.type);
304 } 321 }
305 322
306 function openNotificationLinks() 323 function openNotificationLinks()
307 { 324 {
308 if (activeNotification.links) 325 if (activeNotification.links)
309 { 326 {
310 activeNotification.links.forEach(function(link) 327 activeNotification.links.forEach(function(link)
311 { 328 {
312 ext.windows.getLastFocused(function(win) 329 ext.windows.getLastFocused(function(win)
313 { 330 {
314 win.openTab(Utils.getDocLink(link)); 331 win.openTab(Utils.getDocLink(link));
315 }); 332 });
316 }); 333 });
317 } 334 }
318 } 335 }
319 336
320 function notificationButtonClick(id, index) 337 function notificationButtonClick(id, index)
321 { 338 {
322 if (activeNotification.links && activeNotification.links[index]) 339 if (activeNotification.type === "question")
340 {
341 Notification.triggerQuestionListeners(activeNotification.id, index === 0);
342 Notification.markAsShown(activeNotification.id);
343 activeNotification.onClicked();
344 }
345 else if (activeNotification.links && activeNotification.links[index])
323 { 346 {
324 ext.windows.getLastFocused(function(win) 347 ext.windows.getLastFocused(function(win)
325 { 348 {
326 win.openTab(Utils.getDocLink(activeNotification.links[index])); 349 win.openTab(Utils.getDocLink(activeNotification.links[index]));
327 }); 350 });
328 } 351 }
329 } 352 }
330 353
331 function showNotification(notification) 354 function showNotification(notification)
332 { 355 {
356 if (activeNotification && activeNotification.id === notification.id)
357 return;
358
333 activeNotification = notification; 359 activeNotification = notification;
334 if (activeNotification.severity === "critical") 360 if (activeNotification.type === "critical" || activeNotification.type === "que stion")
335 { 361 {
336 var hasWebkitNotifications = typeof webkitNotifications !== "undefined"; 362 var hasWebkitNotifications = typeof webkitNotifications !== "undefined";
337 if (hasWebkitNotifications && "createHTMLNotification" in webkitNotification s) 363 if (hasWebkitNotifications && "createHTMLNotification" in webkitNotification s)
338 { 364 {
339 var notification = webkitNotifications.createHTMLNotification("notificatio n.html"); 365 var notification = webkitNotifications.createHTMLNotification("notificatio n.html");
340 notification.show(); 366 notification.show();
341 notification.addEventListener("close", prepareNotificationIconAndPopup, fa lse); 367 prepareNotificationIconAndPopup();
342 return; 368 return;
343 } 369 }
344 370
345 var texts = Notification.getLocalizedTexts(notification); 371 var texts = Notification.getLocalizedTexts(notification);
346 var title = texts.title || ""; 372 var title = texts.title || "";
347 var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 373 var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
348 var iconUrl = ext.getURL("icons/abp-128.png"); 374 var iconUrl = ext.getURL("icons/abp-128.png");
349 var hasLinks = activeNotification.links && activeNotification.links.length > 0; 375 var hasLinks = activeNotification.links && activeNotification.links.length > 0;
350 if ("browserNotifications" in ext) 376
377 // Chrome on Linux does not fully support chrome.notifications yet
378 // https://code.google.com/p/chromium/issues/detail?id=291485
379 if (chrome && "notifications" in chrome && navigator.platform.indexOf("Linux ") === -1)
351 { 380 {
352 var opts = { 381 var opts = {
353 type: "basic", 382 type: "basic",
354 title: title, 383 title: title,
355 message: message, 384 message: message,
356 iconUrl: iconUrl, 385 iconUrl: iconUrl,
357 buttons: [] 386 buttons: [],
387 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
358 }; 388 };
359 var regex = /<a>(.*?)<\/a>/g; 389 if (activeNotification.type === "question")
360 var plainMessage = texts.message || ""; 390 {
361 var match; 391 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_yes")});
362 while (match = regex.exec(plainMessage)) 392 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_no")});
363 opts.buttons.push({title: match[1]}); 393 }
394 else
395 {
396 var regex = /<a>(.*?)<\/a>/g;
397 var plainMessage = texts.message || "";
398 var match;
399 while (match = regex.exec(plainMessage))
400 opts.buttons.push({title: match[1]});
401 }
364 402
365 var notification = ext.browserNotifications; 403 chrome.notifications.create("", opts, function() {});
366 notification.create("", opts, function() {}); 404 chrome.notifications.onButtonClicked.addListener(notificationButtonClick);
367 notification.onClosed.addListener(prepareNotificationIconAndPopup);
368 notification.onButtonClicked.addListener(notificationButtonClick);
369 } 405 }
370 else if (hasWebkitNotifications && "createNotification" in webkitNotificatio ns) 406 else if (hasWebkitNotifications && "createNotification" in webkitNotificatio ns && activeNotification.type !== "question")
371 { 407 {
372 if (hasLinks) 408 if (hasLinks)
373 message += " " + ext.i18n.getMessage("notification_without_buttons"); 409 message += " " + ext.i18n.getMessage("notification_without_buttons");
374 410
375 var notification = webkitNotifications.createNotification(iconUrl, title, message); 411 var notification = webkitNotifications.createNotification(iconUrl, title, message);
376 notification.show(); 412 notification.show();
377 notification.addEventListener("close", prepareNotificationIconAndPopup, fa lse);
378 notification.addEventListener("click", openNotificationLinks, false); 413 notification.addEventListener("click", openNotificationLinks, false);
379 } 414 }
380 else 415 else
381 { 416 {
382 var message = title + "\n" + message; 417 var message = title + "\n" + message;
383 if (hasLinks) 418 if (hasLinks)
384 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); 419 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
385 420
386 if (confirm(message)) 421 var approved = confirm(message);
422 if (activeNotification.type === "question")
423 notificationButtonClick(null, approved ? 0 : 1);
424 else if (approved)
387 openNotificationLinks(); 425 openNotificationLinks();
388 prepareNotificationIconAndPopup();
389 } 426 }
390 } 427 }
391 else 428 prepareNotificationIconAndPopup();
392 prepareNotificationIconAndPopup();
393 } 429 }
394 430
395 ext.onMessage.addListener(function (msg, sender, sendResponse) 431 ext.onMessage.addListener(function (msg, sender, sendResponse)
396 { 432 {
397 switch (msg.type) 433 switch (msg.type)
398 { 434 {
399 case "get-selectors": 435 case "get-selectors":
400 var selectors = null; 436 var selectors = null;
401 437
402 if (!isFrameWhitelisted(sender.tab, sender.frame, "DOCUMENT") && 438 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"}); 528 tab.sendMessage({type: "clickhide-deactivate"});
493 refreshIconAndContextMenu(tab); 529 refreshIconAndContextMenu(tab);
494 }); 530 });
495 531
496 setTimeout(function() 532 setTimeout(function()
497 { 533 {
498 var notificationToShow = Notification.getNextToShow(); 534 var notificationToShow = Notification.getNextToShow();
499 if (notificationToShow) 535 if (notificationToShow)
500 showNotification(notificationToShow); 536 showNotification(notificationToShow);
501 }, 3 * 60 * 1000); 537 }, 3 * 60 * 1000);
OLDNEW
« no previous file with comments | « no previous file | chrome/ext/background.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld