-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbackground.ts
More file actions
56 lines (49 loc) · 1.29 KB
/
background.ts
File metadata and controls
56 lines (49 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
export default defineBackground(() => {
const injectContentToTab = async (tab: chrome.tabs.Tab): Promise<void> => {
// Skip if URL is undefined
if (!tab.url) {
return;
}
// Skip if tab is discarded
if (tab.discarded) {
return;
}
// Skip if tab ID is undefined
if (tab.id === undefined) {
return;
}
// Skip if not a GitHub URL
if (!tab.url.startsWith('https://github.com/')) {
return;
}
try {
const manifest = chrome.runtime.getManifest();
// Inject CSS
if (manifest.content_scripts?.[0]?.css) {
await chrome.scripting.insertCSS({
target: { tabId: tab.id },
files: manifest.content_scripts[0].css,
});
}
// Inject JavaScript
if (manifest.content_scripts?.[0]?.js) {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: manifest.content_scripts[0].js,
});
}
} catch (error) {
console.error('Error injecting content script:', error);
}
};
// Update extension content for tabs
chrome.tabs.query({}, async (tabs: chrome.tabs.Tab[]) => {
for (const tab of tabs) {
try {
await injectContentToTab(tab);
} catch (e) {
console.error(e);
}
}
});
});