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 = {
|
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-24 23:03:18 +01:00
|
|
|
|
2020-02-03 23:05:20 +01:00
|
|
|
// Send the app a close command if the window closes
|
|
|
|
|
browser.windows.onRemoved.addListener(SysTrayX.Window.closed);
|
2020-01-24 23:03:18 +01:00
|
|
|
|
2020-02-03 23:05:20 +01:00
|
|
|
// Try to catch the window state
|
|
|
|
|
browser.windows.onFocusChanged.addListener(SysTrayX.Window.focusChanged);
|
2020-01-04 23:47:26 +01:00
|
|
|
},
|
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
|
|
|
|
|
//
|
2020-01-23 23:28:52 +01:00
|
|
|
const filtersDiv = document.getElementById("filters");
|
|
|
|
|
const filtersAttr = filtersDiv.getAttribute("data-filters");
|
|
|
|
|
const filters = JSON.parse(filtersAttr);
|
2020-01-13 23:22:11 +01:00
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
if (filters.length > 0) {
|
|
|
|
|
SysTrayX.Messaging.unReadMessages(filters).then(
|
|
|
|
|
SysTrayX.Messaging.unreadCb
|
|
|
|
|
);
|
2020-01-23 23:28:52 +01:00
|
|
|
} else {
|
|
|
|
|
SysTrayX.Messaging.unReadMessages([{ unread: true }]).then(
|
|
|
|
|
SysTrayX.Messaging.unreadCb
|
|
|
|
|
);
|
2020-01-22 23:14:46 +01:00
|
|
|
}
|
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-23 23:28:52 +01:00
|
|
|
const getter = browser.storage.sync.get([
|
2020-01-22 23:14:46 +01:00
|
|
|
"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");
|
|
|
|
|
|
2020-01-23 23:28:52 +01:00
|
|
|
const debug = result.debug || "false";
|
|
|
|
|
const iconType = result.iconType || "0";
|
|
|
|
|
const iconMime = result.iconMime || "image/png";
|
|
|
|
|
const icon = result.icon || [];
|
2020-01-22 23:14:46 +01:00
|
|
|
|
2020-01-24 22:20:30 +01:00
|
|
|
console.log(`Debug ${debug}`);
|
|
|
|
|
console.log(`Type ${iconType}`);
|
|
|
|
|
console.log(`Mime ${iconMime}`);
|
2020-01-22 23:14:46 +01:00
|
|
|
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-23 23:28:52 +01:00
|
|
|
const 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) {
|
2020-01-23 23:28:52 +01:00
|
|
|
const accountsDiv = document.getElementById("accounts");
|
2020-01-04 23:47:26 +01:00
|
|
|
|
2020-01-23 23:28:52 +01:00
|
|
|
const accountsAttr = accountsDiv.getAttribute("data-accounts");
|
2020-01-24 22:20:30 +01:00
|
|
|
console.debug(`Accounts attr: ${accountsAttr}`);
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-01-23 23:28:52 +01:00
|
|
|
const accounts = JSON.parse(accountsAttr);
|
2020-01-24 22:20:30 +01:00
|
|
|
console.debug(`Accounts poll: ${accounts.length}`);
|
2020-01-12 15:09:47 +01:00
|
|
|
}
|
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");
|
|
|
|
|
|
2020-01-23 23:28:52 +01:00
|
|
|
const accounts = result.accounts || [];
|
2020-01-22 23:14:46 +01:00
|
|
|
|
|
|
|
|
// Store them in the background HTML
|
2020-01-23 23:28:52 +01:00
|
|
|
const accountsDiv = document.getElementById("accounts");
|
2020-01-22 23:14:46 +01:00
|
|
|
accountsDiv.setAttribute("data-accounts", JSON.stringify(accounts));
|
|
|
|
|
|
2020-01-23 23:28:52 +01:00
|
|
|
const filters = result.filters || [];
|
2020-01-22 23:14:46 +01:00
|
|
|
|
|
|
|
|
// Store them in the background HTML
|
2020-01-23 23:28:52 +01:00
|
|
|
const filtersDiv = document.getElementById("filters");
|
2020-01-22 23:14:46 +01:00
|
|
|
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) {
|
2020-01-24 22:20:30 +01:00
|
|
|
console.log(`Received: ${response}`);
|
2020-01-22 23:14:46 +01:00
|
|
|
|
2020-02-03 23:05:20 +01:00
|
|
|
if (response["window"]) {
|
|
|
|
|
console.log("Window received: " + response["window"]);
|
|
|
|
|
|
|
|
|
|
if (response["window"] === "minimized") {
|
|
|
|
|
browser.windows.update(SysTrayX.Window.startWindow.id, {
|
|
|
|
|
state: "minimized"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response["window"] === "normal") {
|
|
|
|
|
browser.windows.update(SysTrayX.Window.startWindow.id, {
|
|
|
|
|
state: "normal",
|
|
|
|
|
focused: true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 23:14:46 +01:00
|
|
|
if (response["preferences"]) {
|
|
|
|
|
// Store the preferences from the app
|
|
|
|
|
console.log("Preferences received");
|
|
|
|
|
|
2020-01-23 23:28:52 +01:00
|
|
|
const iconMime = response["preferences"].iconMime;
|
2020-01-22 23:14:46 +01:00
|
|
|
if (iconMime) {
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
iconMime: iconMime
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 23:28:52 +01:00
|
|
|
const icon = response["preferences"].icon;
|
2020-01-22 23:14:46 +01:00
|
|
|
if (icon) {
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
icon: icon
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 23:28:52 +01:00
|
|
|
const iconType = response["preferences"].iconType;
|
2020-01-22 23:14:46 +01:00
|
|
|
if (iconType) {
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
iconType: iconType
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 23:28:52 +01:00
|
|
|
const debug = response["preferences"].debug;
|
2020-01-22 23:14:46 +01:00
|
|
|
if (debug) {
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
debug: debug
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-13 23:22:11 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-03 23:05:20 +01:00
|
|
|
SysTrayX.Window = {
|
|
|
|
|
startWindow: undefined,
|
|
|
|
|
|
|
|
|
|
closed: function() {
|
|
|
|
|
// Window closed
|
|
|
|
|
console.debug("Shutting down");
|
|
|
|
|
|
|
|
|
|
// Send it to the app
|
|
|
|
|
SysTrayX.Link.postSysTrayXMessage({
|
|
|
|
|
shutdown: ""
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
focusChanged: function(windowId) {
|
|
|
|
|
console.debug("Win focus changed");
|
|
|
|
|
|
|
|
|
|
browser.windows.getCurrent().then(win => {
|
|
|
|
|
SysTrayX.Link.postSysTrayXMessage({ window: win.state });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
if (windowId === -1) {
|
|
|
|
|
// Assume minimized
|
|
|
|
|
SysTrayX.Link.postSysTrayXMessage({
|
|
|
|
|
window: "minimized"
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
browser.windows.get(windowId, function(win) {
|
|
|
|
|
SysTrayX.Link.postSysTrayXMessage({
|
|
|
|
|
window: win.state
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-24 22:20:30 +01:00
|
|
|
async function start() {
|
|
|
|
|
// Init defaults before everything
|
|
|
|
|
await getDefaultIcon();
|
2020-01-11 22:08:11 +01:00
|
|
|
|
2020-02-03 23:05:20 +01:00
|
|
|
SysTrayX.Window.startWindow = await browser.windows
|
|
|
|
|
.getCurrent()
|
|
|
|
|
.then(currentWindow => currentWindow);
|
|
|
|
|
|
|
|
|
|
console.debug("Window focus: " + SysTrayX.Window.startWindow.focused);
|
|
|
|
|
console.debug("Window name: " + SysTrayX.Window.startWindow.title);
|
|
|
|
|
console.debug("Window name: " + SysTrayX.Window.startWindow.state);
|
|
|
|
|
|
|
|
|
|
// browser.windows.update(currentWindow.id, { state: "minimized" });
|
|
|
|
|
// browser.windows.update(currentWindow.id, { state: "normal", focused: true });
|
|
|
|
|
|
|
|
|
|
// ?? browser.windows.update(currentWindow.id, { state: "docked" });
|
|
|
|
|
|
2020-01-24 22:20:30 +01:00
|
|
|
// Setup the link first
|
|
|
|
|
SysTrayX.Link.init();
|
|
|
|
|
|
|
|
|
|
// Main start
|
|
|
|
|
SysTrayX.Messaging.init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log("Starting SysTray-X");
|
2020-01-04 23:47:26 +01:00
|
|
|
|
2020-01-24 22:20:30 +01:00
|
|
|
// Start the add-on
|
|
|
|
|
start();
|
2020-01-22 23:14:46 +01:00
|
|
|
|
2020-01-24 22:20:30 +01:00
|
|
|
console.log("End SysTray-X");
|