-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
86 lines (75 loc) · 2.13 KB
/
background.js
File metadata and controls
86 lines (75 loc) · 2.13 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
const CONFIG = {
API_URLS: {
ip: 'https://www.virustotal.com/api/v3/ip_addresses/',
domain: 'https://www.virustotal.com/api/v3/domains/',
hash: 'https://www.virustotal.com/api/v3/files/'
}
};
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'fetchIndicatorData') {
fetchIndicatorData(request.value, request.type)
.then(data => sendResponse({ success: true, data }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true;
}
if (request.action === 'updateBadge') {
if (sender.tab && sender.tab.id) {
updateBadge(sender.tab.id, request.count);
}
}
});
async function fetchIndicatorData(value, type) {
const result = await chrome.storage.sync.get('vtApiKey');
const apiKey = result.vtApiKey;
if (!apiKey) {
throw new Error('No API key configured. Please add your VirusTotal API key in extension settings.');
}
const baseUrl = CONFIG.API_URLS[type];
if (!baseUrl) {
throw new Error(`Unsupported indicator type: ${type}`);
}
const response = await fetch(`${baseUrl}${value}`, {
headers: {
'x-apikey': apiKey
}
});
if (!response.ok) {
if (response.status === 429) {
throw new Error('API rate limit exceeded');
}
if (response.status === 401) {
throw new Error('Invalid API key. Please check your settings.');
}
if (response.status === 404) {
throw new Error('Not found in VirusTotal database');
}
throw new Error(`API error: ${response.status}`);
}
const data = await response.json();
return data;
}
function updateBadge(tabId, count) {
if (count > 0) {
chrome.action.setBadgeText({
tabId: tabId,
text: count.toString()
});
chrome.action.setBadgeBackgroundColor({
tabId: tabId,
color: '#667eea'
});
} else {
chrome.action.setBadgeText({
tabId: tabId,
text: ''
});
}
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'loading') {
chrome.action.setBadgeText({
tabId: tabId,
text: ''
});
}
});