Skip to content

Commit 26878d7

Browse files
authored
feat: use badge to detect svelte in active tab (#154)
1 parent d25eeb1 commit 26878d7

16 files changed

+93
-13
lines changed

rollup.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export default defineConfig([
77
file: 'build/background.js',
88
},
99
},
10+
{
11+
input: 'static/sensor.js',
12+
output: {
13+
file: 'build/sensor.js',
14+
},
15+
},
1016
{
1117
input: 'src/client/index.js',
1218
output: [

src/app.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ declare global {
116116
data: unknown;
117117
}
118118
>;
119+
120+
SvelteDevTools: CusomEvent<{
121+
type: string;
122+
payload: string;
123+
}>;
119124
}
120125
}
121126

static/background.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ chrome.runtime.onConnect.addListener((port) => {
1616
ports.delete(message.tabId);
1717

1818
if (ports.size === 0) {
19-
chrome.tabs.onUpdated.removeListener(attach);
19+
chrome.tabs.onUpdated.removeListener(courier);
2020
}
2121
});
2222

23-
return chrome.tabs.onUpdated.addListener(attach);
23+
return chrome.tabs.onUpdated.addListener(courier);
2424
} else if (message.type === 'ext/reload') {
2525
return chrome.runtime.reload();
2626
} else if (message.type === 'page/refresh') {
@@ -35,12 +35,26 @@ chrome.runtime.onConnect.addListener((port) => {
3535
// relay messages from `chrome.scripting` to devtools page
3636
chrome.runtime.onMessage.addListener((message, sender) => {
3737
if (sender.id !== chrome.runtime.id) return; // unexpected sender
38+
39+
if (message.type === 'ext/icon:set') {
40+
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+
});
50+
}
51+
3852
const port = sender.tab?.id && ports.get(sender.tab.id);
39-
if (port) port.postMessage(message);
53+
if (port) return port.postMessage(message);
4054
});
4155

4256
/** @type {Parameters<chrome.tabs.TabUpdatedEvent['addListener']>[0]} */
43-
function attach(tabId, changed) {
57+
function courier(tabId, changed) {
4458
if (!ports.has(tabId) || changed.status !== 'loading') return;
4559

4660
chrome.scripting.executeScript({
@@ -94,3 +108,27 @@ function attach(tabId, changed) {
94108
},
95109
});
96110
}
111+
112+
chrome.tabs.onActivated.addListener(({ tabId }) => sensor(tabId));
113+
chrome.tabs.onUpdated.addListener(
114+
(tabId, changed) => changed.status === 'loading' && sensor(tabId),
115+
);
116+
117+
/** @param {number} tabId */
118+
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+
});
134+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

static/icons/disabled-128.png

4.58 KB
Loading

static/icons/disabled-16.png

537 Bytes
Loading

0 commit comments

Comments
 (0)