-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
52 lines (49 loc) · 1.77 KB
/
background.js
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
let phishingData;
chrome.runtime.onMessage.addListener(function (request, sender = "content.js", sendResponse) {
if (request.action === 'analyzePage') {
const data = request.data;
// Call the function to initiate analysis with the received data
initiateAnalysis(data, sendResponse);
}
return true;
});
function initiateAnalysis(data, sendResponse) {
// Making a request to localhost machine learning model
const modelEndpoint = 'http://127.0.0.1:5000/predict';
fetch(modelEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.text()) // Assuming the ML model sends a JSON string
.then(jsonString => {
try {
const result = JSON.parse(jsonString);
if (typeof result.my_integer === 'number') {
const phishingScore = result.my_integer;
chrome.storage.local.set({ 'phishingScore': phishingScore }, function () {
sendResponse({ phishingScore: phishingScore });
});
} else {
console.error('Error: ML model response does not contain a valid "my_integer" field.');
sendResponse({ phishingScore: 0 });
}
} catch (error) {
console.error('Error parsing JSON:', error);
sendResponse({ phishingScore: 0 });
}
})
.catch(error => {
console.error('Error analyzing page:', error);
sendResponse({ phishingScore: 0 });
});
}
chrome.runtime.onMessage.addListener(function (request, sender = "popup.js", sendResponse) {
if (request.action === 'initiateAnalysis') {
initiateAnalysis(phishingData, sendResponse);
sendResponse({ message: "Action perfomed" });
}
return true;
});