hyper/notify.js

42 lines
919 B
JavaScript
Raw Normal View History

2016-07-07 12:48:43 -08:00
const { app, BrowserWindow } = require('electron');
const isDev = require('electron-is-dev');
const { resolve } = require('path');
let win;
// the hack of all hacks
// electron doesn't have a built in notification thing,
// so we launch a window on which we can use the
// HTML5 `Notification` API :'(
2016-07-07 14:17:11 -08:00
let buffer = [];
2016-07-07 12:48:43 -08:00
app.on('ready', () => {
const win_ = new BrowserWindow({
show: false
});
const url = 'file://' + resolve(
isDev ? __dirname : app.getAppPath(),
'notify.html'
);
win_.loadURL(url);
2016-07-07 14:17:11 -08:00
win_.webContents.on('dom-ready', () => {
win = win_;
buffer.forEach(([title, body]) => {
notify(title, body);
});
buffer = null;
});
2016-07-07 12:48:43 -08:00
});
2016-07-07 14:17:11 -08:00
function notify (title, body) {
2016-07-13 12:44:24 -08:00
console.log(`[Notification] ${title}: ${body}`);
2016-07-07 12:48:43 -08:00
if (win) {
win.webContents.send('notification', { title, body });
2016-07-07 14:17:11 -08:00
} else {
buffer.push([title, body]);
2016-07-07 12:48:43 -08:00
}
2016-07-07 14:17:11 -08:00
}
module.exports = notify;