Last active
May 8, 2017 14:54
-
-
Save rcambrj/4d1b4fde4e4fa00f938f to your computer and use it in GitHub Desktop.
Userscript for desktop notifications for outlook.office365.com in a Fluid App (SSB)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// native notifications for https://outlook.office365.com/ | |
(function () { | |
var logging = false; | |
var bound = []; | |
function log () { | |
return logging && console.log.apply(console, ['notifications:'].concat(Array.prototype.slice.apply(arguments))); | |
} | |
function handleNotification (event) { | |
var originalNotification = event.target.cloneNode(true); | |
log('notifying (original)', originalNotification); | |
// <div> | |
// <button type="button" class="o365cs-notifications-newMailLink ms-bgc-w ms-bgc-nl-h o365button"> | |
// <div class="o365cs-notifications-newMailPopupButton ms-bcl-tp"> | |
// <div class="o365cs-notifications-newMailPopupButtonCell"> | |
// <div class="o365cs-notifications-newMailPopupButtonContent"> | |
// <span autoid="__Microsoft_O365_ShellG2_Owa_templates_cs_Q" class="wf-size-x14 o365cs-notifications-text ms-fcl-nd" style="display: none;">You have 1 new messages</span> | |
// <div> | |
// <span autoid="__Microsoft_O365_ShellG2_Owa_templates_cs_R" class="wf-size-x14 o365cs-notifications-text o365cs-NFP-segoeRegular">John Doe</span> | |
// <span autoid="__Microsoft_O365_ShellG2_Owa_templates_cs_S" class="wf-size-x12 o365cs-notifications-text o365cs-NFP-segoeSemiBold ms-fcl-tp">Re: Subject</span> | |
// <span autoid="__Microsoft_O365_ShellG2_Owa_templates_cs_T" class="wf-size-x12 o365cs-notifications-text o365cs-NFP-segoeSemiLight ms-fcl-ns"> | |
// " | |
// email body | |
// " | |
// </span> | |
// </div> | |
// <button type="button" class="o365cs-notifications-closeButton ms-fcl-np ms-bgc-w-h o365button" aria-label="Close"> | |
// <span class="o365cs-notifications-closeIcon owaimg wf wf-size-x16 wf-o365-cross wf-family-o365" role="presentation"></span> | |
// </button> | |
// </div> | |
// </div> | |
// </div> | |
// </button> | |
// </div> | |
// https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification | |
var newNotification = { | |
title: originalNotification.querySelector('[autoid="__Microsoft_O365_ShellG2_Owa_templates_cs_Q"]').innerText, | |
body: originalNotification.querySelector('[autoid="__Microsoft_O365_ShellG2_Owa_templates_cs_R"]').innerText + "\n" + originalNotification.querySelector('[autoid="__Microsoft_O365_ShellG2_Owa_templates_cs_S"]').innerText | |
}; | |
log('notifying (mangled)', newNotification); | |
new Notification(newNotification.title, newNotification); | |
} | |
function bind () { | |
var containers = Array.prototype.slice.apply(document.querySelectorAll('.o365cs-notifications-notificationPopup')); | |
log('looking for containers...') | |
containers.map(function (container) { | |
if (bound.indexOf(container) < 0) { | |
log('binding to:', container); | |
container.addEventListener('DOMNodeInserted', handleNotification); | |
bound.push(container); | |
} | |
}); | |
window.setTimeout(bind, 5000); | |
} | |
function initialize () { | |
if (Notification.permission === 'default') { | |
log('requesting permissions...') | |
Notification.requestPermission(initialize); | |
return; | |
} else if (Notification.permission !== 'granted') { | |
log("aborting, permission:", Notification.permission); | |
return; | |
} | |
bind(); | |
}; | |
initialize(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment