From f2498088a12eaafe9800e37aa8257d69b4d98141 Mon Sep 17 00:00:00 2001 From: Stefan Sundin Date: Mon, 18 Mar 2024 20:02:25 -0700 Subject: [PATCH 1/2] Fix monochrome icon in Chrome when `colorTheme == system`. --- .eslintrc | 1 + dist/manifest_chromium.json | 1 + .../background/background_service.js | 1 + keepassxc-browser/background/browserAction.js | 2 +- keepassxc-browser/background/offscreen.js | 31 +++++++++++++++++++ keepassxc-browser/offscreen/offscreen.html | 7 +++++ keepassxc-browser/offscreen/offscreen.js | 9 ++++++ 7 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 keepassxc-browser/background/offscreen.js create mode 100644 keepassxc-browser/offscreen/offscreen.html create mode 100644 keepassxc-browser/offscreen/offscreen.js diff --git a/.eslintrc b/.eslintrc index 04d1a718..92933ab2 100644 --- a/.eslintrc +++ b/.eslintrc @@ -159,6 +159,7 @@ "Pixels": true, "PREDEFINED_SITELIST": true, "RED_BUTTON": true, + "retrieveColorScheme": true, "sendMessage": true, "showNotification": true, "siteMatch": true, diff --git a/dist/manifest_chromium.json b/dist/manifest_chromium.json index b7dc2a56..ff4e77e5 100755 --- a/dist/manifest_chromium.json +++ b/dist/manifest_chromium.json @@ -143,6 +143,7 @@ "cookies", "nativeMessaging", "notifications", + "offscreen", "storage", "tabs", "webNavigation", diff --git a/keepassxc-browser/background/background_service.js b/keepassxc-browser/background/background_service.js index 06c21b8d..e38e1dbf 100644 --- a/keepassxc-browser/background/background_service.js +++ b/keepassxc-browser/background/background_service.js @@ -10,6 +10,7 @@ try { 'client.js', 'keepass.js', 'httpauth.js', + 'offscreen.js', 'browserAction.js', 'page.js', 'event.js', diff --git a/keepassxc-browser/background/browserAction.js b/keepassxc-browser/background/browserAction.js index 679c7f41..7ecdd59d 100755 --- a/keepassxc-browser/background/browserAction.js +++ b/keepassxc-browser/background/browserAction.js @@ -83,7 +83,7 @@ browserAction.generateIconName = async function(iconType) { let style = 'colored'; if (page.settings.useMonochromeToolbarIcon) { if (page.settings.colorTheme === 'system') { - style = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; + style = await retrieveColorScheme(); } else { style = page.settings.colorTheme; } diff --git a/keepassxc-browser/background/offscreen.js b/keepassxc-browser/background/offscreen.js new file mode 100644 index 00000000..921816a7 --- /dev/null +++ b/keepassxc-browser/background/offscreen.js @@ -0,0 +1,31 @@ +'use strict'; + +async function retrieveColorScheme() { + if (typeof window !== 'undefined') { + // Firefox does not support the offscreen API but its background script still has a window (so far) + return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; + } + + const offscreenUrl = browser.runtime.getURL('offscreen/offscreen.html'); + const existingContexts = await browser.runtime.getContexts({ + contextTypes: [ 'OFFSCREEN_DOCUMENT' ], + documentUrls: [ offscreenUrl ], + }); + if (existingContexts.length === 0) { + await browser.offscreen.createDocument({ + url: offscreenUrl, + reasons: [ 'MATCH_MEDIA' ], + justification: 'Retrieve color scheme', + }); + } + + const style = await browser.runtime.sendMessage({ + target: 'offscreen', + action: 'retrieve_color_scheme', + }); + if (!style) { + // if messaging fails then use "light" icon + return 'light'; + } + return style; +} diff --git a/keepassxc-browser/offscreen/offscreen.html b/keepassxc-browser/offscreen/offscreen.html new file mode 100644 index 00000000..a20a5709 --- /dev/null +++ b/keepassxc-browser/offscreen/offscreen.html @@ -0,0 +1,7 @@ + + + + + + + diff --git a/keepassxc-browser/offscreen/offscreen.js b/keepassxc-browser/offscreen/offscreen.js new file mode 100644 index 00000000..b07bef37 --- /dev/null +++ b/keepassxc-browser/offscreen/offscreen.js @@ -0,0 +1,9 @@ +chrome.runtime.onMessage.addListener(({ target, action }, sender, sendResponse) => { + if (target !== 'offscreen') { + return; + } + if (action === 'retrieve_color_scheme') { + const style = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; + sendResponse(style); + } +}); From 34f2e032ee7b2150153b5c2b217b63e30ad16046 Mon Sep 17 00:00:00 2001 From: Stefan Sundin Date: Fri, 29 Mar 2024 23:32:33 -0700 Subject: [PATCH 2/2] Add `background/offscreen.js` to list. --- dist/manifest_firefox.json | 1 + keepassxc-browser/manifest.json | 1 + 2 files changed, 2 insertions(+) diff --git a/dist/manifest_firefox.json b/dist/manifest_firefox.json index 5eaf8a34..f35c3eec 100644 --- a/dist/manifest_firefox.json +++ b/dist/manifest_firefox.json @@ -38,6 +38,7 @@ "background/client.js", "background/keepass.js", "background/httpauth.js", + "background/offscreen.js", "background/browserAction.js", "background/page.js", "background/event.js", diff --git a/keepassxc-browser/manifest.json b/keepassxc-browser/manifest.json index e82c3468..53efa26d 100755 --- a/keepassxc-browser/manifest.json +++ b/keepassxc-browser/manifest.json @@ -40,6 +40,7 @@ "background/client.js", "background/keepass.js", "background/httpauth.js", + "background/offscreen.js", "background/browserAction.js", "background/page.js", "background/event.js",