-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
132 lines (116 loc) · 3.33 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
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
let sites = {};
const subdomains = [
"www", "support", "mail", "ssl", "new", "cgi1", "en", "myaccount", "meta",
"help", "edit", "it", "da", "de", "fr", "es"
];
// Function to update the sites list
async function updateSites() {
try {
const response = await fetch(
"https://raw.githubusercontent.com/Fastbyte01/Deletecy/master/sites.json"
);
if (response.ok) {
sites = await response.json();
chrome.storage.local.set({
sites: sites,
lastUpdated: Date.now(),
});
console.log("Sites updated successfully.");
} else {
console.error("Failed to fetch sites:", response.statusText);
}
} catch (error) {
console.error("Error fetching sites.json:", error);
}
}
// Wrapper function to handle async chrome.storage.local.get
function getStorageData(keys) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(keys, (result) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(result);
}
});
});
}
// Function to initialize and run the updater
async function runUpdater() {
try {
const { sites: cachedSites, lastUpdated } = await getStorageData([
"sites",
"lastUpdated",
]);
if (cachedSites) {
sites = cachedSites;
if (Date.now() - (lastUpdated || 0) > 86400000) {
await updateSites();
}
} else {
await updateSites();
}
} catch (error) {
console.error("Error accessing storage:", error);
}
}
// Function to extract hostname from URL
function getHostname(url, optsStrict = false) {
const urlObj = new URL(url);
if (optsStrict) {
return urlObj.hostname;
}
let hostname = urlObj.hostname;
subdomains.forEach((sub) => {
hostname = hostname.replace(`${sub}.`, "");
});
return hostname;
}
// Function to get site info for a specific URL
function getInfo(url) {
const strictHostname = getHostname(url, true);
for (const site of Object.values(sites)) {
for (const domain of site.domains) {
if (domain.includes(strictHostname)) {
return site;
}
}
}
const relaxedHostname = getHostname(url, false);
for (const site of Object.values(sites)) {
for (const domain of site.domains) {
if (domain.includes(relaxedHostname)) {
return site;
}
}
}
return false;
}
// Listener for requests from popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "getSiteInfo" && message.url) {
const info = getInfo(message.url);
sendResponse({ info });
}
return true; // Indicates asynchronous response
});
// Update icon and title on tab updates
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "loading") {
const info = getInfo(tab.url);
if (info) {
chrome.action.setTitle({ tabId, title: `${info.name}: ${info.notes || "No notes"}` });
chrome.action.setIcon({
tabId,
path: `img/icon_${info.difficulty}_38.png`, // Assumes icons for difficulty levels
});
chrome.action.enable(tabId);
} else {
chrome.action.setTitle({ tabId, title: "No account deletion info available" });
chrome.action.setIcon({ tabId, path: "img/icon_48.png" });
chrome.action.disable(tabId);
}
}
});
// Initialize the updater
runUpdater();