Skip to content

Commit 1e4504d

Browse files
Merge pull request #3472 from getAlby/browser-theme
feat: detect browser theme instead of extension theme for icon change…
2 parents d20a173 + 3ed090f commit 1e4504d

File tree

1 file changed

+22
-4
lines changed
  • src/extension/background-script/actions/setup

1 file changed

+22
-4
lines changed

src/extension/background-script/actions/setup/setIcon.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import browser, { Runtime } from "webextension-polyfill";
22
import { isManifestV3 } from "~/common/utils/mv3";
33
import { MessageSetIcon } from "~/types";
44

5-
import state from "../../state";
6-
75
// Store all tabs with their respective icon
86
const tabIcons = new Map<number, string>();
97

@@ -46,7 +44,27 @@ const setIcon = async (icon: string, tabId: number): Promise<void> => {
4644

4745
tabIcons.set(tabId, icon);
4846

49-
const theme = state.getState().settings.theme == "dark" ? "_dark" : "";
47+
// For Chrome (Manifest V3): Use browser theme (prefers-color-scheme)
48+
// For Firefox (Manifest V2): theme_icons in manifest.json handles OS-based icons automatically
49+
let theme = "";
50+
if (isManifestV3) {
51+
try {
52+
const results = await browser.scripting.executeScript({
53+
target: { tabId },
54+
func: () => {
55+
return window.matchMedia &&
56+
window.matchMedia("(prefers-color-scheme: dark)").matches
57+
? "dark"
58+
: "light";
59+
},
60+
});
61+
theme = results[0]?.result === "dark" ? "_dark" : "";
62+
} catch (error) {
63+
console.warn("Failed to detect browser theme, using default:", error);
64+
theme = "";
65+
}
66+
}
67+
5068
const iconsParams = {
5169
path: {
5270
// it's looking relative from the "js" folder
@@ -63,4 +81,4 @@ const setIcon = async (icon: string, tabId: number): Promise<void> => {
6381
: browser.browserAction.setIcon(iconsParams);
6482
};
6583

66-
export { setIcon, setIconMessageHandler, ExtensionIcon };
84+
export { ExtensionIcon, setIcon, setIconMessageHandler };

0 commit comments

Comments
 (0)