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: moved code from /extcommon to ext/background Created Feb. 19, 2014, 1:09 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 openNotificationLinks() 295 function openNotificationLinks()
296 { 296 {
297 if (activeNotification.links) 297 if (activeNotification.links)
298 { 298 {
299 activeNotification.links.forEach(function(link) 299 activeNotification.links.forEach(function(link)
300 { 300 {
301 ext.windows.getLastFocused(function(win) 301 ext.windows.getLastFocused(function(win)
302 { 302 {
303 win.openTab(Utils.getDocLink(link)); 303 win.openTab(Utils.getDocLink(link));
304 }); 304 });
305 }); 305 });
306 } 306 }
307 prepareNotificationIconAndPopup();
308 } 307 }
309 308
310 function notificationButtonClick(id, index) 309 function notificationButtonClick(id, index)
311 { 310 {
312 if (activeNotification.links && activeNotification.links[index]) 311 if (activeNotification.links && activeNotification.links[index])
313 { 312 {
314 ext.windows.getLastFocused(function(win) 313 ext.windows.getLastFocused(function(win)
315 { 314 {
316 win.openTab(Utils.getDocLink(activeNotification.links[index])); 315 win.openTab(Utils.getDocLink(activeNotification.links[index]));
317 }); 316 });
318 } 317 }
319 prepareNotificationIconAndPopup();
320 } 318 }
321 319
322 function showNotification(notification) 320 function showNotification(notification)
323 { 321 {
324 activeNotification = notification; 322 activeNotification = notification;
325 if (activeNotification.severity === "critical") 323 if (activeNotification.severity === "critical")
326 { 324 {
327 var hasWebkitNotifications = typeof webkitNotifications !== "undefined"; 325 var hasWebkitNotifications = typeof webkitNotifications !== "undefined";
328 if (hasWebkitNotifications && "createHTMLNotification" in webkitNotification s) 326 if (hasWebkitNotifications && "createHTMLNotification" in webkitNotification s)
329 { 327 {
330 var notification = webkitNotifications.createHTMLNotification("notificatio n.html"); 328 var notification = webkitNotifications.createHTMLNotification("notificatio n.html");
331 notification.show(); 329 notification.show();
332 notification.addEventListener("close", prepareNotificationIconAndPopup, fa lse); 330 notification.addEventListener("close", prepareNotificationIconAndPopup, fa lse);
333 return; 331 return;
334 } 332 }
335 333
336 var texts = Notification.getLocalizedTexts(notification); 334 var texts = Notification.getLocalizedTexts(notification);
337 var title = texts.title ? texts.title : ""; 335 var title = texts.title || "";
Felix Dahlke 2014/02/21 09:48:49 How about: var title = texts.title || "";
saroyanm 2014/02/21 13:39:22 Looks cool :)
338 var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 336 var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
339 var iconUrl = ext.getURL("icons/abp-128.png"); 337 var iconUrl = ext.getURL("icons/abp-128.png");
340 if ("notifications" in ext) 338 var hasLinks = activeNotification.links && activeNotification.links.length > 0;
Felix Dahlke 2014/02/21 09:48:49 I personally find it a bit confusing to have ext.n
Thomas Greiner 2014/02/21 09:53:43 Since we want to look into a Safari specific imple
Felix Dahlke 2014/02/21 09:56:22 My point is basically that, in the current stage,
Felix Dahlke 2014/02/21 10:04:01 Well, I don't have that strong feelings about this
saroyanm 2014/02/21 13:39:22 This looks a bit tricky, Anyway if we could move a
Thomas Greiner 2014/02/21 13:59:55 Large parts of the notifications code is also shar
saroyanm 2014/02/21 15:11:52 Thanks for pointing this, maybe I'll have some que
341 { 339 if ("browserNotifications" in ext)
342 var opt = { 340 {
Felix Dahlke 2014/02/21 09:48:49 Shouldn't it be plural "opts"?
saroyanm 2014/02/21 13:39:22 Done.
341 var opts = {
343 type: "basic", 342 type: "basic",
344 title: title, 343 title: title,
345 message: message, 344 message: message,
346 iconUrl: iconUrl, 345 iconUrl: iconUrl,
347 buttons: [] 346 buttons: []
348 }; 347 };
349 var regex = /<a>(.*?)<\/a>/g; 348 var regex = /<a>(.*?)<\/a>/g;
350 var plainMessage = texts.message ? texts.message : ""; 349 var plainMessage = texts.message || "";
351 while (regex.test(plainMessage)) 350 var match;
352 opt.buttons.push({title: RegExp.$1}); 351 while (match = regex.exec(plainMessage))
Felix Dahlke 2014/02/21 09:48:49 RegExp.$n is deprecated (https://developer.mozilla
saroyanm 2014/02/21 13:39:22 Good point. I've made performance test between th
Felix Dahlke 2014/02/21 13:48:20 A single match call would suffice in the first exa
352 opts.buttons.push({title: match[1]});
353 353
354 var notification = ext.notifications; 354 var notification = ext.browserNotifications;
355 notification.create("", opt, function() {}); 355 notification.create("", opts, function() {});
356 notification.onClosed.addListener(prepareNotificationIconAndPopup); 356 notification.onClosed.addListener(prepareNotificationIconAndPopup);
357 notification.onButtonClicked.addListener(notificationButtonClick); 357 notification.onButtonClicked.addListener(notificationButtonClick);
358 } 358 }
359 else if (hasWebkitNotifications && "createNotification" in webkitNotificatio ns) 359 else if (hasWebkitNotifications && "createNotification" in webkitNotificatio ns)
360 { 360 {
361 if (hasLinks)
362 message += " " + ext.i18n.getMessage("notification_without_buttons");
363
361 var notification = webkitNotifications.createNotification(iconUrl, title, message); 364 var notification = webkitNotifications.createNotification(iconUrl, title, message);
362 notification.show(); 365 notification.show();
363 notification.addEventListener("close", prepareNotificationIconAndPopup, fa lse); 366 notification.addEventListener("close", prepareNotificationIconAndPopup, fa lse);
364 notification.addEventListener("click", openNotificationLinks, false); 367 notification.addEventListener("click", openNotificationLinks, false);
365 } 368 }
366 else 369 else
367 { 370 {
368 var notification = confirm(title + "\n" + message); 371 var message = title + "\n" + message;
Felix Dahlke 2014/02/21 09:48:49 I think we should add some explanatory text here f
saroyanm 2014/02/21 13:39:22 Good point.
369 if (notification == true) 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 {
370 openNotificationLinks(); 377 openNotificationLinks();
378 prepareNotificationIconAndPopup();
379 }
380 else
381 prepareNotificationIconAndPopup();
371 } 382 }
372 } 383 }
373 else 384 else
374 prepareNotificationIconAndPopup(); 385 prepareNotificationIconAndPopup();
375 } 386 }
376 387
377 ext.onMessage.addListener(function (msg, sender, sendResponse) 388 ext.onMessage.addListener(function (msg, sender, sendResponse)
378 { 389 {
379 switch (msg.type) 390 switch (msg.type)
380 { 391 {
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 tab.sendMessage({type: "clickhide-deactivate"}); 497 tab.sendMessage({type: "clickhide-deactivate"});
487 refreshIconAndContextMenu(tab); 498 refreshIconAndContextMenu(tab);
488 }); 499 });
489 500
490 setTimeout(function() 501 setTimeout(function()
491 { 502 {
492 var notificationToShow = Notification.getNextToShow(); 503 var notificationToShow = Notification.getNextToShow();
493 if (notificationToShow) 504 if (notificationToShow)
494 showNotification(notificationToShow); 505 showNotification(notificationToShow);
495 }, 3 * 60 * 1000); 506 }, 3 * 60 * 1000);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld