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

Unified Diff: background.js

Issue 6098518317989888: Fix: Notification popup is broken (Closed)
Patch Set: Changes after Thomas notes Created Feb. 18, 2014, 10:13 a.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/common.js » ('j') | chrome/ext/common.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: background.js
===================================================================
--- a/background.js
+++ b/background.js
@@ -292,16 +292,95 @@
iconAnimation.update(activeNotification.severity);
}
+function openNotificationLinks()
+{
+ if (activeNotification.links)
+ {
+ activeNotification.links.forEach(function(link)
+ {
+ ext.windows.getLastFocused(function(win)
+ {
+ win.openTab(Utils.getDocLink(link));
+ });
+ });
+ }
+ prepareNotificationIconAndPopup();
+}
+
+function notificationButtonClick(id, index)
+{
+ if (activeNotification.links && activeNotification.links[index])
+ {
+ ext.windows.getLastFocused(function(win)
+ {
+ win.openTab(Utils.getDocLink(activeNotification.links[index]));
+ });
+ }
+ prepareNotificationIconAndPopup();
+}
+
+function getNotificationLinkTitles(text)
Thomas Greiner 2014/02/18 14:26:17 You can simply do: var titles = []; var regex = /
saroyanm 2014/02/18 16:20:28 I like this :)
+{
+ var titles = [];
+ var text = text;
+ while (text)
+ {
+ var match = /<a[^>]*>(.*?)<\/a>(.*)/.exec(text);
+ if(!match)
Thomas Greiner 2014/02/18 14:26:17 Nit: Missing space between "if" and opening bracke
saroyanm 2014/02/18 16:20:28 I have to be more attentive, I thought I fixed thi
+ return titles;
+ titles.push({"title": match[1]});
Thomas Greiner 2014/02/18 14:26:17 That's inconsistent. The function's name suggests
saroyanm 2014/02/18 16:20:28 Done.
+ text = match[2];
+ }
+ return titles;
+}
+
function showNotification(notification)
{
activeNotification = notification;
-
- if (activeNotification.severity === "critical"
- && typeof webkitNotifications !== "undefined")
+ if (activeNotification.severity === "critical")
{
- var notification = webkitNotifications.createHTMLNotification("notification.html");
- notification.show();
- notification.addEventListener("close", prepareNotificationIconAndPopup);
+ var hasWebkitNotifications = typeof webkitNotifications !== "undefined";
+ if (hasWebkitNotifications && "createHTMLNotification" in webkitNotifications)
+ {
+ var notification = webkitNotifications.createHTMLNotification("notification.html");
+ notification.show();
+ notification.addEventListener("close", prepareNotificationIconAndPopup, false);
+ return;
+ }
+
+ var texts = Notification.getLocalizedTexts(notification);
+ var title = texts.title ? texts.title : "";
+ var message = texts.message ? texts.message : "";
+ var iconUrl = ext.getURL("icons/abp-128.png");
+ if ("notifications" in ext)
+ {
+ var buttons = getNotificationLinkTitles(message);
Thomas Greiner 2014/02/18 14:26:17 There's no need for a variable here if you only us
saroyanm 2014/02/18 16:20:28 Done.
+ var opt = {
+ type: "basic",
+ title: title,
+ message: message,
Thomas Greiner 2014/02/18 14:26:17 HTML tags need to be stripped from message.
saroyanm 2014/02/18 16:20:28 Done.
+ iconUrl: iconUrl,
+ buttons: buttons
+ };
+ var notification = ext.notifications;
+ notification.create("", opt, function() {});
+ notification.onClosed.addListener(prepareNotificationIconAndPopup);
+ notification.onClicked.addListener(openNotificationLinks);
Thomas Greiner 2014/02/18 14:26:17 We don't need this anymore if there's a button for
saroyanm 2014/02/18 16:20:28 Done.
+ notification.onButtonClicked.addListener(notificationButtonClick);
+ }
+ else if (hasWebkitNotifications && "createNotification" in webkitNotifications)
+ {
+ var notification = webkitNotifications.createNotification(iconUrl, title, message);
+ notification.show();
+ notification.addEventListener("close", prepareNotificationIconAndPopup, false);
+ notification.addEventListener("click", openNotificationLinks, false);
+ }
+ else
+ {
+ var notification = confirm(title+"\n"+message);
Thomas Greiner 2014/02/18 14:26:17 Nit: Missing spaces. |confirm(title + "\n" + messa
saroyanm 2014/02/18 16:20:28 Done.
+ if (notification == true)
+ openNotificationLinks();
+ }
}
else
prepareNotificationIconAndPopup();
« no previous file with comments | « no previous file | chrome/ext/common.js » ('j') | chrome/ext/common.js » ('J')

Powered by Google App Engine
This is Rietveld