-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
84 lines (73 loc) · 2.5 KB
/
Copy pathbackground.js
File metadata and controls
84 lines (73 loc) · 2.5 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
// Keep service worker alive
chrome.alarms.create('keep-alive', { periodInMinutes: 1 });
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'keep-alive') {
console.log('Service worker active');
}
});
// Replace with your Google Safe Browsing API key
const API_KEY = 'YOUR_API_KEY';
// Check URL safety (only allow HTTP/HTTPS URLs)
async function checkUrlSafety(url) {
try {
// Validate URL format
if (!url || !url.startsWith('http')) {
console.warn('Skipping non-HTTP URL:', url || 'undefined');
return false;
}
// API request setup
const apiUrl = `https://safebrowsing.googleapis.com/v4/threatMatches:find?key=${API_KEY}`;
const requestBody = {
client: { clientId: 'scam-detector', clientVersion: '1.0' },
threatInfo: {
threatTypes: ['MALWARE', 'SOCIAL_ENGINEERING'],
platformTypes: ['ANY_PLATFORM'],
threatEntryTypes: ['URL'],
threatEntries: [{ url: encodeURI(url) }]
}
};
// Send API request
const response = await fetch(apiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestBody)
});
// Handle response
const data = await response.json();
return data.matches ? true : false;
} catch (error) {
console.error('API Error:', error);
return false;
}
}
// Listen for URL changes in tabs
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
// Only process if URL changes and is valid
if (changeInfo.url && tab.url?.startsWith('http')) {
console.log("Checking URL:", changeInfo.url);
const isUnsafe = await checkUrlSafety(changeInfo.url);
if (isUnsafe) {
chrome.scripting.executeScript({
target: { tabId },
func: (message) => alert(`⚠️ ${message}`),
args: ['This site is flagged as unsafe!']
});
}
}
});
// Listen for tab switches (edge cases)
chrome.tabs.onActivated.addListener(async (activeInfo) => {
const tab = await chrome.tabs.get(activeInfo.tabId);
// Validate tab URL
if (tab?.url?.startsWith('http')) {
console.log("Checking URL (activated tab):", tab.url);
const isUnsafe = await checkUrlSafety(tab.url);
if (isUnsafe) {
chrome.scripting.executeScript({
target: { tabId: activeInfo.tabId },
func: (message) => alert(`⚠️ ${message}`),
args: ['This site is flagged as unsafe!']
});
}
}
});