-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.ts
More file actions
234 lines (201 loc) · 7.46 KB
/
background.ts
File metadata and controls
234 lines (201 loc) · 7.46 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { storage } from './storage/index.js';
import { DNSLinkProbe } from './utils/dnslink.js';
class BackgroundManager {
private defaultIcon = {
'16': 'icons/icon16.png',
'48': 'icons/icon48.png',
'128': 'icons/icon128.png'
};
constructor() {
this.init();
}
private async init() {
await storage.init();
this.setupEventListeners();
}
private setupEventListeners() {
// Handle extension installation
chrome.runtime.onInstalled.addListener(async (details) => {
if (details.reason === 'install') {
console.log('IPFS QuickLaunch extension installed');
// Initialize storage with default settings
await storage.init();
}
});
// Handle extension startup
chrome.runtime.onStartup.addListener(async () => {
console.log('IPFS QuickLaunch extension started');
await storage.init();
});
// Handle messages from popup or content scripts
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
this.handleMessage(request, sender, sendResponse);
return true; // Keep message channel open for async responses
});
// Handle tab updates for DNSLink detection
chrome.tabs.onUpdated.addListener(async (_tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
await this.checkTabForDNSLink(tab);
}
});
// Handle tab activation to update icon
chrome.tabs.onActivated.addListener(async (activeInfo) => {
const tab = await chrome.tabs.get(activeInfo.tabId);
if (tab.url) {
await this.checkTabForDNSLink(tab);
}
});
}
private async handleMessage(request: any, _sender: chrome.runtime.MessageSender, sendResponse: (response?: any) => void) {
try {
switch (request.type) {
case 'GET_APPS':
const apps = await storage.getAllApps();
sendResponse({ success: true, data: apps });
break;
case 'CREATE_APP':
const newApp = await storage.createApp(request.data);
sendResponse({ success: true, data: newApp });
break;
case 'UPDATE_APP':
const updatedApp = await storage.updateApp(request.data);
sendResponse({ success: true, data: updatedApp });
break;
case 'DELETE_APP':
const deleted = await storage.deleteApp(request.data.id);
sendResponse({ success: true, data: deleted });
break;
case 'CREATE_VERSION':
const appWithNewVersion = await storage.createVersion(request.data);
sendResponse({ success: true, data: appWithNewVersion });
break;
case 'UPDATE_LAST_USED':
await storage.updateLastUsed(request.data.appId);
sendResponse({ success: true });
break;
case 'GET_SETTINGS':
const settings = await storage.getSettings();
sendResponse({ success: true, data: settings });
break;
case 'UPDATE_SETTINGS':
const updatedSettings = await storage.updateSettings(request.data);
sendResponse({ success: true, data: updatedSettings });
break;
case 'CHECK_DNSLINK':
try {
const gatewayConfig = await storage.getGatewayConfig();
const result = await DNSLinkProbe.probe(request.data.domain, gatewayConfig.dnsOverHttpsUrl);
sendResponse({ success: true, data: result });
} catch (error) {
sendResponse({ success: false, error: error instanceof Error ? error.message : 'DNSLink check failed' });
}
break;
case 'GET_CURRENT_TAB':
try {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (tabs[0]?.url) {
const url = new URL(tabs[0].url);
sendResponse({ success: true, data: { domain: url.hostname, url: tabs[0].url } });
} else {
sendResponse({ success: false, error: 'No active tab found' });
}
} catch (error) {
sendResponse({ success: false, error: error instanceof Error ? error.message : 'Failed to get current tab' });
}
break;
case 'IPFS_PATH_DETECTED':
// Content script detected IPFS path header
console.log('IPFS path detected:', request.data);
// We could store this information or trigger notifications here
sendResponse({ success: true });
break;
default:
sendResponse({ success: false, error: 'Unknown message type' });
}
} catch (error) {
console.error('Background script error:', error);
sendResponse({ success: false, error: error instanceof Error ? error.message : 'Unknown error' });
}
}
/**
* Check tab for DNSLink and update extension icon accordingly
*/
private async checkTabForDNSLink(tab: chrome.tabs.Tab): Promise<void> {
if (!tab.url || !tab.id) return;
try {
const url = new URL(tab.url);
// Skip non-web URLs
if (!url.hostname || url.protocol !== 'https:' && url.protocol !== 'http:') {
await this.setDefaultIcon(tab.id);
return;
}
// Check for DNSLink and x-ipfs-path
const gatewayConfig = await storage.getGatewayConfig();
const dnslinkResult = await DNSLinkProbe.probe(url.hostname, gatewayConfig.dnsOverHttpsUrl);
if (dnslinkResult.hasDNSLink || dnslinkResult.hasIPFSPath) {
await this.setDNSLinkIcon(tab.id);
// Update action badge based on detection method
const badgeIcon = dnslinkResult.detectionMethod === 'x-ipfs-path' ? '📁' : '🔗';
await chrome.action.setBadgeText({
text: badgeIcon,
tabId: tab.id
});
await chrome.action.setBadgeBackgroundColor({
color: '#3498db',
tabId: tab.id
});
// Update title based on detection method
const detectionType = dnslinkResult.detectionMethod === 'x-ipfs-path' ? 'IPFS content' : 'DNSLink';
await chrome.action.setTitle({
title: `IPFS QuickLaunch - ${detectionType} detected on ${url.hostname}`,
tabId: tab.id
});
} else {
await this.setDefaultIcon(tab.id);
await chrome.action.setBadgeText({
text: '',
tabId: tab.id
});
await chrome.action.setTitle({
title: 'IPFS QuickLaunch',
tabId: tab.id
});
}
} catch (error) {
console.error('Error checking tab for DNSLink:', error);
if (tab.id) {
await this.setDefaultIcon(tab.id);
}
}
}
/**
* Set default extension icon
*/
private async setDefaultIcon(tabId: number): Promise<void> {
try {
await chrome.action.setIcon({
path: this.defaultIcon,
tabId
});
} catch (error) {
console.error('Failed to set default icon:', error);
}
}
/**
* Set DNSLink detected icon (highlighted version)
*/
private async setDNSLinkIcon(tabId: number): Promise<void> {
try {
// For now, we'll use the same icon but with badge
// In the future, we could create special "highlighted" versions
await chrome.action.setIcon({
path: this.defaultIcon,
tabId
});
} catch (error) {
console.error('Failed to set DNSLink icon:', error);
}
}
}
// Initialize background manager
new BackgroundManager();