2020-01-04 23:47:26 +01:00
|
|
|
console.log("Starting background.js");
|
|
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
var SysTrayX = {
|
|
|
|
|
debugAccounts: false
|
|
|
|
|
};
|
2020-01-04 23:47:26 +01:00
|
|
|
|
|
|
|
|
SysTrayX.Messaging = {
|
|
|
|
|
initialized: false,
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
unreadFiltersTest: [
|
|
|
|
|
{ unread: true },
|
|
|
|
|
{ unread: true, folder: { accountId: "account1", path: "/INBOX" } }
|
|
|
|
|
],
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-04 23:47:26 +01:00
|
|
|
init: function() {
|
|
|
|
|
console.log("Enabling Messaging");
|
|
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
// Get the accounts from the storage
|
|
|
|
|
SysTrayX.Messaging.getAccounts();
|
|
|
|
|
browser.storage.onChanged.addListener(SysTrayX.Messaging.storageChanged);
|
|
|
|
|
|
|
|
|
|
// Send preferences to app
|
|
|
|
|
SysTrayX.Messaging.sendPreferences();
|
|
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
// this.unReadMessages(this.unreadFiltersTest).then(this.unreadCb);
|
2020-01-22 23:14:46 +01:00
|
|
|
window.setInterval(SysTrayX.Messaging.pollAccounts, 10000);
|
2020-01-04 23:47:26 +01:00
|
|
|
|
|
|
|
|
this.initialized = true;
|
|
|
|
|
},
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
//
|
|
|
|
|
// Handle a storage change
|
|
|
|
|
//
|
|
|
|
|
storageChanged: function(changes, area) {
|
|
|
|
|
console.debug("Changes in store");
|
|
|
|
|
|
|
|
|
|
// Get the new preferences
|
|
|
|
|
SysTrayX.Messaging.getAccounts();
|
|
|
|
|
|
|
|
|
|
if ("addonprefchanged" in changes && changes["addonprefchanged"].newValue) {
|
|
|
|
|
console.debug("Sending preference");
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Send new preferences to the app
|
|
|
|
|
//
|
|
|
|
|
SysTrayX.Messaging.sendPreferences();
|
|
|
|
|
|
|
|
|
|
// Reset flag
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
addonprefchanged: false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
var changedItems = Object.keys(changes);
|
|
|
|
|
for (var item of changedItems) {
|
|
|
|
|
console.log(item + " has changed:");
|
|
|
|
|
console.log("Old value: ");
|
|
|
|
|
console.log(changes[item].oldValue);
|
|
|
|
|
console.log("New value: ");
|
|
|
|
|
console.log(changes[item].newValue);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
},
|
|
|
|
|
|
2020-01-13 23:22:11 +01:00
|
|
|
//
|
|
|
|
|
// Poll the accounts
|
|
|
|
|
//
|
|
|
|
|
pollAccounts: function() {
|
|
|
|
|
console.debug("Polling");
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Get the unread nessages of the selected accounts
|
|
|
|
|
//
|
|
|
|
|
let filtersDiv = document.getElementById("filters");
|
|
|
|
|
let filtersAttr = filtersDiv.getAttribute("data-filters");
|
|
|
|
|
let filters = JSON.parse(filtersAttr);
|
|
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
if (filters.length > 0) {
|
|
|
|
|
SysTrayX.Messaging.unReadMessages(filters).then(
|
|
|
|
|
SysTrayX.Messaging.unreadCb
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-01-13 23:22:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
//
|
2020-01-22 23:14:46 +01:00
|
|
|
// Use the messages API to get the unread messages (Promise)
|
|
|
|
|
// Be aware that the data is only avaiable inside the callback
|
2020-01-13 23:22:11 +01:00
|
|
|
//
|
2020-01-12 15:09:47 +01:00
|
|
|
unReadMessages: async function(filters) {
|
|
|
|
|
let unreadMessages = 0;
|
|
|
|
|
for (let i = 0; i < filters.length; ++i) {
|
|
|
|
|
let page = await browser.messages.query(filters[i]);
|
|
|
|
|
let unread = page.messages.length;
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
while (page.id) {
|
|
|
|
|
page = await browser.messages.continueList(page.id);
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
unread = unread + page.messages.length;
|
|
|
|
|
}
|
2020-01-04 23:47:26 +01:00
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
unreadMessages = unreadMessages + unread;
|
2020-01-04 23:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
return unreadMessages;
|
2020-01-04 23:47:26 +01:00
|
|
|
},
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-13 23:22:11 +01:00
|
|
|
//
|
2020-01-22 23:14:46 +01:00
|
|
|
// Callback for unReadMessages
|
2020-01-13 23:22:11 +01:00
|
|
|
//
|
2020-01-04 23:47:26 +01:00
|
|
|
unreadCb: function(count) {
|
2020-01-22 23:14:46 +01:00
|
|
|
SysTrayX.Link.postSysTrayXMessage({ unreadMail: count });
|
2020-01-11 22:08:11 +01:00
|
|
|
},
|
|
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
sendPreferences: function() {
|
|
|
|
|
console.debug("Send preferences");
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
let getter = browser.storage.sync.get([
|
|
|
|
|
"debug",
|
|
|
|
|
"iconType",
|
|
|
|
|
"iconMime",
|
|
|
|
|
"icon"
|
|
|
|
|
]);
|
|
|
|
|
getter.then(this.sendPreferencesStorage, this.onSendPreferecesStorageError);
|
|
|
|
|
},
|
2020-01-12 15:09:47 +01:00
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
sendPreferencesStorage: function(result) {
|
|
|
|
|
console.debug("Get preferences from storage");
|
|
|
|
|
|
|
|
|
|
let debug = result.debug || "false";
|
|
|
|
|
let iconType = result.iconType || "0";
|
|
|
|
|
let iconMime = result.iconMime || "image/png";
|
|
|
|
|
let icon = result.icon || [];
|
|
|
|
|
|
|
|
|
|
console.log("Debug" + debug);
|
|
|
|
|
console.log("Type" + iconType);
|
|
|
|
|
console.log("Mime" + iconMime);
|
|
|
|
|
console.log(icon);
|
|
|
|
|
|
|
|
|
|
// Send it to the app
|
|
|
|
|
SysTrayX.Link.postSysTrayXMessage({
|
|
|
|
|
preferences: {
|
|
|
|
|
debug: debug,
|
|
|
|
|
iconType: iconType,
|
|
|
|
|
iconMime: iconMime,
|
|
|
|
|
icon: icon
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-01-11 22:08:11 +01:00
|
|
|
},
|
|
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
onSendIconStorageError: function(error) {
|
|
|
|
|
console.log(`GetIcon Error: ${error}`);
|
2020-01-11 22:08:11 +01:00
|
|
|
},
|
|
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
//
|
|
|
|
|
// Get the accounts from the storage
|
|
|
|
|
//
|
2020-01-11 22:08:11 +01:00
|
|
|
getAccounts: function() {
|
|
|
|
|
console.debug("Get accounts");
|
|
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
let getter = browser.storage.sync.get(["accounts", "filters"]);
|
2020-01-13 22:50:50 +01:00
|
|
|
getter.then(this.getAccountsStorage, this.onGetAccountsStorageError);
|
2020-01-04 23:47:26 +01:00
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
if (SysTrayX.debugAccounts) {
|
|
|
|
|
let accountsDiv = document.getElementById("accounts");
|
2020-01-04 23:47:26 +01:00
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
let accountsAttr = accountsDiv.getAttribute("data-accounts");
|
|
|
|
|
console.debug("Accounts attr: " + accountsAttr);
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
let accounts = JSON.parse(accountsAttr);
|
|
|
|
|
console.debug("Accounts poll: " + accounts.length);
|
|
|
|
|
}
|
2020-01-22 23:14:46 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Get the accounts from the storage and
|
|
|
|
|
// make them available in the background HTML
|
|
|
|
|
//
|
|
|
|
|
getAccountsStorage: function(result) {
|
|
|
|
|
console.debug("Get accounts from storage");
|
|
|
|
|
|
|
|
|
|
let accounts = result.accounts || [];
|
|
|
|
|
|
|
|
|
|
// Store them in the background HTML
|
|
|
|
|
let accountsDiv = document.getElementById("accounts");
|
|
|
|
|
accountsDiv.setAttribute("data-accounts", JSON.stringify(accounts));
|
|
|
|
|
|
|
|
|
|
let filters = result.filters || [];
|
|
|
|
|
|
|
|
|
|
// Store them in the background HTML
|
|
|
|
|
let filtersDiv = document.getElementById("filters");
|
|
|
|
|
filtersDiv.setAttribute("data-filters", JSON.stringify(filters));
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onGetAccountsStorageError: function(error) {
|
|
|
|
|
console.log(`GetAccounts Error: ${error}`);
|
2020-01-13 22:50:50 +01:00
|
|
|
}
|
2020-01-12 15:09:47 +01:00
|
|
|
};
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-13 23:22:11 +01:00
|
|
|
//
|
2020-01-22 23:14:46 +01:00
|
|
|
// Link object. Handles the native messaging to the system tray app
|
2020-01-13 23:22:11 +01:00
|
|
|
//
|
|
|
|
|
SysTrayX.Link = {
|
|
|
|
|
portSysTrayX: undefined,
|
|
|
|
|
|
|
|
|
|
init: function() {
|
2020-01-22 23:14:46 +01:00
|
|
|
// Connect to the app
|
2020-01-13 23:22:11 +01:00
|
|
|
this.portSysTrayX = browser.runtime.connectNative("SysTray_X");
|
|
|
|
|
|
|
|
|
|
// Listen for messages from the app.
|
|
|
|
|
this.portSysTrayX.onMessage.addListener(
|
|
|
|
|
SysTrayX.Link.receiveSysTrayXMessage
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
postSysTrayXMessage: function(object) {
|
|
|
|
|
// Send object (will be stringified by postMessage)
|
|
|
|
|
SysTrayX.Link.portSysTrayX.postMessage(object);
|
2020-01-13 23:22:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
receiveSysTrayXMessage: function(response) {
|
|
|
|
|
console.log("Received: " + response);
|
2020-01-22 23:14:46 +01:00
|
|
|
|
|
|
|
|
if (response["preferences"]) {
|
|
|
|
|
// Store the preferences from the app
|
|
|
|
|
console.log("Preferences received");
|
|
|
|
|
|
|
|
|
|
let iconMime = response["preferences"].iconMime;
|
|
|
|
|
if (iconMime) {
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
iconMime: iconMime
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let icon = response["preferences"].icon;
|
|
|
|
|
if (icon) {
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
icon: icon
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let iconType = response["preferences"].iconType;
|
|
|
|
|
if (iconType) {
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
iconType: iconType
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let debug = response["preferences"].debug;
|
|
|
|
|
if (debug) {
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
debug: debug
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-13 23:22:11 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
console.log("Starting SysTray-X");
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
// Setup the link first
|
2020-01-13 23:22:11 +01:00
|
|
|
SysTrayX.Link.init();
|
2020-01-04 23:47:26 +01:00
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
// Main start
|
|
|
|
|
SysTrayX.Messaging.init();
|
|
|
|
|
|
2020-01-04 23:47:26 +01:00
|
|
|
console.log("Done");
|