-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
274 lines (238 loc) · 9.33 KB
/
background.js
File metadata and controls
274 lines (238 loc) · 9.33 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Context Menu Creation
chrome.contextMenus.create({
title: "What's that link?",
contexts: ['link'],
id: 'summarizeLink'
});
// Context Menu initialization
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === 'summarizeLink') {
const linkUrl = info.linkUrl;
const currentDomain = new URL(tab.url).hostname;
const linkType = identifyLinkType(linkUrl, currentDomain);
console.log(linkType[0]);
const isSafe = await checkLinkSafety(linkUrl);
console.log('Link safety:', isSafe ? 'Safe' : 'Unsafe');
// Send message to popupScript to display a popup skeleton on load
chrome.tabs.sendMessage(tab.id, {action: 'displayPopup', linkType: linkType[0], isSafe: isSafe})
if (linkType[0] === 'internal' || linkType[0] === 'external') {
sendProxiedLink(info, tab, linkType, isSafe);
}
// Discontinued feature to analyze download links
else if (linkType[0] === 'download') {
fileExtension = linkType[1];
}
// Call function to get the rating of webpage
await getUrlRatings(linkUrl, tab);
}
});
// Link categorization
function identifyLinkType(linkUrl, currentDomain) {
const downloadExtensions = ['.pdf', '.zip', '.jpg', '.png', '.mp3', '.mp4', '.doc', '.xls', '.exe', '.docx', '.apk'];
const fileExtension = linkUrl.slice(linkUrl.lastIndexOf('.')).toLowerCase();
// Check if the link is an internal link
const isInternal = linkUrl.includes(currentDomain);
// Check if the link is a download link
const isDownload = downloadExtensions.some(ext => fileExtension === ext);
let type;
// Determine the type of link
if (isDownload) {
type = 'download';
} else if (isInternal) {
type = 'internal';
} else {
type = 'external';
}
const linkInfo = [type, isDownload ? fileExtension : null];
return linkInfo;
}
// Send cors proxied link to parserScript for fetching the content
async function sendProxiedLink(info, tab, linkType) {
const linkUrl = info.linkUrl;
const corsProxy = 'http://localhost:8080/';
const proxiedUrl = corsProxy + linkUrl;
try {
// Sends the URL to the parserScript
chrome.tabs.sendMessage(tab.id, { action: 'fetchAndParse', url: proxiedUrl, linkType: linkType, plainUrl: linkUrl });
console.log("Sent message to parser script");
} catch (error) {
console.error('Error sending URL to content script:', error);
}
}
// Gemini API comms
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
const API_URL = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=AIzaSyA5h89GaEy6DlnEV0s_fsBQXvKN4wKxzaM';
if (message.action === 'summarizeText') {
const textContent = message.text;
let prompt = "";
if(textContent === "") prompt = "";
try {
let response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
contents: [{
parts: [{
text: `Summarize this webpage very shortly without using filler text and being straight to the point and if it is about a particuar topic dont talk about the website and just talk abou the topic and never use markdown syntax: ${textContent}`
}]
}]
})
});
if (!response.ok)
throw new Error('Network response was not ok: ' + response.statusText);
let data = await response.json();
console.log('Fetch data received:', data);
// Take only the summary text
if(data.candidates[0].content) {
const summary = data.candidates[0].content.parts[0].text;
chrome.tabs.sendMessage(sender.tab.id, { action: 'updatePopup', summaryText: summary, title: message.title, url: message.url });
}
else {
console.log("Error occured");
chrome.scripting.executeScript({
target: {tabId: sender.tab.id},
func: (message) => alert(message),
args: ["Sorry could you try again?"]
});
}
}
catch (error) {
console.error('Error during fetch or processing:', error);
chrome.scripting.executeScript({
target: {tabId: tab.id},
func: (errorMessage) => alert(errorMessage),
args: [error.message]
});
}
}
});
// Safe browsing API comms
const checkLinkSafety = async (linkUrl) => {
const safeBrowsingAPI = `https://safebrowsing.googleapis.com/v4/threatMatches:find?key=AIzaSyCczjH1d70T3uCAjvmCOCxVV8o5TdAkeOw`;
const body = {
client: {
clientId: "your-client-id",
clientVersion: "1.0"
},
threatInfo: {
threatTypes: ["MALWARE", "SOCIAL_ENGINEERING", "UNWANTED_SOFTWARE"],
platformTypes: ["ANY_PLATFORM"],
threatEntryTypes: ["URL"],
threatEntries: [
{ url: linkUrl }
]
}
};
try {
const response = await fetch(safeBrowsingAPI, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
const data = await response.json();
// If the response contains matches, the link is unsafe
if (data.matches && data.matches.length > 0) {
return false;
} else {
return true;
}
} catch (error) {
console.error('Error checking link safety:', error);
return false; // Assume unsafe if error occurs
}
}
// VirusTotal API key
const apiKey = 'ff01f664ef0ae500365ab56607ecde80d505cf09c2089f54a9554916004c446e';
// Main function to scan and get the report
async function getUrlRatings(linkUrl, tab) {
try {
const analysisId = await submitURLForScan(linkUrl);
setTimeout(async () => {
const result = await getAnalysisResult(analysisId);
console.log("Analysis report:", result);
chrome.tabs.sendMessage(tab.id, {action: 'updateRating', report: result}, (response)=> {
if(chrome.runtime.lastError)
console.error("Error:", chrome.runtime.lastError);
else {
if(response.status === 'success') console.log("Rating updated successfully!");
else console.log("Failed to update rating.");
}
});
}, 20000);
} catch (error) {
console.error("Error:", error);
}
}
// Function to submit the URL to the API for scanning and get the analysis ID
async function submitURLForScan(url) {
const urlEncoded = encodeURIComponent(url);
const response = await fetch('https://www.virustotal.com/api/v3/urls', {
method: 'POST',
headers: {
'x-apikey': apiKey,
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `url=${urlEncoded}`
});
const data = await response.json();
const analysisId = data.data.id;
console.log("Analysis ID:", analysisId);
return analysisId;
}
// Function to poll for the analysis result using the ID
async function getAnalysisResult(analysisId) {
const url = `https://www.virustotal.com/api/v3/analyses/${analysisId}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'x-apikey': apiKey
}
});
const result = await response.json();
console.log("Analysis report in getAnalysisResult: " , result)
return result;
}
// Communication between content scripts
// Actions: 6, 7, 4, 8
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// From: popupScript To: popupHTML
if(message.action == 'createPopup') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, message, (response) => {
sendResponse(response);
});
});
return true;
}
// From: popupScript To: bookmarkScript
if (message.action === 'saveBookmark') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, message, (response) => {
sendResponse(response);
});
});
return true;
}
// From: popupHTML To: popupHTML
if (message.action === 'displayBookmarks') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, message, (response) => {
sendResponse(response);
});
});
return true;
}
// From: popupHTML To: bookmarkScript
if(message.action == 'deleteBookmark') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, message, (response) => {
sendResponse(response);
});
});
return true;
}
});