From 07acbced3eedf5e691e372fafdf1884d1064991b Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Sat, 23 Jan 2016 13:32:20 +0800 Subject: [PATCH] Cleanup notification integration #360 --- app/src/main.js | 6 +++--- app/src/preload.js | 40 ++++++++++++++++++++++------------------ src/buildApp.js | 6 ++---- src/cli.js | 2 -- src/options.js | 2 -- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/app/src/main.js b/app/src/main.js index f84be1e..664a237 100644 --- a/app/src/main.js +++ b/app/src/main.js @@ -73,7 +73,7 @@ app.on('ready', function () { mainWindow.webContents.send('params', JSON.stringify(appArgs)); }); - if (appArgs.badge || appArgs.counter) { + if (appArgs.counter) { mainWindow.on('page-title-updated', function () { if (!isOSX() || mainWindow.isFocused()) { @@ -94,7 +94,7 @@ app.on('ready', function () { }); } - mainWindow.on('notification', function (title) { + ipcMain.on('notification', function(event, title, opts) { if (!isOSX() || mainWindow.isFocused()) { return; } @@ -111,7 +111,7 @@ app.on('ready', function () { }); mainWindow.loadURL(appArgs.targetUrl); - // if the window is focused, clear the badge + mainWindow.on('focus', function () { if (!isOSX()) { return; diff --git a/app/src/preload.js b/app/src/preload.js index d5a1953..d64fa10 100644 --- a/app/src/preload.js +++ b/app/src/preload.js @@ -4,13 +4,12 @@ var ipc = require('electron').ipcRenderer; -// monkeypatch window.Notification -hookNotify(function(title, opt){ - ipc.emit('notification', title, opt); +setNotificationCallback(function (title, opt) { + ipc.send('notification', title, opt); }); -document.addEventListener("DOMContentLoaded", function(event) { - // do things +document.addEventListener("DOMContentLoaded", function (event) { + // do things }); ipc.on('params', function (event, message) { @@ -18,18 +17,23 @@ ipc.on('params', function (event, 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; - } - }); +/** + * Patches window.Notification to set a callback on a new Notification + * @param callback + */ +function setNotificationCallback(callback) { - window.Notification = newNotify; + var oldNotify = window.Notification; + var newNotify = function (title, opt) { + callback(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; } diff --git a/src/buildApp.js b/src/buildApp.js index acd576a..2af17d9 100644 --- a/src/buildApp.js +++ b/src/buildApp.js @@ -31,7 +31,7 @@ function buildApp(options, callback) { async.waterfall([ callback => { - copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.badge, options.counter, options.width, options.height, options.userAgent, callback); + copyPlaceholderApp(options.dir, tmpPath, options.name, options.targetUrl, options.counter, options.width, options.height, options.userAgent, callback); }, (tempDir, callback) => { @@ -60,14 +60,13 @@ function buildApp(options, callback) { * @param {string} tempDir * @param {string} name * @param {string} targetURL - * @param {boolean} badge * @param {boolean} counter * @param {number} width * @param {number} height * @param {string} userAgent * @param {tempDirCallback} callback */ -function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, counter, width, height, userAgent, callback) { +function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, counter, width, height, userAgent, callback) { const loadedPackageJson = packageJson; copy(srcAppDir, tempDir, function(error) { if (error) { @@ -79,7 +78,6 @@ function copyPlaceholderApp(srcAppDir, tempDir, name, targetURL, badge, counter, const appArgs = { name: name, targetUrl: targetURL, - badge: badge, counter: counter, width: width, height: height, diff --git a/src/cli.js b/src/cli.js index 8dbf778..421e411 100755 --- a/src/cli.js +++ b/src/cli.js @@ -23,7 +23,6 @@ function main(program) { program.overwrite, program.conceal, program.icon, - program.badge, program.counter, program.width, program.height, @@ -59,7 +58,6 @@ if (require.main === module) { .option('-e, --electron-version ', 'electron version to package, without the \'v\', see https://github.com/atom/electron/releases') .option('-o, --overwrite', 'if output directory for a platform already exists, replaces it rather than skipping it, defaults to false') .option('-c, --conceal', 'packages the source code within your app into an archive, defaults to false, see http://electron.atom.io/docs/v0.36.0/tutorial/application-packaging/') - .option('-b, --badge', 'if the target app should show badges in the dock on receipt of desktop notifications (OSX only), defaults to false') .option('--counter', 'if the target app should use a persistant counter badge in the dock (OSX only), defaults to false') .option('-i, --icon ', 'the icon file to use as the icon for the app (should be a .icns file on OSX)') .option('-w, --width ', 'set window width, defaults to 1280px', parseInt) diff --git a/src/options.js b/src/options.js index 75f9cbe..0da2ea2 100644 --- a/src/options.js +++ b/src/options.js @@ -19,7 +19,6 @@ function optionsFactory(name, overwrite = false, conceal = false, icon, - badge = false, counter = false, width = 1280, height = 800, @@ -59,7 +58,6 @@ function optionsFactory(name, icon: icon, // app configuration - badge: badge, counter: counter, width: width, height: height,