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(` `) function bodyMutationHandler (mutationRecords) { if ($('.o365cs-nav-leftAlign').length && !insertedQuickLinks) { $(` `).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 }); }); } }); }