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() {
|
|
|
|
|
if (this.initialized) {
|
|
|
|
|
console.log("Messaging already initialized");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log("Enabling Messaging");
|
|
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
// this.unReadMessages(this.unreadFiltersTest).then(this.unreadCb);
|
2020-01-04 23:47:26 +01:00
|
|
|
|
|
|
|
|
this.initialized = true;
|
|
|
|
|
},
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-04 23:47:26 +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-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-04 23:47:26 +01:00
|
|
|
/*
|
|
|
|
|
* Callback for unReadMessages
|
|
|
|
|
*/
|
|
|
|
|
unreadCb: function(count) {
|
2020-01-11 22:08:11 +01:00
|
|
|
console.log("SysTrayX unread " + count);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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));
|
2020-01-12 15:09:47 +01:00
|
|
|
|
|
|
|
|
let filters = result.filters || [];
|
|
|
|
|
|
|
|
|
|
// Store them in the background HTML
|
|
|
|
|
let filtersDiv = document.getElementById("filters");
|
|
|
|
|
filtersDiv.setAttribute("data-filters", JSON.stringify(filters));
|
2020-01-11 22:08:11 +01:00
|
|
|
},
|
|
|
|
|
|
2020-01-13 22:50:50 +01:00
|
|
|
onGetAccountsStorageError: function(error) {
|
2020-01-11 22:08:11 +01:00
|
|
|
console.log(`GetAccounts Error: ${error}`);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
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-13 22:50:50 +01:00
|
|
|
}
|
2020-01-12 15:09:47 +01:00
|
|
|
};
|
2020-01-11 22:08: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-12 15:09:47 +01:00
|
|
|
SysTrayX.Messaging.init();
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-12 19:12:34 +01:00
|
|
|
/*
|
2020-01-13 22:50:50 +01:00
|
|
|
* Start native messaging ping pong
|
2020-01-12 19:12:34 +01:00
|
|
|
*/
|
|
|
|
|
var port = browser.runtime.connectNative("ping_pong");
|
|
|
|
|
|
|
|
|
|
// Listen for messages from the app.
|
2020-01-13 22:50:50 +01:00
|
|
|
port.onMessage.addListener(response => {
|
2020-01-12 19:12:34 +01:00
|
|
|
console.log("Received: " + response);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Every second, send the app a message.
|
|
|
|
|
function ping() {
|
|
|
|
|
console.log("Sending: ping");
|
|
|
|
|
port.postMessage("ping");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.setInterval(ping, 1000);
|
|
|
|
|
|
2020-01-13 22:50:50 +01:00
|
|
|
/*
|
|
|
|
|
* Start native messaging SysTray-X
|
|
|
|
|
*/
|
|
|
|
|
var portSysTrayX = browser.runtime.connectNative("SysTray_X");
|
|
|
|
|
|
|
|
|
|
// Listen for messages from the app.
|
|
|
|
|
portSysTrayX.onMessage.addListener(response => {
|
|
|
|
|
console.log("Received: " + response);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Every second, send the app a message.
|
|
|
|
|
function postSysTrayXMessage() {
|
|
|
|
|
console.log("Sending: Hallo World!");
|
|
|
|
|
portSysTrayX.postMessage("Hallo World!");
|
|
|
|
|
// portSysTrayX.postMessage({ key: "Hallo", value: "World!" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.setInterval(postSysTrayXMessage, 1000);
|
2020-01-12 19:12:34 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Poll the accounts
|
|
|
|
|
*/
|
2020-01-11 22:08:11 +01:00
|
|
|
function pollAccounts() {
|
2020-01-12 15:09:47 +01:00
|
|
|
console.debug("Polling");
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
SysTrayX.Messaging.getAccounts();
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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-11 22:08:11 +01:00
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
SysTrayX.Messaging.unReadMessages(filters).then(SysTrayX.Messaging.unreadCb);
|
2020-01-11 22:08:11 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-12 15:09:47 +01:00
|
|
|
window.setInterval(pollAccounts, 1000);
|
2020-01-04 23:47:26 +01:00
|
|
|
|
|
|
|
|
console.log("Done");
|