From f75382d63581a8793ced32cb5f92c8a14c25b3cd Mon Sep 17 00:00:00 2001 From: Contra Date: Fri, 22 Jan 2016 10:44:42 -0800 Subject: [PATCH 1/2] add monkeypatching notification support --- app/src/main.js | 12 +++++++++--- app/src/preload.js | 26 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/src/main.js b/app/src/main.js index 915f8ac..f84be1e 100644 --- a/app/src/main.js +++ b/app/src/main.js @@ -94,6 +94,14 @@ app.on('ready', function () { }); } + mainWindow.on('notification', function (title) { + if (!isOSX() || mainWindow.isFocused()) { + return; + } + + app.dock.setBadge('●'); + }); + mainWindow.webContents.on('new-window', function (event, urlToGo) { if (linkIsInternal(appArgs.targetUrl, urlToGo)) { return; @@ -108,9 +116,7 @@ app.on('ready', function () { if (!isOSX()) { return; } - if (appArgs.badge || appArgs.counter) { - app.dock.setBadge(''); - } + app.dock.setBadge(''); }); mainWindow.on('close', (e) => { diff --git a/app/src/preload.js b/app/src/preload.js index 154a311..9da4371 100644 --- a/app/src/preload.js +++ b/app/src/preload.js @@ -4,11 +4,35 @@ var ipc = require('electron').ipcRenderer; +// monkeypatch window.Notification +hookNotify(function(title, opt){ + ipc.emit('notification', title, opt); +}); + document.addEventListener("DOMContentLoaded", function(event) { - // do things + // do things here + Notification.requestPermission().then(function(){ + new Notification('test') + }) }); ipc.on('params', function (event, message) { var appArgs = JSON.parse(message); console.log('nativefier.json', appArgs); }); + +function hookNotify(cb){ + var oldNotify = window.Notification; + var newNotify = function (title, opt) { + cb(title, opt); + return new oldNotify(title, opt); + }; + newNotify.requestPermission = oldNotify.requestPermission.bind(oldNotify); + Object.defineProperty(newNotify, 'permission', { + get: function() { + return oldNotify.permission; + } + }); + + window.Notification = newNotify; +} From 9393f863e2a143c4bd49a4455dc695422b697ff3 Mon Sep 17 00:00:00 2001 From: Contra Date: Fri, 22 Jan 2016 10:48:04 -0800 Subject: [PATCH 2/2] clean up --- app/src/preload.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/preload.js b/app/src/preload.js index 9da4371..d5a1953 100644 --- a/app/src/preload.js +++ b/app/src/preload.js @@ -10,10 +10,7 @@ hookNotify(function(title, opt){ }); document.addEventListener("DOMContentLoaded", function(event) { - // do things here - Notification.requestPermission().then(function(){ - new Notification('test') - }) + // do things }); ipc.on('params', function (event, message) {