'use strict'; console.log("Loading service worker...."); let notificationMap = {}; chrome.runtime.onInstalled.addListener(() => { chrome.storage.sync.set({ emailDelay: -1, calendarDelay: -1, showQuickLinks: true }); chrome.declarativeContent.onPageChanged.removeRules(undefined, function () { chrome.declarativeContent.onPageChanged.addRules([{ conditions: [new chrome.declarativeContent.PageStateMatcher({ css: ["body[aria-label='Outlook']"] })], actions: [new chrome.declarativeContent.ShowPageAction()] }]); }); }); chrome.runtime.onMessage.addListener((message, sender) => { if (message.type === 'quick-links') { chrome.storage.sync.get('showQuickLinks', (data) => { if (data.showQuickLinks) { chrome.scripting.executeScript({ target: { tabId: sender.tab.id }, func: () => { $('#quick-links').show() } }); } }); } else if (message.type === 'email') { const { from, subject, body } = message; chrome.notifications.create(null, { type: "basic", iconUrl: "images/email.png", title: subject, message: `From: ${from}\n${body}`, requireInteraction: true }, (notificationId) => { notificationMap[notificationId] = { type: 'email', tabId: sender.tab.id, windowId: sender.tab.windowId }; startCloseNotificationTimer(notificationId, 'email'); }); } else { const { title, duration } = message; chrome.notifications.create(null, { type: "basic", iconUrl: "images/calendar.png", title, message: duration, requireInteraction: true }, (notificationId) => { notificationMap[notificationId] = { type: 'calendar', tabId: sender.tab.id, windowId: sender.tab.windowId }; startCloseNotificationTimer(notificationId, 'calendar'); }); } } ); chrome.notifications.onClicked.addListener((notificationId) => { if (notificationMap[notificationId]) { const { type, tabId, windowId } = notificationMap[notificationId]; chrome.tabs.update(tabId, { active: true, highlighted: true }); chrome.windows.update(windowId, { focused: true }); chrome.notifications.clear(notificationId); if (type === 'email') { chrome.scripting.executeScript({ target: { tabId: tabId }, func: () => { $('#quick-link-mail')[0].click() } }); } else { chrome.scripting.executeScript({ target: { tabId: tabId }, tabId, func: () => { $('#quick-link-calendar')[0].click() } }); } } }); function startCloseNotificationTimer(notificationId, type) { chrome.storage.sync.get(`${type}Delay`, (data) => { const delay = data[`${type}Delay`]; if (delay > 0) { console.log(`Showing notification for ${delay} seconds`); setTimeout(() => { chrome.notifications.clear(notificationId); }, delay * 1000); } }); }