A-Toasty-OWA/contentScript.js

88 lines
4.1 KiB
JavaScript

const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
const bodyObserver = new MutationObserver(bodyMutationHandler);
const popUpObserver = new MutationObserver(popUpAreaHandler);
const obsConfig = { childList: true };
let insertedQuickLinks = false;
bodyObserver.observe($('body').get()[0], obsConfig);
$('body').append(`
<style>
.quick-links-container {
display: none;
line-height: 41px;
border-left: 1px solid rgba(255,255,255,.3);
border-right: 1px solid rgba(255,255,255,.3);
}
.o365cs-nav-appTitle.quick-link {
margin-left: 0;
text-align: center;
width: 50px;
}
.quick-link-icon {
font-size: 25px;
vertical-align: middle;
}
</style>
`)
function bodyMutationHandler (mutationRecords) {
if ($('.o365cs-nav-leftAlign').length && !insertedQuickLinks) {
$(`
<div id="quick-links" class="o365cs-nav-topItem o365cs-topnavLinkBackground-2 quick-links-container" style='display: none'>
<a id='quick-link-mail' title="Inbox" class="o365cs-nav-appTitle o365cs-topnavText o365button ms-bgc-tdr-h quick-link" href="https://${window.location.host}/owa/#path=/mail/inbox">
<span class="red o365cs-nav-appTileIcon owaimg ms-Icon--outlook ms-icon-font-size-55 ms-fcl-w quick-link-icon"></span>
</a>
<a id='quick-link-calendar' title="Calendar" class="o365cs-nav-appTitle o365cs-topnavText o365button ms-bgc-tdr-h quick-link" href="https://${window.location.host}/owa/#path=/calendar">
<span class="o365cs-nav-appTileIcon owaimg ms-Icon--calendar ms-icon-font-size-55 ms-fcl-w quick-link-icon"></span>
</a>
<a id='quick-link-people' title="People" class="o365cs-nav-appTitle o365cs-topnavText o365button ms-bgc-tdr-h quick-link" href="https://${window.location.host}/owa/#path=/people">
<span class="o365cs-nav-appTileIcon owaimg ms-Icon--people ms-icon-font-size-55 ms-fcl-w quick-link-icon"></span>
</a>
<a id='quick-link-tasks' title="Tasks" class="o365cs-nav-appTitle o365cs-topnavText o365button ms-bgc-tdr-h quick-link" href="https://${window.location.host}/owa/#path=/tasks">
<span class="o365cs-nav-appTileIcon owaimg ms-Icon--tasks ms-icon-font-size-55 ms-fcl-w quick-link-icon"></span>
</a>
</div>`).insertBefore($('.o365cs-nav-leftAlign').find('.o365cs-nav-topItem').last());
insertedQuickLinks = true;
chrome.runtime.sendMessage(null, { type: 'quick-links' });
}
if ($(".o365cs-notifications-notificationPopupArea").length === 1 && $(".o365cs-notifications-notificationPopup").length > 0) {
bodyObserver.disconnect();
console.log('Attaching popup observer...');
popUpObserver.observe($(".o365cs-notifications-notificationPopup").get()[0], obsConfig);
sendMessages();
}
}
function popUpAreaHandler(mutationRecords) {
mutationRecords.forEach(function (mutation) {
if (mutation.addedNodes.length > 0) {
sendMessages();
}
});
}
function sendMessages() {
$('.o365cs-notifications-notificationPopup').each(function () {
const textNodes = $(this).find('.o365cs-notifications-text');
if (textNodes.length) {
console.log('New message...');
const from = $(textNodes[1]).text();
const subject = $(textNodes[2]).text();
const body = $(textNodes[3]).text();
chrome.runtime.sendMessage(null, { type: 'email', from, subject, body });
}
if ($(this).find('.o365cs-notifications-reminders-listPanel').length) {
console.log('New calendar reminder...');
$(this).find('.o365cs-notifications-reminders-container').each(function () {
const title = $(this).find('.o365cs-notifications-reminders-title').text();
const duration = $(this).find('.o365cs-notifications-reminders-timeDuration').text();
chrome.runtime.sendMessage(null, { type: 'calendar', title, duration });
});
}
});
}