diff --git a/webext/background.js b/webext/background.js index 940a3ff..f2a6b5b 100644 --- a/webext/background.js +++ b/webext/background.js @@ -1 +1,54 @@ -console.log("Hello world!"); +console.log("Starting background.js"); + +var SysTrayX = {}; + +SysTrayX.Messaging = { + + initialized: false, + + unreadFilter: { unread: true }, + + init: function() { + if (this.initialized) { + console.log("Messaging already initialized"); + return; + } + console.log("Enabling Messaging"); + + this.unReadMessages(this.unreadFilter).then(this.unreadCb); + + this.initialized = true; + }, + + /* + * Use the messages API to get the unread messages (Promise) + * Be aware that the data is only avaiable inside the callback + */ + unReadMessages: async function(filter) { + let page = await browser.messages.query(filter); + + let unread = page.messages.length; + + while (page.id) { + page = await browser.messages.continueList(page.id); + + unread = unread + page.messages.length + } + + return unread; + }, + + /* + * Callback for unReadMessages + */ + unreadCb: function(count) { + console.log("SysTrayX unread "+count); + } + +}; + +console.log("Starting SysTray-X"); + +SysTrayX.Messaging.init(); + +console.log("Done"); diff --git a/webext/manifest.json b/webext/manifest.json index 9b883ed..83ed510 100644 --- a/webext/manifest.json +++ b/webext/manifest.json @@ -17,11 +17,11 @@ "48": "icons/message.svg" }, + "permissions": [ + "messagesRead" + ], + "background": { "scripts": ["background.js"] - }, - - "browser_action": { - "default_icon": "icons/message.svg" } }