From cb1f0d292064a7e4441d4cd92ea72368bb0a300a Mon Sep 17 00:00:00 2001 From: Ximi1970 Date: Fri, 24 Jan 2020 22:20:30 +0100 Subject: [PATCH] Cleanup, fix initial setup icon --- webext/background.html | 3 +++ webext/background.js | 36 +++++++++++++++------------ webext/js/defaults.js | 56 ++++++++++++++++++++++++++++++++++++++++++ webext/options.html | 53 ++++++++++++++++++++++++++------------- webext/options.js | 40 ------------------------------ 5 files changed, 115 insertions(+), 73 deletions(-) create mode 100644 webext/js/defaults.js diff --git a/webext/background.html b/webext/background.html index 7a847c2..d9387ec 100644 --- a/webext/background.html +++ b/webext/background.html @@ -13,6 +13,9 @@
+ +
+ diff --git a/webext/background.js b/webext/background.js index 1057376..28b52ef 100644 --- a/webext/background.js +++ b/webext/background.js @@ -5,8 +5,6 @@ var SysTrayX = { }; SysTrayX.Messaging = { - initialized: false, - unreadFiltersTest: [ { unread: true }, { unread: true, folder: { accountId: "account1", path: "/INBOX" } } @@ -24,8 +22,6 @@ SysTrayX.Messaging = { // this.unReadMessages(this.unreadFiltersTest).then(this.unreadCb); window.setInterval(SysTrayX.Messaging.pollAccounts, 10000); - - this.initialized = true; }, // @@ -136,9 +132,9 @@ SysTrayX.Messaging = { const iconMime = result.iconMime || "image/png"; const icon = result.icon || []; - console.log("Debug" + debug); - console.log("Type" + iconType); - console.log("Mime" + iconMime); + console.log(`Debug ${debug}`); + console.log(`Type ${iconType}`); + console.log(`Mime ${iconMime}`); console.log(icon); // Send it to the app @@ -169,10 +165,10 @@ SysTrayX.Messaging = { const accountsDiv = document.getElementById("accounts"); const accountsAttr = accountsDiv.getAttribute("data-accounts"); - console.debug("Accounts attr: " + accountsAttr); + console.debug(`Accounts attr: ${accountsAttr}`); const accounts = JSON.parse(accountsAttr); - console.debug("Accounts poll: " + accounts.length); + console.debug(`Accounts poll: ${accounts.length}`); } }, @@ -223,7 +219,7 @@ SysTrayX.Link = { }, receiveSysTrayXMessage: function(response) { - console.log("Received: " + response); + console.log(`Received: ${response}`); if (response["preferences"]) { // Store the preferences from the app @@ -260,12 +256,20 @@ SysTrayX.Link = { } }; +async function start() { + // Init defaults before everything + await getDefaultIcon(); + + // Setup the link first + SysTrayX.Link.init(); + + // Main start + SysTrayX.Messaging.init(); +} + console.log("Starting SysTray-X"); -// Setup the link first -SysTrayX.Link.init(); +// Start the add-on +start(); -// Main start -SysTrayX.Messaging.init(); - -console.log("Done"); +console.log("End SysTray-X"); diff --git a/webext/js/defaults.js b/webext/js/defaults.js new file mode 100644 index 0000000..deb70f0 --- /dev/null +++ b/webext/js/defaults.js @@ -0,0 +1,56 @@ +// +// Set default icon +// Use
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) { + const toDataURL = url => + fetch(url) + .then(response => response.blob()) + .then( + blob => + 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( + dataUrl => { + const data = dataUrl + .split(":") + .pop() + .split(","); + return { iconMime: data[0].split(";")[0], iconBase64: data[1] }; + } + ); + + // Store default icon (base64) + browser.storage.sync.set({ + iconMime: iconMime, + icon: iconBase64 + }); + + // Store in HTML + const iconDiv = document.getElementById("icon"); + iconDiv.setAttribute("data-icon-mime", iconMime); + iconDiv.setAttribute("data-icon", iconBase64); + + console.debug("Default: " + iconMime); + console.debug("Default: " + iconBase64); + } +} diff --git a/webext/options.html b/webext/options.html index 7bb1ee0..ccb17f5 100644 --- a/webext/options.html +++ b/webext/options.html @@ -7,15 +7,15 @@ - - +
- Display debug window
+ Display debug + window

- +
@@ -36,7 +36,6 @@ Check 1
Check 2
Check 3
-
@@ -44,30 +43,49 @@

Icon

-

Please select your option:

- - - - - - -
Blank icon
New mail icon
Custom icon - - + New mail icon
+ + + Custom icon + + + + + + + + + +
-
@@ -75,8 +93,9 @@

Included accounts

- +
+
diff --git a/webext/options.js b/webext/options.js index cd8c7c7..902518d 100644 --- a/webext/options.js +++ b/webext/options.js @@ -306,46 +306,6 @@ SysTrayX.StorageChanged = { } }; -// -// Set default icon -// -const toDataURL = url => - fetch(url) - .then(response => response.blob()) - .then( - blob => - new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.onloadend = () => resolve(reader.result); - reader.onerror = reject; - reader.readAsDataURL(blob); - }) - ); - -toDataURL("icons/blank-icon.png").then(dataUrl => { - const data = dataUrl - .split(":") - .pop() - .split(","); - const mime = data[0].split(";")[0]; - const base64 = data[1]; - - const iconDiv = document.getElementById("icon"); - - if ( - iconDiv.getAttribute("data-icon") === "" && - iconDiv.getAttribute("data-icon-mime") === "" - ) { - iconDiv.setAttribute("data-icon", base64); - iconDiv.setAttribute("data-icon-mime", mime); - - console.debug("Default set"); - } - - console.debug("Default: " + mime); - console.debug("Default: " + base64); -}); - // // Main //