From 3eaf5905cc597f20727b1afdb1aaf7c67c9d613c Mon Sep 17 00:00:00 2001 From: nielm Date: Thu, 30 May 2024 13:37:18 +0200 Subject: [PATCH] Add autodisable time indicator to popup menu When a keepawake with autodisable timeeout is active, show the message "Automatically disable at HH:MM" in the popup --- .../_locales/en/messages.json | 6 ++- .../power/keepAwake Advanced/background.js | 16 +++++-- .../power/keepAwake Advanced/popup.html | 18 +++---- api-samples/power/keepAwake Advanced/popup.js | 47 ++++++++++++++----- 4 files changed, 61 insertions(+), 26 deletions(-) diff --git a/api-samples/power/keepAwake Advanced/_locales/en/messages.json b/api-samples/power/keepAwake Advanced/_locales/en/messages.json index af8dabb29e..a37e542a31 100644 --- a/api-samples/power/keepAwake Advanced/_locales/en/messages.json +++ b/api-samples/power/keepAwake Advanced/_locales/en/messages.json @@ -23,10 +23,14 @@ "message": " until: ", "description": "Suffix to append to above Titles to append an end time" }, - "autoDisableText": { + "autoDisableAfterText": { "message": "Automatically disable after:", "description": "Text labelling a slider allowing setting a timeout for disabling the power saving state." }, + "autoDisableAtText": { + "message": "Automatically disable at: ", + "description": "Prefix Text indicating the time when the state will automatically switch to disabled." + }, "autoDisableHoursSuffix": { "message": "h", "description": "Text to append after a number indicating a quantity of hours" diff --git a/api-samples/power/keepAwake Advanced/background.js b/api-samples/power/keepAwake Advanced/background.js index 75e244d074..79f66ace1d 100644 --- a/api-samples/power/keepAwake Advanced/background.js +++ b/api-samples/power/keepAwake Advanced/background.js @@ -72,10 +72,10 @@ function updateState(mode) { chrome.action.setBadgeText({ text: `${hoursLeft}h` }); const endDate = new Date(mode.endMillis); chrome.action.setTitle({ - title: `${title}${chrome.i18n.getMessage('untilText')} ${endDate.toLocaleTimeString('short')}` + title: `${title}${chrome.i18n.getMessage('untilText')} ${endDate.toLocaleTimeString(undefined, { timeStyle: 'short' })}` }); log( - `mode = ${mode.state} for the next ${hoursLeft}hrs until ${endDate.toLocaleTimeString('short')}` + `mode = ${mode.state} for the next ${hoursLeft}hrs until ${endDate.toLocaleTimeString()}` ); } else { // No timeout. @@ -86,9 +86,11 @@ function updateState(mode) { } /** + * * Apply a new KeepAwake mode. * * @param {KeepAwakeMode} newMode + * @return {Promise} */ async function setNewMode(newMode) { // Clear any old alarms @@ -110,6 +112,7 @@ async function setNewMode(newMode) { // Store the new mode. chrome.storage.local.set(newMode); updateState(newMode); + return newMode; } /** @@ -197,7 +200,6 @@ chrome.runtime.onMessage.addListener(function (request, _, sendResponse) { request.state, request.duration ); - sendResponse({}); setNewMode( verifyMode({ @@ -205,7 +207,13 @@ chrome.runtime.onMessage.addListener(function (request, _, sendResponse) { defaultDurationHrs: request.duration, endMillis: null }) - ); + ) + .then((newMode) => sendResponse(newMode)) + .catch((e) => { + log(`failed to set new mode: ${e}`, e); + sendResponse(null); + }); + return true; // sendResponse() called asynchronously }); // Handle action clicks - rotates the mode to the next mode. diff --git a/api-samples/power/keepAwake Advanced/popup.html b/api-samples/power/keepAwake Advanced/popup.html index 8c1aef91d1..f62c7a2f79 100644 --- a/api-samples/power/keepAwake Advanced/popup.html +++ b/api-samples/power/keepAwake Advanced/popup.html @@ -1,32 +1,32 @@ - Keep Awake + #extensionName