-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
53 lines (49 loc) · 1.66 KB
/
background.js
File metadata and controls
53 lines (49 loc) · 1.66 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
const BACKEND_URL = "http://127.0.0.1:5000/predict";
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "checkURL") {
const { url } = message;
// Validate URL
if (!url || !url.startsWith("http")) {
console.error("Invalid URL:", url);
sendResponse({ isPhishing: false });
return;
}
// Send URL to backend for analysis
fetch(BACKEND_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ url }),
})
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then((data) => {
sendResponse({ isPhishing: data.isPhishing });
})
.catch((error) => {
console.error("Error contacting backend:", error);
sendResponse({ isPhishing: false });
});
return true; // Keep the messaging channel open for async responses
} else if (message.action === "whitelistCurrentSite") {
chrome.storage.local.get("whitelist", ({ whitelist }) => {
whitelist = whitelist || [];
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const url = new URL(tabs[0].url).hostname;
if (!whitelist.includes(url)) {
whitelist.push(url);
chrome.storage.local.set({ whitelist });
console.log(`Added ${url} to whitelist.`);
}
});
});
} else if (message.action === "viewLogs") {
// Open logs page from the logs folder
chrome.tabs.create({ url: chrome.runtime.getURL("logs/logs.html") });
}
});