Fix notifications on chrome

Chrome has removed the webkitNotifications API and not only has the w3c
web notifications API. This adds a shim when webkitNotifications is
missing but Notification is present.

(imported from commit e21c476f9ae6570c297c88bd6ff90a97818688e6)
This commit is contained in:
Jason Michalski 2014-05-28 20:00:45 -07:00
parent 8c72eddb72
commit cb153967ec
1 changed files with 30 additions and 6 deletions

View File

@ -17,12 +17,36 @@ var current_favicon;
var previous_favicon; var previous_favicon;
var flashing = false; var flashing = false;
var notifications_api;
if (window.webkitNotifications) {
notifications_api = window.webkitNotifications;
} else if (window.Notification) {
// Build a shim to the new notification API
notifications_api = {
checkPermission: function checkPermission() {
if (window.Notification.permission === 'granted') {
return 0;
} else {
return 2;
}
},
requestPermission: window.Notification.requestPermission,
createNotification: function createNotification(icon, title, content) {
var notification_object = new window.Notification(title, {icon: icon, body: content});
notification_object.show = function () {};
notification_object.cancel = function () { notification_object.close(); };
return notification_object;
}
};
}
function browser_desktop_notifications_on () { function browser_desktop_notifications_on () {
return (window.webkitNotifications && return (notifications_api &&
// Firefox on Ubuntu claims to do webkitNotifications but its notifications are terrible // Firefox on Ubuntu claims to do webkitNotifications but its notifications are terrible
$.browser.webkit && $.browser.webkit &&
// 0 is PERMISSION_ALLOWED // 0 is PERMISSION_ALLOWED
window.webkitNotifications.checkPermission() === 0) || notifications_api.checkPermission() === 0) ||
// window.bridge is the desktop client // window.bridge is the desktop client
(window.bridge !== undefined); (window.bridge !== undefined);
} }
@ -80,13 +104,13 @@ exports.initialize = function () {
} }
} }
if (window.webkitNotifications) { if (notifications_api) {
$(document).click(function () { $(document).click(function () {
if (!page_params.desktop_notifications_enabled || asked_permission_already) { if (!page_params.desktop_notifications_enabled || asked_permission_already) {
return; return;
} }
if (window.webkitNotifications.checkPermission() !== 0) { // 0 is PERMISSION_ALLOWED if (notifications_api.checkPermission() !== 0) { // 0 is PERMISSION_ALLOWED
window.webkitNotifications.requestPermission(function () {}); notifications_api.requestPermission(function () {});
asked_permission_already = true; asked_permission_already = true;
} }
}); });
@ -280,7 +304,7 @@ function process_notification(notification) {
if (window.bridge === undefined && notification.webkit_notify === true) { if (window.bridge === undefined && notification.webkit_notify === true) {
var icon_url = ui.small_avatar_url(message); var icon_url = ui.small_avatar_url(message);
notice_memory[key] = { notice_memory[key] = {
obj: window.webkitNotifications.createNotification( obj: notifications_api.createNotification(
icon_url, title, content), icon_url, title, content),
msg_count: msg_count, msg_count: msg_count,
message_id: message.id message_id: message.id