Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix monochrome icon in Chrome when colorTheme == 'system' (MV3 fix) #2152

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
"Pixels": true,
"PREDEFINED_SITELIST": true,
"RED_BUTTON": true,
"retrieveColorScheme": true,
"sendMessage": true,
"showNotification": true,
"siteMatch": true,
Expand Down
1 change: 1 addition & 0 deletions dist/manifest_chromium.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
"cookies",
"nativeMessaging",
"notifications",
"offscreen",
"storage",
"tabs",
"webNavigation",
Expand Down
1 change: 1 addition & 0 deletions keepassxc-browser/background/background_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ try {
'client.js',
'keepass.js',
'httpauth.js',
'offscreen.js',
'browserAction.js',
'page.js',
'event.js',
Expand Down
2 changes: 1 addition & 1 deletion keepassxc-browser/background/browserAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 31 additions & 0 deletions keepassxc-browser/background/offscreen.js
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 7 additions & 0 deletions keepassxc-browser/offscreen/offscreen.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script src="offscreen.js"></script>
</head>
<body></body>
</html>
9 changes: 9 additions & 0 deletions keepassxc-browser/offscreen/offscreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
chrome.runtime.onMessage.addListener(({ target, action }, sender, sendResponse) => {
varjolintu marked this conversation as resolved.
Show resolved Hide resolved
if (target !== 'offscreen') {
return;
}
if (action === 'retrieve_color_scheme') {
const style = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
sendResponse(style);
}
});