Make extention acceptable by Edge Add-Ons website
This commit is contained in:
92
src/contentScript.js
Normal file
92
src/contentScript.js
Normal file
@ -0,0 +1,92 @@
|
||||
const $ = require('jquery');
|
||||
|
||||
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);
|
||||
window.$ = $;
|
||||
|
||||
$('body').append(`
|
||||
<script src="jquery.js"></script>
|
||||
<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 });
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
16
src/options.js
Normal file
16
src/options.js
Normal file
@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const $ = require('jquery');
|
||||
|
||||
$('#emailDelay').change((event) => {
|
||||
chrome.storage.sync.set({ emailDelay: parseInt(event.target.value) });
|
||||
});
|
||||
|
||||
$('#calendarDelay').change((event) => {
|
||||
chrome.storage.sync.set({ calendarDelay: parseInt(event.target.value) });
|
||||
});
|
||||
|
||||
chrome.storage.sync.get(null, (data) => {
|
||||
$('#emailDelay').val(data.emailDelay);
|
||||
$('#calendarDelay').val(data.calendarDelay);
|
||||
});
|
26
src/popup.js
Normal file
26
src/popup.js
Normal file
@ -0,0 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
const $ = require('jquery');
|
||||
|
||||
$('#emailDelay').change((event) => {
|
||||
chrome.storage.sync.set({ emailDelay: parseInt(event.target.value) });
|
||||
});
|
||||
|
||||
$('#calendarDelay').change((event) => {
|
||||
chrome.storage.sync.set({ calendarDelay: parseInt(event.target.value) });
|
||||
});
|
||||
|
||||
$('#quickLinks').change((event) => {
|
||||
chrome.storage.sync.set({ showQuickLinks: $('#quickLinks').prop('checked') });
|
||||
if ($('#quickLinks').prop('checked')) {
|
||||
chrome.tabs.executeScript({ code: "$('#quick-links').show()" });
|
||||
} else {
|
||||
chrome.tabs.executeScript({ code: "$('#quick-links').hide()" });
|
||||
}
|
||||
});
|
||||
|
||||
chrome.storage.sync.get(null, (data) => {
|
||||
$('#emailDelay').val(data.emailDelay);
|
||||
$('#calendarDelay').val(data.calendarDelay);
|
||||
$('#quickLinks').prop('checked', data.showQuickLinks);
|
||||
});
|
89
src/service_worker.js
Normal file
89
src/service_worker.js
Normal file
@ -0,0 +1,89 @@
|
||||
'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);
|
||||
}
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user