-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
87 lines (72 loc) · 2.21 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
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
// StopMalwareContent's Source Code for Firefox extension
// Inspired from https://github.com/StopModReposts/Extension
const API_URL = "https://smc-api.lodine.xyz/sites";
let cachedSites = [];
let ignoreList = [];
let lastBlockedSite = {
domain: "",
reason: "",
notes: "",
path: "",
url: "",
};
refreshCache();
function refreshCache() {
fetch(API_URL)
.then((response) => response.json())
.then((response) => {
cachedSites = response;
});
}
// some complex code
chrome.runtime.onMessage.addListener((message, _, sendResponse) => {
if (message.type === "get-blocked-site") {
return sendResponse(lastBlockedSite);
}
if (message.type === "add-to-ignore") {
ignoreList.push(lastBlockedSite.domain);
console.log(ignoreList);
return sendResponse(null);
}
});
let lastNavUrl = "";
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// If URL is empty, skip
if (!tab.url) return;
// If URL has not changed, skip
if (lastNavUrl == tab.url) return;
// Making URL a real URL
const parsed = new URL(tab.url);
// Getting domain from URL
const host = parsed.host;
// We will need that for future
lastNavUrl = tab.url;
// Check if the site is flagged
const flaggedSites = cachedSites.filter(
(site) => site.domain === host || parsed.host.endsWith(`.${site.domain}`),
);
if (!flaggedSites.length) return;
// Iterate through all flagged sites for this domain
for (const site of flaggedSites) {
// Check if user ignored this domain
const isIgnored = ignoreList.includes(site.domain);
if (isIgnored) continue;
// Check if the path matches
const pathCorrect = site.path
? parsed.pathname.startsWith(site.path)
: true;
if (!pathCorrect) continue;
// Set variables so alert.html page will know site's domain, reason, and URL
lastBlockedSite.domain = site.domain;
lastBlockedSite.reason = site.reason;
lastBlockedSite.notes = site.notes;
lastBlockedSite.path = site.path;
lastBlockedSite.url = tab.url;
// Redirect to the alert page
chrome.tabs.update(tabId, {
url: chrome.runtime.getURL(`/html/alert.html`),
});
// Exit the loop as we found a match and handled it
break;
}
});