electron-demo.js/notifications.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

var electron = require('electron');
var ipc = electron.ipcRenderer;
// This file runs in a render thread of the Electron app. It must be required (directly or
// indirectly) from one of the html files.
var timeoutId;
var stopped;
var count;
function startNotifications() {
stopped = false;
count = 0;
if (timeoutId) {
clearTimeout(timeoutId);
}
2017-04-20 18:58:11 +00:00
function notifyUser() {
timeoutId = null;
2017-04-20 18:58:11 +00:00
count += 1;
var notif = new window.Notification('Annoying Alert ' + count, {
body: 'See what happens when you try to click on it.',
silent: true,
});
// Notifications are unclickable on my system currently, not sure why.
notif.onclick = function () {
console.log('notification ' + count + ' clicked');
notif.close();
};
notif.onclose = function () {
if (!stopped && count < 10) {
timeoutId = setTimeout(notifyUser, 5000);
}
2017-04-20 18:58:11 +00:00
};
ipc.send('notification', count);
2017-04-20 18:58:11 +00:00
}
timeoutId = setTimeout(notifyUser, 1000);
}
function stopNotifications() {
stopped = true;
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
// Let the progress bar know that we are finished.
ipc.send('notification', 0);
}
document.body.addEventListener('click', function (ev) {
if (ev.target.classList.contains('start-notify')) {
startNotifications();
}
else if (ev.target.classList.contains('stop-notify')) {
stopNotifications();
}
});