2020-01-24 22:20:30 +01:00
|
|
|
//
|
|
|
|
|
// Set default icon
|
|
|
|
|
// Use <div> as storage
|
|
|
|
|
//
|
|
|
|
|
async function getDefaultIcon() {
|
|
|
|
|
function getStoredIcon(result) {
|
|
|
|
|
return result.iconMime && result.icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onStoredIconError() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getIcon = browser.storage.sync.get(["iconMime", "icon"]);
|
|
|
|
|
const iconStored = await getIcon.then(getStoredIcon, onStoredIconError);
|
|
|
|
|
|
|
|
|
|
if (!iconStored) {
|
2020-05-09 17:10:54 +02:00
|
|
|
const toDataURL = (url) =>
|
2020-01-24 22:20:30 +01:00
|
|
|
fetch(url)
|
2020-05-09 17:10:54 +02:00
|
|
|
.then((response) => response.blob())
|
2020-01-24 22:20:30 +01:00
|
|
|
.then(
|
2020-05-09 17:10:54 +02:00
|
|
|
(blob) =>
|
2020-01-24 22:20:30 +01:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onloadend = () => resolve(reader.result);
|
|
|
|
|
reader.onerror = reject;
|
|
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Convert image to storage param
|
|
|
|
|
let { iconMime, iconBase64 } = await toDataURL("icons/blank-icon.png").then(
|
2020-05-09 17:10:54 +02:00
|
|
|
(dataUrl) => {
|
|
|
|
|
const data = dataUrl.split(":").pop().split(",");
|
2020-01-24 22:20:30 +01:00
|
|
|
return { iconMime: data[0].split(";")[0], iconBase64: data[1] };
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Store default icon (base64)
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
iconMime: iconMime,
|
2020-05-09 17:10:54 +02:00
|
|
|
icon: iconBase64,
|
2020-01-24 22:20:30 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Store in HTML
|
|
|
|
|
const iconDiv = document.getElementById("icon");
|
|
|
|
|
iconDiv.setAttribute("data-icon-mime", iconMime);
|
|
|
|
|
iconDiv.setAttribute("data-icon", iconBase64);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-29 20:36:08 +01:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Get window startup state
|
|
|
|
|
//
|
|
|
|
|
async function getStartupState() {
|
|
|
|
|
function getStartupState(result) {
|
2020-05-14 23:13:05 +02:00
|
|
|
const startMinimized = result.startMinimized || "false";
|
|
|
|
|
return startMinimized === "true" ? "minimized" : "normal";
|
2020-02-29 20:36:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onStartupStateError() {
|
|
|
|
|
return "normal";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getState = browser.storage.sync.get("startMinimized");
|
|
|
|
|
return await getState.then(getStartupState, onStartupStateError);
|
|
|
|
|
}
|
2020-04-03 17:45:20 +02:00
|
|
|
|
|
|
|
|
//
|
2020-05-10 20:55:27 +02:00
|
|
|
// Get minimize on close state
|
2020-04-03 17:45:20 +02:00
|
|
|
//
|
2020-05-10 20:55:27 +02:00
|
|
|
async function getMinimizeOnClose() {
|
|
|
|
|
function getMinimizeOnClosePref(result) {
|
2020-05-14 23:13:05 +02:00
|
|
|
const minimizeOnClose = result.minimizeOnClose || "true";
|
|
|
|
|
return minimizeOnClose === "true";
|
2020-04-03 17:45:20 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-10 20:55:27 +02:00
|
|
|
function onMinimizeOnClosePrefError() {
|
|
|
|
|
return true;
|
2020-04-03 17:45:20 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-10 20:55:27 +02:00
|
|
|
const getState = browser.storage.sync.get("minimizeOnClose");
|
2020-05-10 21:47:41 +02:00
|
|
|
return await getState.then(
|
|
|
|
|
getMinimizeOnClosePref,
|
|
|
|
|
onMinimizeOnClosePrefError
|
|
|
|
|
);
|
2020-05-09 17:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-10 23:44:12 +02:00
|
|
|
//
|
2020-05-12 21:38:03 +02:00
|
|
|
// Check filters
|
2020-05-10 23:44:12 +02:00
|
|
|
//
|
2020-05-13 21:24:29 +02:00
|
|
|
function checkFilters(filters) {
|
2020-05-14 20:41:06 +02:00
|
|
|
let newFilters = [];
|
2020-05-13 21:24:29 +02:00
|
|
|
|
|
|
|
|
if (filters === undefined) {
|
|
|
|
|
// Create base filters
|
2020-05-13 23:54:59 +02:00
|
|
|
for (const account of SysTrayX.Messaging.accounts) {
|
|
|
|
|
const inbox = account.folders.filter((folder) => folder.type == "inbox");
|
|
|
|
|
|
|
|
|
|
if (inbox.length > 0) {
|
|
|
|
|
let folder = { ...inbox[0], accountName: account.name };
|
|
|
|
|
delete folder.type;
|
|
|
|
|
delete folder.subFolders;
|
|
|
|
|
|
|
|
|
|
newFilters.push({
|
|
|
|
|
unread: true,
|
|
|
|
|
folder: folder,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Store the converted filters
|
|
|
|
|
//
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
filters: newFilters,
|
|
|
|
|
});
|
2020-05-13 21:24:29 +02:00
|
|
|
} else if (filters.length > 0) {
|
|
|
|
|
// Check the format
|
2020-05-14 20:41:06 +02:00
|
|
|
|
|
|
|
|
// Find a folder in a account structure
|
|
|
|
|
function findFolder(accountId, path) {
|
|
|
|
|
const account = SysTrayX.Messaging.accounts.filter(
|
|
|
|
|
(account) => account.id === accountId
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (SysTrayX.browserInfo.version.split(".")[0] < 74) {
|
|
|
|
|
//
|
|
|
|
|
// Search the pre TB74 account structure
|
|
|
|
|
//
|
|
|
|
|
const folder = account[0].folders.filter(
|
|
|
|
|
(folder) => folder.path === path
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const folders = account[0].folders.filter((folder) =>
|
|
|
|
|
folder.path.startsWith(path)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (folders.length === 0) {
|
|
|
|
|
return undefined;
|
|
|
|
|
} else if (folders.length === 1) {
|
|
|
|
|
return folder[0].name;
|
|
|
|
|
} else {
|
|
|
|
|
return "^ Add base folder";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//
|
|
|
|
|
// Search the TB74+ account structure
|
|
|
|
|
//
|
|
|
|
|
let arrayOfFolders = [];
|
|
|
|
|
|
|
|
|
|
function traverse(folders, path) {
|
|
|
|
|
if (!folders) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (let f of folders) {
|
|
|
|
|
if (f.path === path) arrayOfFolders.push(f);
|
|
|
|
|
traverse(f.subFolders, path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
traverse(account[0].folders, path);
|
|
|
|
|
|
|
|
|
|
if (arrayOfFolders.length === 0) {
|
|
|
|
|
return undefined;
|
|
|
|
|
} else if (arrayOfFolders[0].subFolders.length > 0) {
|
|
|
|
|
return "^ Add base folder";
|
|
|
|
|
} else {
|
|
|
|
|
return arrayOfFolders[0].name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-13 21:24:29 +02:00
|
|
|
if (filters[0].folder.accountName === undefined) {
|
|
|
|
|
// Old format, convert
|
2020-05-13 23:54:59 +02:00
|
|
|
|
|
|
|
|
console.log("Converting old filters");
|
2020-05-13 21:24:29 +02:00
|
|
|
|
2020-05-14 20:41:06 +02:00
|
|
|
// Create an id -> name map
|
2020-05-13 21:24:29 +02:00
|
|
|
accountNames = {};
|
2020-05-13 23:19:17 +02:00
|
|
|
for (const account of SysTrayX.Messaging.accounts) {
|
|
|
|
|
accountNames[account.id] = account.name;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 20:41:06 +02:00
|
|
|
for (let i = 0; i < filters.length; ++i) {
|
|
|
|
|
let filter = filters[i];
|
2020-05-13 21:24:29 +02:00
|
|
|
|
2020-05-14 20:41:06 +02:00
|
|
|
const accountName = accountNames[filter.folder.accountId];
|
|
|
|
|
if (accountName === undefined) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-05-13 23:19:17 +02:00
|
|
|
|
2020-05-14 20:41:06 +02:00
|
|
|
// Account still active
|
|
|
|
|
filter.folder.accountName = accountName;
|
2020-05-13 23:19:17 +02:00
|
|
|
|
2020-05-14 20:41:06 +02:00
|
|
|
const name = findFolder(filter.folder.accountId, filter.folder.path);
|
|
|
|
|
if (name === undefined) {
|
|
|
|
|
continue;
|
2020-05-13 23:19:17 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-14 20:41:06 +02:00
|
|
|
// Folder still active
|
|
|
|
|
filter.folder.name = name;
|
|
|
|
|
|
|
|
|
|
// Store the new filter
|
|
|
|
|
newFilters.push(filter);
|
2020-05-13 23:19:17 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-13 23:54:59 +02:00
|
|
|
//
|
|
|
|
|
// Store the converted filters
|
|
|
|
|
//
|
2020-05-13 21:24:29 +02:00
|
|
|
browser.storage.sync.set({
|
|
|
|
|
filters: newFilters,
|
|
|
|
|
});
|
2020-05-14 20:41:06 +02:00
|
|
|
} else {
|
|
|
|
|
// Need to check if the accounts still exist
|
|
|
|
|
|
|
|
|
|
// Create an id -> name map
|
|
|
|
|
accountNames = {};
|
|
|
|
|
for (const account of SysTrayX.Messaging.accounts) {
|
|
|
|
|
accountNames[account.id] = account.name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let filtersChanged = false;
|
|
|
|
|
for (let i = 0; i < filters.length; ++i) {
|
|
|
|
|
let filter = filters[i];
|
|
|
|
|
|
|
|
|
|
const accountName = accountNames[filter.folder.accountId];
|
|
|
|
|
if (accountName === undefined) {
|
|
|
|
|
let filtersChanged = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const name = findFolder(filter.folder.accountId, filter.folder.path);
|
|
|
|
|
if (name === undefined) {
|
|
|
|
|
let filtersChanged = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store the new filter
|
|
|
|
|
newFilters.push(filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filtersChanged) {
|
|
|
|
|
//
|
|
|
|
|
// Store the converted filters
|
|
|
|
|
//
|
|
|
|
|
browser.storage.sync.set({
|
|
|
|
|
filters: newFilters,
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-05-13 21:24:29 +02:00
|
|
|
}
|
2020-05-10 23:44:12 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-13 21:24:29 +02:00
|
|
|
return newFilters;
|
2020-05-10 23:44:12 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-09 17:10:54 +02:00
|
|
|
//
|
2020-05-12 21:38:03 +02:00
|
|
|
// Get filters
|
2020-05-09 17:10:54 +02:00
|
|
|
//
|
2020-05-12 21:38:03 +02:00
|
|
|
async function getFilters() {
|
|
|
|
|
function getFiltersCb(result) {
|
2020-05-14 20:41:06 +02:00
|
|
|
const filters = result.filters || undefined;
|
2020-05-13 21:24:29 +02:00
|
|
|
|
2020-05-14 20:41:06 +02:00
|
|
|
const newFilters = checkFilters(filters);
|
2020-05-13 21:24:29 +02:00
|
|
|
|
2020-05-14 20:41:06 +02:00
|
|
|
return newFilters;
|
2020-05-09 17:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-12 21:38:03 +02:00
|
|
|
function onFiltersError() {
|
2020-05-09 17:10:54 +02:00
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 21:38:03 +02:00
|
|
|
const getFiltersStorage = browser.storage.sync.get("filters");
|
|
|
|
|
return await getFiltersStorage.then(getFiltersCb, onFiltersError);
|
2020-05-09 17:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Get count type
|
|
|
|
|
//
|
|
|
|
|
async function getCountType() {
|
|
|
|
|
function getCountTypeCb(result) {
|
|
|
|
|
return result.countType || "0";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onCountTypeError() {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getCountType = browser.storage.sync.get("countType");
|
|
|
|
|
return await getCountType.then(getCountTypeCb, onCountTypeError);
|
2020-04-03 17:45:20 +02:00
|
|
|
}
|