@@ -2,8 +2,6 @@ import browser, { Runtime } from "webextension-polyfill";
22import { isManifestV3 } from "~/common/utils/mv3" ;
33import { MessageSetIcon } from "~/types" ;
44
5- import state from "../../state" ;
6-
75// Store all tabs with their respective icon
86const 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