Skip to content

Instantly share code, notes, and snippets.

@herejia
Created October 10, 2017 14:24
Show Gist options
  • Save herejia/bfb29c21a6fbf513889a3ce1074ea6b5 to your computer and use it in GitHub Desktop.
Save herejia/bfb29c21a6fbf513889a3ce1074ea6b5 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name inotes-web-notifier
// @namespace net.herezia
// @description Notifies user when new mails arrive in the web version of IBM iNotes
// @include http://YOUR_INOTES_URL
// @version 1
// @grant none
// ==/UserScript==
let shouldNotify = false;
function onVisibilityChanged() {
if (document[hidden]) {
shouldNotify = true;
} else {
shouldNotify = false;
}
}
setInterval(checkForNewMails, 60000);
function checkForNewMails() {
if (document.activeElement.nodeName !== "FRAME") {
return;
}
if (shouldNotify) {
const mainFrame = document.getElementById('e-frameset').querySelector('#s_MainFrame').contentWindow.document;
const inboxMenuItem = mainFrame.querySelector("[title='Courrier en arrivée']");
const refreshButton = mainFrame.querySelector('#e-actions-mailview-inbox-refresh');
inboxMenuItem.click(); // show the inbox back in case we were reading or writing a mail in another tab
refreshButton.click();
}
warnIfNewMails();
}
let lastUnreadMailsCount = 0;
const MESSAGE_COUNTER = /.+\((\d+)\)/;
function warnIfNewMails() {
const mainFrame = document.getElementById('e-frameset').querySelector('#s_MainFrame').contentWindow.document;
const inboxMails = mainFrame.querySelector('#e-mailoutline-content > div:first-child table tbody tr td:nth-child(3)').innerHTML;
let unreadMailsCount = MESSAGE_COUNTER.test(inboxMails) ? inboxMails.match(MESSAGE_COUNTER)[1] : 0;
unreadMailsCount = parseInt(unreadMailsCount);
let newMailsCount = unreadMailsCount - lastUnreadMailsCount;
if (shouldNotify && newMailsCount > 0) {
warnNewMails(newMailsCount, unreadMailsCount);
}
lastUnreadMailsCount = unreadMailsCount;
}
function warnNewMails(newMailsCount, unreadMails) {
new Notification('IBM iNotes', {
icon: getFavicon(),
body: `${newMailsCount} new email${plural(newMailsCount)} / ${unreadMails} unread mail${plural(unreadMails)}`,
renotify: true,
requireInteraction: true
})
}
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
if (typeof document.addEventListener === "undefined" || typeof document[hidden] === "undefined") {
console.error("Requires a browser that supports the Page Visibility API.");
} else if (document.activeElement.nodeName !== "FRAME") {
// Handle page visibility change
document.addEventListener(visibilityChange, onVisibilityChanged, false);
Notification.requestPermission();
}
function getFavicon() {
return document.querySelector("link[rel='shortcut icon']").href;
}
function plural(input) {
return input > 1 ? "s" : "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment