diff --git a/src/browser/js/notification.js b/src/browser/js/notification.js index f5fe8515..30ff5097 100644 --- a/src/browser/js/notification.js +++ b/src/browser/js/notification.js @@ -1,7 +1,7 @@ 'use strict'; const OriginalNotification = Notification; -const {remote} = require('electron'); +const {ipcRenderer, remote} = require('electron'); const {throttle} = require('underscore'); const osVersion = require('../../common/osVersion'); const dingDataURL = require('../../assets/ding.mp3'); // https://github.com/mattermost/platform/blob/v3.7.3/webapp/images/ding.mp3 @@ -13,8 +13,8 @@ const playDing = throttle(() => { ding.play(); }, 3000, {trailing: false}); -function override(eventHandlers) { - Notification = function constructor(title, options) { // eslint-disable-line no-global-assign, no-native-reassign +class EnhancedNotification extends OriginalNotification { + constructor(title, options) { if (process.platform === 'win32') { // Replace with application icon. options.icon = appIconURL; @@ -22,84 +22,46 @@ function override(eventHandlers) { // Notification Center shows app's icon, so there were two icons on the notification. Reflect.deleteProperty(options, 'icon'); } - this.notification = new OriginalNotification(title, options); - if (eventHandlers.notification) { - eventHandlers.notification(title, options); - } + + super(title, options); + + ipcRenderer.send('notified', { + title, + options + }); if (process.platform === 'win32' && osVersion.isLowerThanOrEqualWindows8_1()) { if (!options.silent) { playDing(); } } - }; - - // static properties - Notification.__defineGetter__('permission', () => { - return OriginalNotification.permission; - }); - - // instance properties - function defineReadProperty(property) { - Notification.prototype.__defineGetter__(property, function getter() { - 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 - function defineEventHandler(event, callback) { - defineReadProperty(event); - Notification.prototype.__defineSetter__(event, function setter(originalCallback) { - this.notification[event] = () => { - const callbackevent = { - preventDefault() { - this.isPrevented = true; - } - }; - if (callback) { - callback(callbackevent); - if (!callbackevent.isPrevented) { - originalCallback(); - } + set onclick(handler) { + super.onclick = () => { + const currentWindow = remote.getCurrentWindow(); + if (process.platform === 'win32') { + // show() breaks Aero Snap state. + if (currentWindow.isVisible()) { + currentWindow.focus(); + } else if (currentWindow.isMinimized()) { + currentWindow.restore(); } else { - originalCallback(); + currentWindow.show(); } - }; - }); + } else if (currentWindow.isMinimized()) { + currentWindow.restore(); + } else { + currentWindow.show(); + } + ipcRenderer.sendToHost('onNotificationClick'); + handler(); + }; } - defineEventHandler('onclick', eventHandlers.onclick); - defineEventHandler('onerror', eventHandlers.onerror); - // obsolete handlers - defineEventHandler('onclose', eventHandlers.onclose); - defineEventHandler('onshow', eventHandlers.onshow); - - // static methods - Notification.requestPermission = (callback) => { - OriginalNotification.requestPermission(callback); - }; - - // instance methods - Notification.prototype.close = function close() { - this.notification.close(); - }; + get onclick() { + return super.onclick; + } } -module.exports = { - override -}; +module.exports = EnhancedNotification; diff --git a/src/browser/webview/mattermost.js b/src/browser/webview/mattermost.js index ef4d59ce..a2caa138 100644 --- a/src/browser/webview/mattermost.js +++ b/src/browser/webview/mattermost.js @@ -3,7 +3,9 @@ const electron = require('electron'); const ipc = electron.ipcRenderer; const webFrame = electron.webFrame; -const notification = require('../js/notification'); +const EnhancedNotification = require('../js/notification'); + +Notification = EnhancedNotification; // eslint-disable-line no-global-assign, no-native-reassign Reflect.deleteProperty(global.Buffer); // http://electron.atom.io/docs/tutorial/security/#buffer-global @@ -117,37 +119,6 @@ function isElementVisible(elem) { return elem.offsetHeight !== 0; } -notification.override({ - - // Send a notification event to the main process. - notification(title, options) { - ipc.send('notified', { - title, - options - }); - }, - - // Show window even if it is hidden/minimized when notification is clicked. - onclick() { - const currentWindow = electron.remote.getCurrentWindow(); - if (process.platform === 'win32') { - // show() breaks Aero Snap state. - if (currentWindow.isVisible()) { - currentWindow.focus(); - } else if (currentWindow.isMinimized()) { - currentWindow.restore(); - } else { - currentWindow.show(); - } - } else if (currentWindow.isMinimized()) { - currentWindow.restore(); - } else { - currentWindow.show(); - } - ipc.sendToHost('onNotificationClick'); - } -}); - function resetMisspelledState() { ipc.once('spellchecker-is-ready', () => { const element = document.activeElement;