Initial commit
This commit is contained in:
80
background.js
Normal file
80
background.js
Normal file
@@ -0,0 +1,80 @@
|
||||
'use strict';
|
||||
|
||||
console.log("Loading background script....");
|
||||
|
||||
let notificationMap = {};
|
||||
|
||||
chrome.runtime.onMessage.addListener(
|
||||
function (message, sender) {
|
||||
if (message.type === 'quick-links') {
|
||||
chrome.storage.sync.get('showQuickLinks', (data) => {
|
||||
if (data.showQuickLinks) {
|
||||
chrome.tabs.executeScript(sender.tab.id, { code: "$('#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.tabs.executeScript(tabId, { code: `$('#quick-link-mail')[0].click()` });
|
||||
} else {
|
||||
chrome.tabs.executeScript(tabId, { code: `$('#quick-link-calendar')[0].click()` });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
chrome.runtime.onInstalled.addListener(function() {
|
||||
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()]
|
||||
}]);
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user