2019-11-28 05:17:01 -09:00
|
|
|
import {resolve} from 'path';
|
|
|
|
|
import {app, BrowserWindow} from 'electron';
|
|
|
|
|
import isDev from 'electron-is-dev';
|
2016-07-07 12:48:43 -08:00
|
|
|
|
2020-01-02 05:44:11 -09:00
|
|
|
let win: BrowserWindow;
|
2016-07-07 12:48:43 -08:00
|
|
|
|
|
|
|
|
// 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 :'(
|
|
|
|
|
|
2020-01-02 05:44:11 -09:00
|
|
|
let buffer: string[][] = [];
|
|
|
|
|
|
|
|
|
|
function notify(title: string, body = '', details: any = {}) {
|
|
|
|
|
//eslint-disable-next-line no-console
|
|
|
|
|
console.log(`[Notification] ${title}: ${body}`);
|
|
|
|
|
if (details.error) {
|
|
|
|
|
//eslint-disable-next-line no-console
|
|
|
|
|
console.error(details.error);
|
|
|
|
|
}
|
|
|
|
|
if (win) {
|
|
|
|
|
win.webContents.send('notification', {title, body});
|
|
|
|
|
} else {
|
|
|
|
|
buffer.push([title, body]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-07 14:17:11 -08:00
|
|
|
|
2016-07-07 12:48:43 -08:00
|
|
|
app.on('ready', () => {
|
|
|
|
|
const win_ = new BrowserWindow({
|
2019-10-10 11:20:26 -08:00
|
|
|
show: false,
|
|
|
|
|
webPreferences: {
|
|
|
|
|
nodeIntegration: true
|
|
|
|
|
}
|
2016-07-07 12:48:43 -08:00
|
|
|
});
|
2019-11-28 05:17:01 -09:00
|
|
|
const url = `file://${resolve(isDev ? __dirname : app.getAppPath(), 'notify.html')}`;
|
2016-07-07 12:48:43 -08:00
|
|
|
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);
|
|
|
|
|
});
|
2020-01-02 05:44:11 -09:00
|
|
|
buffer = [];
|
2016-07-07 14:17:11 -08:00
|
|
|
});
|
2016-07-07 12:48:43 -08:00
|
|
|
});
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export default notify;
|