Skip to content

Commit f641ee6

Browse files
authored
fix: account for internal URLs (#155)
1 parent 26878d7 commit f641ee6

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

static/background.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,34 @@ chrome.tabs.onUpdated.addListener(
116116

117117
/** @param {number} tabId */
118118
async function sensor(tabId) {
119-
chrome.scripting.executeScript({
120-
target: { tabId },
121-
122-
func: () => {
123-
const source = chrome.runtime.getURL('/sensor.js');
124-
document.querySelector(`script[src="${source}"]`)?.remove();
125-
const script = document.createElement('script');
126-
script.setAttribute('src', source);
127-
document.documentElement.appendChild(script);
128-
129-
document.addEventListener('SvelteDevTools', ({ detail }) => {
130-
chrome.runtime.sendMessage(detail);
131-
});
132-
},
133-
});
119+
const { url } = await chrome.tabs.get(tabId);
120+
if (url) {
121+
// only execute script for valid tabs with URLs
122+
chrome.scripting.executeScript({
123+
target: { tabId },
124+
125+
func: () => {
126+
const source = chrome.runtime.getURL('/sensor.js');
127+
document.querySelector(`script[src="${source}"]`)?.remove();
128+
const script = document.createElement('script');
129+
script.setAttribute('src', source);
130+
document.documentElement.appendChild(script);
131+
132+
document.addEventListener('SvelteDevTools', ({ detail }) => {
133+
chrome.runtime.sendMessage(detail);
134+
});
135+
},
136+
});
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+
}
134149
}

0 commit comments

Comments
 (0)