Skip to content

Instantly share code, notes, and snippets.

@bparker98
Last active July 21, 2023 22:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bparker98/fdd125541c8ec9c676ca435e9eb9165a to your computer and use it in GitHub Desktop.
Save bparker98/fdd125541c8ec9c676ca435e9eb9165a to your computer and use it in GitHub Desktop.
Office 365 Notifications Tampermonkey Script
// ==UserScript==
// @name Office 365 Notifications
// @namespace http://tampermonkey.net/
// @version 0.4
// @description try to take over the world!
// @author Brandon Parker
// @match https://outlook.office365.com/*
// @match https://outlook.office.com/*
// @iconURL https://r1.res.office365.com/owa/prem/16.1630.12.2223203/resources/images/0/favicon_mail.ico
// @UpdateURL https://gist.githubusercontent.com/bparker98/fdd125541c8ec9c676ca435e9eb9165a/raw/o365-notifications.user.js
// @grant GM_notification
// @grant window.focus
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==
/*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
that detects and handles AJAXed content.
Usage example:
waitForKeyElements (
"div.comments"
, commentCallbackFunction
);
//--- Page-specific function to do what we want when the node is found.
function commentCallbackFunction (jNode) {
jNode.text ("This comment changed by waitForKeyElements().");
}
IMPORTANT: This function requires your script to have loaded jQuery.
*/
function waitForKeyElements (
selectorTxt, /* Required: The jQuery selector string that
specifies the desired element(s).
*/
actionFunction, /* Required: The code to run when elements are
found. It is passed a jNode to the matched
element.
*/
bWaitOnce, /* Optional: If false, will continue to scan for
new elements even after the first match is
found.
*/
iframeSelector /* Optional: If set, identifies the iframe to
search.
*/
) {
var targetNodes, btargetsFound;
if (typeof iframeSelector == "undefined")
targetNodes = $(selectorTxt);
else
targetNodes = $(iframeSelector).contents ()
.find (selectorTxt);
if (targetNodes && targetNodes.length > 0) {
btargetsFound = true;
/*--- Found target node(s). Go through each and act if they
are new.
*/
targetNodes.each ( function () {
var jThis = $(this);
var alreadyFound = jThis.data ('alreadyFound') || false;
if (!alreadyFound) {
//--- Call the payload function.
var cancelFound = actionFunction (jThis);
if (cancelFound)
btargetsFound = false;
else
jThis.data ('alreadyFound', true);
}
} );
}
else {
btargetsFound = false;
}
//--- Get the timer-control variable for this selector.
var controlObj = waitForKeyElements.controlObj || {};
var controlKey = selectorTxt.replace (/[^\w]/g, "_");
var timeControl = controlObj [controlKey];
//--- Now set or clear the timer as appropriate.
if (btargetsFound && bWaitOnce && timeControl) {
//--- The only condition where we need to clear the timer.
clearInterval (timeControl);
delete controlObj [controlKey];
}
else {
//--- Set a timer, if needed.
if ( ! timeControl) {
timeControl = setInterval ( function () {
waitForKeyElements ( selectorTxt,
actionFunction,
bWaitOnce,
iframeSelector
);
},
300
);
controlObj [controlKey] = timeControl;
}
}
waitForKeyElements.controlObj = controlObj;
}
function showNotification(title, text, icon) {
if (icon === null) {
icon = "https://r1.res.office365.com/owa/prem/16.1630.12.2223203/resources/images/0/favicon_mail.ico";
}
var notificationDetails = {
body: text,
icon: icon
};
Notification.requestPermission(function() {
var x = new Notification(title, notificationDetails);
x.addEventListener('click', function() {
try {
window.focus();
x.close();
} catch(e) {
console.log(e);
}
}, false);
setTimeout(x.close.bind(x), 15000);
});
}
(function() {
'use strict';
var callback = function(jNode) {
console.log($('div.o365cs-notifications-newMailPopupButtonCell > div.o365cs-notifications-newMailPopupButtonContent'));
$('div.o365cs-notifications-newMailPopupButtonCell > div.o365cs-notifications-newMailPopupButtonContent').each(
function(index, element) {
var elements = element.getElementsByTagName('span');
var from = '';
var subject = '';
var body = '';
try {
from = "From: " + elements[1].textContent;
} finally {}
try {
subject = elements[2].textContent;
} finally {}
try {
body = elements[3].textContent;
} finally {}
var text = from + "\n" + body;
var icon = null;
var img = element.getElementsByTagName('img');
try {
icon = img[0].src;
} finally {}
showNotification(subject, text, icon);
});
$('div.o365cs-notifications-reminders-listPanel div.o365cs-notifications-reminders-flexpaneitem').each(
function(index, element) {
console.log(index);
console.log(element);
var rows = element.getElementsByClassName('o365cs-notifications-reminders-row');
try {
var left = rows[0].getElementsByClassName('o365cs-notifications-reminders-leftColumn')[0];
var right = rows[0].getElementsByClassName('o365cs-notifications-reminders-rightColumn')[0];
var name = left.textContent;
var inTime = right.textContent;
left = rows[1].getElementsByClassName('o365cs-notifications-reminders-leftColumn')[0];
right = rows[1].getElementsByClassName('o365cs-notifications-reminders-rightColumn')[0];
var time = left.textContent;
showNotification(name, inTime + "\n" + time, null);
} finally {}
});
};
waitForKeyElements('audio', callback);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment