Skip to content

Commit 5d19e5e

Browse files
authored
fix: fallback to set icon as disabled (#156)
1 parent 3d10d1c commit 5d19e5e

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

static/background.js

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,8 @@ chrome.runtime.onMessage.addListener((message, sender) => {
3838

3939
if (message.type === 'ext/icon:set') {
4040
const selected = message.payload ? 'default' : 'disabled';
41-
return chrome.action.setIcon({
42-
path: {
43-
16: `icons/${selected}-16.png`,
44-
24: `icons/${selected}-24.png`,
45-
48: `icons/${selected}-48.png`,
46-
96: `icons/${selected}-96.png`,
47-
128: `icons/${selected}-128.png`,
48-
},
49-
});
41+
const icons = [16, 24, 48, 96, 128].map((s) => [s, `icons/${selected}-${s}.png`]);
42+
return chrome.action.setIcon({ path: Object.fromEntries(icons) });
5043
}
5144

5245
const port = sender.tab?.id && ports.get(sender.tab.id);
@@ -115,11 +108,9 @@ chrome.tabs.onUpdated.addListener(
115108
);
116109

117110
/** @param {number} tabId */
118-
async function sensor(tabId) {
119-
const { url } = await chrome.tabs.get(tabId);
120-
if (url) {
121-
// only execute script for valid tabs with URLs
122-
chrome.scripting.executeScript({
111+
function sensor(tabId) {
112+
chrome.scripting
113+
.executeScript({
123114
target: { tabId },
124115

125116
func: () => {
@@ -133,17 +124,11 @@ async function sensor(tabId) {
133124
chrome.runtime.sendMessage(detail);
134125
});
135126
},
127+
})
128+
.catch(() => {
129+
// for internal URLs like `chrome://` or `edge://` and extension gallery
130+
// https://chromium.googlesource.com/chromium/src/+/ee77a52baa1f8a98d15f9749996f90e9d3200f2d/chrome/common/extensions/chrome_extensions_client.cc#131
131+
const icons = [16, 24, 48, 96, 128].map((s) => [s, `icons/disabled-${s}.png`]);
132+
chrome.action.setIcon({ path: Object.fromEntries(icons) });
136133
});
137-
} else {
138-
// for internal pages like `chrome://extensions/`
139-
chrome.action.setIcon({
140-
path: {
141-
16: 'icons/disabled-16.png',
142-
24: 'icons/disabled-24.png',
143-
48: 'icons/disabled-48.png',
144-
96: 'icons/disabled-96.png',
145-
128: 'icons/disabled-128.png',
146-
},
147-
});
148-
}
149134
}

0 commit comments

Comments
 (0)