Refactor desktop notification override
This commit is contained in:
parent
a3bf36b31b
commit
695c091bac
80
src/browser/js/notification.js
Normal file
80
src/browser/js/notification.js
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
const OriginalNotification = Notification;
|
||||||
|
|
||||||
|
function override(eventHandlers) {
|
||||||
|
Notification = function(title, options) {
|
||||||
|
this.notification = new OriginalNotification(title, options);
|
||||||
|
if (eventHandlers.notification) {
|
||||||
|
eventHandlers.notification();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// static properties
|
||||||
|
Notification.__defineGetter__('permission', function() {
|
||||||
|
return OriginalNotification.permission;
|
||||||
|
});
|
||||||
|
|
||||||
|
// instance properties
|
||||||
|
var defineReadProperty = function(property) {
|
||||||
|
Notification.prototype.__defineGetter__(property, function() {
|
||||||
|
return this.notification[property];
|
||||||
|
});
|
||||||
|
};
|
||||||
|
defineReadProperty('title');
|
||||||
|
defineReadProperty('dir');
|
||||||
|
defineReadProperty('lang');
|
||||||
|
defineReadProperty('body');
|
||||||
|
defineReadProperty('tag');
|
||||||
|
defineReadProperty('icon');
|
||||||
|
defineReadProperty('data');
|
||||||
|
defineReadProperty('silent');
|
||||||
|
|
||||||
|
// unsupported properties
|
||||||
|
defineReadProperty('noscreen');
|
||||||
|
defineReadProperty('renotify');
|
||||||
|
defineReadProperty('sound');
|
||||||
|
defineReadProperty('sticky');
|
||||||
|
defineReadProperty('vibrate');
|
||||||
|
|
||||||
|
// event handlers
|
||||||
|
var defineEventHandler = function(event, callback) {
|
||||||
|
defineReadProperty(event);
|
||||||
|
Notification.prototype.__defineSetter__(event, function(originalCallback) {
|
||||||
|
this.notification[event] = function() {
|
||||||
|
callbackevent = {
|
||||||
|
preventDefault: function() {
|
||||||
|
this.isPrevented = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (callback) {
|
||||||
|
callback(callbackevent);
|
||||||
|
if (!callbackevent.isPrevented) {
|
||||||
|
originalCallback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
originalCallback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
defineEventHandler('onclick', eventHandlers.onclick);
|
||||||
|
defineEventHandler('onerror', eventHandlers.onerror);
|
||||||
|
|
||||||
|
// obsolete handlers
|
||||||
|
defineEventHandler('onclose', eventHandlers.onclose);
|
||||||
|
defineEventHandler('onshow', eventHandlers.onshow);
|
||||||
|
|
||||||
|
// static methods
|
||||||
|
Notification.requestPermission = function(callback) {
|
||||||
|
OriginalNotification.requestPermission(callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
// instance methods
|
||||||
|
Notification.prototype.close = function() {
|
||||||
|
this.notification.close();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
override: override
|
||||||
|
};
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
const electron = require('electron');
|
const electron = require('electron');
|
||||||
const ipc = electron.ipcRenderer;
|
const ipc = electron.ipcRenderer;
|
||||||
const NativeNotification = Notification;
|
const notification = require('../js/notification');
|
||||||
|
|
||||||
var hasClass = function(element, className) {
|
var hasClass = function(element, className) {
|
||||||
var rclass = /[\t\r\n\f]/g;
|
var rclass = /[\t\r\n\f]/g;
|
||||||
|
@ -107,35 +107,21 @@ function isLowerThanOrEqualWindows8_1() {
|
||||||
return (osVersion.major <= 6 && osVersion.minor <= 3);
|
return (osVersion.major <= 6 && osVersion.minor <= 3);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Show balloon when notified.
|
if (process.platform === 'win32' && isLowerThanOrEqualWindows8_1()) {
|
||||||
function overrideNotificationWithBalloon() {
|
// Show balloon when notified.
|
||||||
Notification = function(title, options) {
|
notification.override({
|
||||||
|
notification: function(title, options) {
|
||||||
ipc.send('notified', {
|
ipc.send('notified', {
|
||||||
title: title,
|
title: title,
|
||||||
options: options
|
options: options
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
Notification.permission = NativeNotification.permission;
|
});
|
||||||
Notification.requestPermission = function(callback) {
|
}
|
||||||
callback('granted');
|
else {
|
||||||
};
|
// Show window even if it is hidden/minimized when notification is clicked.
|
||||||
Notification.prototype.close = function() {};
|
notification.override({
|
||||||
};
|
onclick: function() {
|
||||||
|
|
||||||
// Show window even if it is hidden/minimized when notification is clicked.
|
|
||||||
function overrideNotification() {
|
|
||||||
Notification = function(title, options) {
|
|
||||||
this.notification = new NativeNotification(title, options);
|
|
||||||
};
|
|
||||||
Notification.permission = NativeNotification.permission;
|
|
||||||
Notification.requestPermission = function(callback) {
|
|
||||||
callback('granted');
|
|
||||||
};
|
|
||||||
Notification.prototype.close = function() {
|
|
||||||
this.notification.close();
|
|
||||||
};
|
|
||||||
Notification.prototype.__defineSetter__('onclick', function(callback) {
|
|
||||||
this.notification.onclick = function() {
|
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
// show() breaks Aero Snap state.
|
// show() breaks Aero Snap state.
|
||||||
electron.remote.getCurrentWindow().focus();
|
electron.remote.getCurrentWindow().focus();
|
||||||
|
@ -144,14 +130,6 @@ function overrideNotification() {
|
||||||
electron.remote.getCurrentWindow().show();
|
electron.remote.getCurrentWindow().show();
|
||||||
}
|
}
|
||||||
ipc.sendToHost('onNotificationClick');
|
ipc.sendToHost('onNotificationClick');
|
||||||
callback();
|
}
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.platform === 'win32' && isLowerThanOrEqualWindows8_1()) {
|
|
||||||
overrideNotificationWithBalloon();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
overrideNotification();
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue