-
Notifications
You must be signed in to change notification settings - Fork 9
/
background.js
105 lines (96 loc) · 3.06 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
//Listen for messages from the content script and perform actions with the local storage.
chrome.runtime.onMessage.addListener((message, sender,sendResponse)=> {
console.log("Message received in background.js:", message);
const target = message?.target || null;
if (message.action === "showNotification") {
console.log("Received showNotification message");
chrome.notifications.create({
type: "basic",
title: "PleasantWeb - Inappropriate content detected. 🚨",
message: "The content detected in the current page may be inappropriate. Proceed at your own risk.",
iconUrl: "assets/Logo128.png"
});
}
switch (target) {
case "setStorageData":
setStorageData(message.data);
break;
case "getStorageData":
getStorageData().then(returnData => {
console.log("Sending data from background.js:", returnData);
sendResponse(returnData);
}).catch(error => {
console.error(error);
sendResponse([]);
});
break;
case "setTheme":
setTheme(message.theme)
break;
case "updateCheckedFields":
chrome.storage.local.set({ pwExtensionOptions : JSON.stringify(message.checkedFields) });
break;
case "readCheckedFields":
getExtensionOptions().then(extensionOptions => {
sendResponse(extensionOptions);
});
break;
}
return true;
})
async function setTheme(info) {
getTheme().then(storedData => {
storedData = info;
chrome.storage.local.set({ theme: JSON.stringify(storedData) });
}).catch(error => {
console.error(error);
});
}
async function getTheme(){
try{
const data = await new Promise((resolve, reject) => {
chrome.storage.local.get(["theme"], function (result) {
resolve(JSON.parse(result.theme || "[]"));
});
})
return data;
}
catch (error){
console.error(error);
return [];
}
}
function setStorageData(data) {
getStorageData().then(storedData => {
storedData.push(data);
chrome.storage.local.set({ pw: JSON.stringify(storedData) });
}).catch(error => {
console.error(error);
});
}
async function getStorageData() {
try {
const data = await new Promise((resolve, reject) => {
chrome.storage.local.get(["pw"], function (result) {
resolve(JSON.parse(result.pw || "[]"));
});
});
return data;
} catch (error) {
console.error("Error while retrieving data from local storage:", error);
return [];
}
}
async function getExtensionOptions() {
try {
const data = await new Promise((resolve, reject) => {
chrome.storage.local.get(["pwExtensionOptions"], function (result) {
resolve(JSON.parse(result.pwExtensionOptions || "[]"));
});
});
return data;
} catch (error) {
console.error("Error while retrieving data from local storage:", error);
return [];
}
}