Add poll and filters to get unread

This commit is contained in:
Ximi1970
2020-01-12 15:09:47 +01:00
parent 2f3165f2cd
commit 3a0f209cb5
2 changed files with 51 additions and 24 deletions

View File

@@ -9,9 +9,10 @@
<h3>Background</h3> <h3>Background</h3>
<p>Background HTML</p> <p>Background HTML</p>
<div id="accounts" data-accounts='[]'> <div id="accounts" data-accounts="[]">
<div id="filters" data-filters="[]"></div>
</div>
<script src="background.js"></script> <script src="background.js"></script>
</body> </body>
</html> </html>

View File

@@ -1,11 +1,16 @@
console.log("Starting background.js"); console.log("Starting background.js");
var SysTrayX = {}; var SysTrayX = {
debugAccounts: false
};
SysTrayX.Messaging = { SysTrayX.Messaging = {
initialized: false, initialized: false,
unreadFilter: { unread: true }, unreadFiltersTest: [
{ unread: true },
{ unread: true, folder: { accountId: "account1", path: "/INBOX" } }
],
init: function() { init: function() {
if (this.initialized) { if (this.initialized) {
@@ -14,7 +19,7 @@ SysTrayX.Messaging = {
} }
console.log("Enabling Messaging"); console.log("Enabling Messaging");
this.unReadMessages(this.unreadFilter).then(this.unreadCb); // this.unReadMessages(this.unreadFiltersTest).then(this.unreadCb);
this.initialized = true; this.initialized = true;
}, },
@@ -23,18 +28,22 @@ SysTrayX.Messaging = {
* Use the messages API to get the unread messages (Promise) * Use the messages API to get the unread messages (Promise)
* Be aware that the data is only avaiable inside the callback * Be aware that the data is only avaiable inside the callback
*/ */
unReadMessages: async function(filter) { unReadMessages: async function(filters) {
let page = await browser.messages.query(filter); let unreadMessages = 0;
for (let i = 0; i < filters.length; ++i) {
let page = await browser.messages.query(filters[i]);
let unread = page.messages.length;
let unread = page.messages.length; while (page.id) {
page = await browser.messages.continueList(page.id);
while (page.id) { unread = unread + page.messages.length;
page = await browser.messages.continueList(page.id); }
unread = unread + page.messages.length; unreadMessages = unreadMessages + unread;
} }
return unread; return unreadMessages;
}, },
/* /*
@@ -56,6 +65,12 @@ SysTrayX.Messaging = {
// Store them in the background HTML // Store them in the background HTML
let accountsDiv = document.getElementById("accounts"); let accountsDiv = document.getElementById("accounts");
accountsDiv.setAttribute("data-accounts", JSON.stringify(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));
}, },
onGetAccountsStoageError: function(error) { onGetAccountsStoageError: function(error) {
@@ -65,29 +80,40 @@ SysTrayX.Messaging = {
getAccounts: function() { getAccounts: function() {
console.debug("Get accounts"); console.debug("Get accounts");
let getter = browser.storage.sync.get("accounts"); let getter = browser.storage.sync.get(["accounts", "filters"]);
getter.then(this.getAccountsStorage, this.onGetAccountsStoageError); getter.then(this.getAccountsStorage, this.onGetAccountsStoageError);
}
if (SysTrayX.debugAccounts) {
let accountsDiv = document.getElementById("accounts");
let accountsAttr = accountsDiv.getAttribute("data-accounts");
console.debug("Accounts attr: " + accountsAttr);
let accounts = JSON.parse(accountsAttr);
console.debug("Accounts poll: " + accounts.length);
}
},
}; };
console.log("Starting SysTray-X"); console.log("Starting SysTray-X");
SysTrayX.Messaging.init(); SysTrayX.Messaging.init();
SysTrayX.Messaging.getAccounts();
function pollAccounts() { function pollAccounts() {
let accountsDiv = document.getElementById("accounts"); console.debug("Polling");
accountsAttr = accountsDiv.getAttribute("data-accounts"); SysTrayX.Messaging.getAccounts();
console.debug("Accounts attr: " + accountsAttr);
accounts = JSON.parse(accountsAttr); /*
console.debug("Accounts poll: " + accounts.length); * Get the unread nessages of the selected accounts
*/
let filtersDiv = document.getElementById("filters");
let filtersAttr = filtersDiv.getAttribute("data-filters");
let filters = JSON.parse(filtersAttr);
SysTrayX.Messaging.unReadMessages(filters).then(SysTrayX.Messaging.unreadCb);
} }
window.setTimeout(pollAccounts, 1000); window.setInterval(pollAccounts, 1000);
console.log("Done"); console.log("Done");