Files
SysTray-X/webext/background.js

387 lines
9.9 KiB
JavaScript
Raw Normal View History

2020-01-12 15:09:47 +01:00
var SysTrayX = {
2020-03-22 21:26:42 +01:00
debugAccounts: false,
2020-04-03 17:45:20 +02:00
pollTiming: {
2020-04-03 21:32:03 +02:00
pollStartupDelay: "30",
pollInterval: "30"
2020-04-03 17:45:20 +02:00
},
2020-04-04 16:51:13 +02:00
platformInfo: undefined,
version: 0
2020-01-12 15:09:47 +01:00
};
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-04 23:47:26 +01:00
init: function() {
// Get the accounts from the storage
SysTrayX.Messaging.getAccounts();
2020-04-03 17:45:20 +02:00
// Lookout for storage changes
browser.storage.onChanged.addListener(SysTrayX.Messaging.storageChanged);
// Send the window title to app
SysTrayX.Messaging.sendTitle();
// Send preferences to app
SysTrayX.Messaging.sendPreferences();
2020-01-12 15:09:47 +01:00
// this.unReadMessages(this.unreadFiltersTest).then(this.unreadCb);
2020-04-03 17:45:20 +02:00
// window.setInterval(SysTrayX.Messaging.pollAccounts, 1000);
window.setTimeout(
SysTrayX.Messaging.pollAccounts,
SysTrayX.pollTiming.pollStartupDelay * 1000
);
2020-01-24 23:03:18 +01:00
// Try to catch the window state
2020-02-24 23:00:45 +01:00
browser.windows.onFocusChanged.addListener(SysTrayX.Window.focusChanged);
2020-01-04 23:47:26 +01:00
},
//
// Handle a storage change
//
storageChanged: function(changes, area) {
// Get the new preferences
SysTrayX.Messaging.getAccounts();
2020-04-03 17:45:20 +02:00
if ("pollStartupDelay" in changes && changes["pollStartupDelay"].newValue) {
SysTrayX.pollTiming = {
...SysTrayX.pollTiming,
pollStartupDelay: changes["pollStartupDelay"].newValue
};
}
if ("pollInterval" in changes && changes["pollInterval"].newValue) {
SysTrayX.pollTiming = {
...SysTrayX.pollTiming,
pollInterval: changes["pollInterval"].newValue
};
}
if ("addonprefchanged" in changes && changes["addonprefchanged"].newValue) {
//
// Send new preferences to the app
//
SysTrayX.Messaging.sendPreferences();
// Reset flag
browser.storage.sync.set({
addonprefchanged: false
});
}
},
2020-01-13 23:22:11 +01:00
//
// Poll the accounts
//
pollAccounts: function() {
//
// 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");
2020-01-13 23:22:11 +01:00
2020-03-02 22:16:19 +01:00
if (filtersAttr !== "undefined") {
const filters = JSON.parse(filtersAttr);
if (filters.length > 0) {
SysTrayX.Messaging.unReadMessages(filters).then(
SysTrayX.Messaging.unreadCb
);
} else {
SysTrayX.Link.postSysTrayXMessage({ unreadMail: 0 });
}
2020-01-23 23:28:52 +01:00
} else {
SysTrayX.Messaging.unReadMessages([{ unread: true }]).then(
SysTrayX.Messaging.unreadCb
);
}
2020-04-03 17:45:20 +02:00
// Next round...
window.setTimeout(
SysTrayX.Messaging.pollAccounts,
SysTrayX.pollTiming.pollInterval * 1000
);
2020-01-13 23:22:11 +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-12 15:09:47 +01:00
while (page.id) {
page = await browser.messages.continueList(page.id);
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-13 23:22:11 +01:00
//
// Callback for unReadMessages
2020-01-13 23:22:11 +01:00
//
2020-01-04 23:47:26 +01:00
unreadCb: function(count) {
SysTrayX.Link.postSysTrayXMessage({ unreadMail: count });
},
sendTitle: function() {
const title = "-" + SysTrayX.Window.startWindow.title.split("-").pop();
SysTrayX.Link.postSysTrayXMessage({ title: title });
},
sendPreferences: function() {
2020-01-23 23:28:52 +01:00
const getter = browser.storage.sync.get([
"debug",
2020-04-02 22:27:34 +02:00
"pollStartupDelay",
"pollInterval",
2020-02-29 19:04:43 +01:00
"hideOnMinimize",
"startMinimized",
"iconType",
"iconMime",
"icon"
]);
getter.then(this.sendPreferencesStorage, this.onSendPreferecesStorageError);
},
2020-01-12 15:09:47 +01:00
sendPreferencesStorage: function(result) {
2020-01-23 23:28:52 +01:00
const debug = result.debug || "false";
2020-04-03 21:32:03 +02:00
const pollStartupDelay = result.pollStartupDelay || "30";
const pollInterval = result.pollInterval || "30";
2020-02-29 19:04:43 +01:00
const hideOnMinimize = result.hideOnMinimize || "true";
2020-03-01 23:43:01 +01:00
const startMinimized = result.startMinimized || "false";
2020-01-23 23:28:52 +01:00
const iconType = result.iconType || "0";
const iconMime = result.iconMime || "image/png";
const icon = result.icon || [];
// Send it to the app
SysTrayX.Link.postSysTrayXMessage({
preferences: {
debug: debug,
2020-04-02 22:27:34 +02:00
pollStartupDelay: pollStartupDelay,
pollInterval: pollInterval,
2020-02-29 19:04:43 +01:00
hideOnMinimize: hideOnMinimize,
startMinimized: startMinimized,
iconType: iconType,
iconMime: iconMime,
icon: icon
}
});
},
onSendIconStorageError: function(error) {
console.log(`GetIcon Error: ${error}`);
},
//
// Get the accounts from the storage
//
getAccounts: function() {
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-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
}
},
//
// Get the accounts from the storage and
// make them available in the background HTML
//
getAccountsStorage: function(result) {
2020-03-02 22:16:19 +01:00
const accounts = result.accounts || undefined;
// Store them in the background HTML
2020-01-23 23:28:52 +01:00
const accountsDiv = document.getElementById("accounts");
accountsDiv.setAttribute("data-accounts", JSON.stringify(accounts));
2020-03-02 22:16:19 +01:00
const filters = result.filters || undefined;
// Store them in the background HTML
2020-01-23 23:28:52 +01:00
const 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-13 23:22:11 +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() {
// 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
);
},
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) {
if (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-02-29 19:04:43 +01:00
if (response["shutdown"]) {
console.log("Shutdown received: " + response["shutdown"]);
}
if (response["preferences"]) {
// Store the preferences from the app
2020-01-23 23:28:52 +01:00
const iconMime = response["preferences"].iconMime;
if (iconMime) {
browser.storage.sync.set({
iconMime: iconMime
});
}
2020-01-23 23:28:52 +01:00
const icon = response["preferences"].icon;
if (icon) {
browser.storage.sync.set({
icon: icon
});
}
2020-01-23 23:28:52 +01:00
const iconType = response["preferences"].iconType;
if (iconType) {
browser.storage.sync.set({
iconType: iconType
});
}
2020-02-29 19:04:43 +01:00
const hideOnMinimize = response["preferences"].hideOnMinimize;
if (hideOnMinimize) {
2020-02-18 00:24:31 +01:00
browser.storage.sync.set({
2020-02-29 19:04:43 +01:00
hideOnMinimize: hideOnMinimize
});
}
const startMinimized = response["preferences"].startMinimized;
if (startMinimized) {
browser.storage.sync.set({
startMinimized: startMinimized
2020-02-18 00:24:31 +01:00
});
}
2020-04-02 22:27:34 +02:00
const pollStartupDelay = response["preferences"].pollStartupDelay;
if (pollStartupDelay) {
browser.storage.sync.set({
pollStartupDelay: pollStartupDelay
});
}
const pollInterval = response["preferences"].pollInterval;
if (pollInterval) {
browser.storage.sync.set({
pollInterval: pollInterval
});
}
2020-01-23 23:28:52 +01:00
const debug = response["preferences"].debug;
if (debug) {
browser.storage.sync.set({
debug: debug
});
}
}
2020-01-13 23:22:11 +01:00
}
};
SysTrayX.Window = {
startWindow: undefined,
focusChanged: function(windowId) {
browser.windows.getCurrent().then(win => {
SysTrayX.Link.postSysTrayXMessage({ window: win.state });
});
}
};
2020-01-24 22:20:30 +01:00
async function start() {
2020-02-29 20:36:08 +01:00
// Get the prefered start state
const state = await getStartupState();
if (state == "minimized") {
browser.windows.update(browser.windows.WINDOW_ID_CURRENT, {
state: "minimized"
});
}
2020-02-29 19:04:43 +01:00
2020-04-03 17:45:20 +02:00
// Get the poll timing
SysTrayX.pollTiming = await getPollTiming();
2020-03-22 21:26:42 +01:00
// Set platform
SysTrayX.platformInfo = await browser.runtime
.getPlatformInfo()
.then(info => info);
console.log("OS: " + SysTrayX.platformInfo.os);
console.log("Arch: " + SysTrayX.platformInfo.arch);
console.log("Nack-Arch: " + SysTrayX.platformInfo.nacl_arch);
2020-04-04 16:51:13 +02:00
// Get addon version
SysTrayX.version = browser.runtime.getManifest().version;
console.log("Addon version: "+SysTrayX.version);
2020-02-29 19:04:43 +01:00
// Init defaults before everything
2020-01-24 22:20:30 +01:00
await getDefaultIcon();
SysTrayX.Window.startWindow = await browser.windows
.getCurrent()
.then(currentWindow => currentWindow);
2020-01-24 22:20:30 +01:00
// Setup the link first
SysTrayX.Link.init();
2020-02-29 20:36:08 +01:00
// Send current state
SysTrayX.Link.postSysTrayXMessage({ window: state });
2020-01-24 22:20:30 +01:00
// 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();