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

Delta Between Two Patch Sets: background.js

Issue 6098518317989888: Fix: Notification popup is broken (Closed)
Left Patch Set: chrome.notification added Created Feb. 14, 2014, 4:18 p.m.
Right Patch Set: Change back activeNotification unset Created Feb. 26, 2014, 6:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 }); 281 });
282 } 282 }
283 283
284 function prepareNotificationIconAndPopup() 284 function prepareNotificationIconAndPopup()
285 { 285 {
286 activeNotification.onClicked = function() 286 activeNotification.onClicked = function()
287 { 287 {
288 iconAnimation.stop(); 288 iconAnimation.stop();
289 activeNotification = null; 289 activeNotification = null;
290 }; 290 };
291 291
292 iconAnimation.update(activeNotification.severity); 292 iconAnimation.update(activeNotification.severity);
Felix Dahlke 2014/02/27 14:10:39 Nit: Probably whitespace differences, this shouldn
293 } 293 }
294 294
295 function notificationWindowClick() { 295 function openNotificationLinks()
Thomas Greiner 2014/02/17 10:02:41 Nit: Bracket on next line
saroyanm 2014/02/18 10:25:08 Done.
296 {
296 if (activeNotification.links) 297 if (activeNotification.links)
297 { 298 {
298 activeNotification.links.forEach(function(link) 299 activeNotification.links.forEach(function(link)
299 { 300 {
300 chrome.tabs.create({ url: Utils.getDocLink(link) }); 301 ext.windows.getLastFocused(function(win)
Thomas Greiner 2014/02/17 10:02:41 This is Chrome-specific. We do have an "openTab" m
saroyanm 2014/02/18 10:25:08 Done.
301 }); 302 {
302 } 303 win.openTab(Utils.getDocLink(link));
303 prepareNotificationIconAndPopup(); 304 });
304 } 305 });
305 306 }
306 function notificationButtonClick(id, index) { 307 }
Thomas Greiner 2014/02/17 10:02:41 Nit: Bracket on next line
saroyanm 2014/02/18 10:25:08 Done.
307 chrome.tabs.create({url: Utils.getDocLink(activeNotification.links[index])}); 308
Thomas Greiner 2014/02/17 10:02:41 This is Chrome-specific. We do have an "openTab" m
saroyanm 2014/02/18 10:25:08 Done.
308 prepareNotificationIconAndPopup(); 309 function notificationButtonClick(id, index)
310 {
311 if (activeNotification.links && activeNotification.links[index])
312 {
313 ext.windows.getLastFocused(function(win)
314 {
315 win.openTab(Utils.getDocLink(activeNotification.links[index]));
316 });
317 }
309 } 318 }
310 319
311 function showNotification(notification) 320 function showNotification(notification)
312 { 321 {
313 activeNotification = notification; 322 activeNotification = notification;
314 if (activeNotification.severity === "critical") 323 if (activeNotification.severity === "critical")
315 { 324 {
316 var isWebkit = typeof webkitNotifications !== "undefined"; 325 var hasWebkitNotifications = typeof webkitNotifications !== "undefined";
Thomas Greiner 2014/02/17 10:02:41 All browsers based on this repository use WebKit s
saroyanm 2014/02/18 10:25:08 Done.
317 if (isWebkit && "createHTMLNotification" in webkitNotifications) 326 if (hasWebkitNotifications && "createHTMLNotification" in webkitNotification s)
318 { 327 {
319 notification = webkitNotifications.createHTMLNotification("notification.ht ml"); 328 var notification = webkitNotifications.createHTMLNotification("notificatio n.html");
320 notification.show(); 329 notification.show();
321 notification.addEventListener("close", prepareNotificationIconAndPopup); 330 notification.addEventListener("close", prepareNotificationIconAndPopup, fa lse);
322 return; 331 return;
323 } 332 }
324 333
325 var texts = Notification.getLocalizedTexts(notification); 334 var texts = Notification.getLocalizedTexts(notification);
326 var title = texts.title ? texts.title : ""; 335 var title = texts.title || "";
327 var message = texts.message ? texts.message : ""; 336 var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
328 var iconUrl = "icons/abp-128.png"; 337 var iconUrl = ext.getURL("icons/abp-128.png");
329 if ("notifications" in chrome) 338 var hasLinks = activeNotification.links && activeNotification.links.length > 0;
Thomas Greiner 2014/02/17 10:02:41 This is Chrome-specific but the adblockpluschrome
saroyanm 2014/02/18 10:25:08 Thanks, for pointing this, now I see what is under
Thomas Greiner 2014/02/18 14:26:17 That's OK for now. We can add a Safari-specific im
330 { 339 if ("browserNotifications" in ext)
331 var buttons = []; 340 {
332 var links = notification.links; 341 var opts = {
333 links.forEach(function(link, index){
Thomas Greiner 2014/02/17 10:02:41 Nit: Missing space after closing bracket.
334 buttons.push({"title": link.replace(new RegExp("_", 'g'), " ")});
Thomas Greiner 2014/02/17 10:02:41 The link texts can actually be extracted from the
saroyanm 2014/02/18 10:25:08 Maybe it make sense also to remove anchors with th
335 });
336 var opt = {
337 type: "basic", 342 type: "basic",
338 title: title, 343 title: title,
339 message: message, 344 message: message,
340 iconUrl: iconUrl, 345 iconUrl: iconUrl,
Thomas Greiner 2014/02/17 10:02:41 You can directly inline "title", "message" and "ic
Thomas Greiner 2014/02/17 10:02:41 Use |ext.getURL("icons/abp-128.png")| as the icon
saroyanm 2014/02/18 10:25:08 while I've added also else case I'm using that var
Thomas Greiner 2014/02/18 14:26:17 Right, that's fine then. Although you could write
341 buttons: buttons 346 buttons: []
342 }; 347 };
343 notification = chrome.notifications; 348 var regex = /<a>(.*?)<\/a>/g;
Thomas Greiner 2014/02/17 10:02:41 Avoid creating global variables by adding "var".
saroyanm 2014/02/18 10:25:08 Done.
344 notification.create("", opt, function(){}); 349 var plainMessage = texts.message || "";
Thomas Greiner 2014/02/17 10:02:41 Nit: Missing space after closing bracket.
saroyanm 2014/02/18 10:25:08 Done.
350 var match;
351 while (match = regex.exec(plainMessage))
352 opts.buttons.push({title: match[1]});
353
354 var notification = ext.browserNotifications;
355 notification.create("", opts, function() {});
345 notification.onClosed.addListener(prepareNotificationIconAndPopup); 356 notification.onClosed.addListener(prepareNotificationIconAndPopup);
346 notification.onClicked.addListener(notificationWindowClick);
347 notification.onButtonClicked.addListener(notificationButtonClick); 357 notification.onButtonClicked.addListener(notificationButtonClick);
348 } 358 }
349 else if (isWebkit && "createNotification" in webkitNotifications) 359 else if (hasWebkitNotifications && "createNotification" in webkitNotificatio ns)
350 { 360 {
351 notification = webkitNotifications.createNotification(iconUrl, title, mess age); 361 if (hasLinks)
362 message += " " + ext.i18n.getMessage("notification_without_buttons");
363
364 var notification = webkitNotifications.createNotification(iconUrl, title, message);
352 notification.show(); 365 notification.show();
353 notification.addEventListener("close", prepareNotificationIconAndPopup); 366 notification.addEventListener("close", prepareNotificationIconAndPopup, fa lse);
Thomas Greiner 2014/02/17 10:02:41 Nit: Add third parameter
saroyanm 2014/02/18 10:25:08 Done.
354 notification.addEventListener("click", notificationWindowClick); 367 notification.addEventListener("click", openNotificationLinks, false);
Thomas Greiner 2014/02/17 10:02:41 Nit: Add third parameter
saroyanm 2014/02/18 10:25:08 Done.
368 }
369 else
370 {
371 var message = title + "\n" + message;
372 if (hasLinks)
373 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
374
375 if (confirm(message))
Thomas Greiner 2014/02/27 10:07:32 Nit: Change to… if (confirm(message)) openNotif
376 {
377 openNotificationLinks();
378 prepareNotificationIconAndPopup();
379 }
380 else
381 prepareNotificationIconAndPopup();
355 } 382 }
Thomas Greiner 2014/02/17 10:02:41 What about the "else" case here?
saroyanm 2014/02/18 10:25:08 Added also confirm dialog for else case, thanks fo
356 } 383 }
357 else 384 else
358 prepareNotificationIconAndPopup(); 385 prepareNotificationIconAndPopup();
359 } 386 }
360 387
361 ext.onMessage.addListener(function (msg, sender, sendResponse) 388 ext.onMessage.addListener(function (msg, sender, sendResponse)
362 { 389 {
363 switch (msg.type) 390 switch (msg.type)
364 { 391 {
365 case "get-selectors": 392 case "get-selectors":
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 tab.sendMessage({type: "clickhide-deactivate"}); 497 tab.sendMessage({type: "clickhide-deactivate"});
471 refreshIconAndContextMenu(tab); 498 refreshIconAndContextMenu(tab);
472 }); 499 });
473 500
474 setTimeout(function() 501 setTimeout(function()
475 { 502 {
476 var notificationToShow = Notification.getNextToShow(); 503 var notificationToShow = Notification.getNextToShow();
477 if (notificationToShow) 504 if (notificationToShow)
478 showNotification(notificationToShow); 505 showNotification(notificationToShow);
479 }, 3 * 60 * 1000); 506 }, 3 * 60 * 1000);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld