-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
211 lines (192 loc) · 7.07 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
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
const ICON_DEFAULT_PATHS = {
"16": chrome.runtime.getURL("/assets/img/icons/normal/icon16.png"),
"32": chrome.runtime.getURL("/assets/img/icons/normal/icon32.png"),
"48": chrome.runtime.getURL("/assets/img/icons/normal/icon48.png"),
"64": chrome.runtime.getURL("/assets/img/icons/normal/icon64.png"),
"96": chrome.runtime.getURL("/assets/img/icons/normal/icon96.png"),
"128": chrome.runtime.getURL("/assets/img/icons/normal/icon128.png"),
"256": chrome.runtime.getURL("/assets/img/icons/normal/icon256.png")
};
const ICON_BINARY_PATHS = {
"16": chrome.runtime.getURL("/assets/img/icons/binarycode/icon16.png"),
"32": chrome.runtime.getURL("/assets/img/icons/binarycode/icon32.png"),
"48": chrome.runtime.getURL("/assets/img/icons/binarycode/icon48.png"),
"64": chrome.runtime.getURL("/assets/img/icons/binarycode/icon64.png"),
"96": chrome.runtime.getURL("/assets/img/icons/binarycode/icon96.png"),
"128": chrome.runtime.getURL("/assets/img/icons/binarycode/icon128.png"),
"256": chrome.runtime.getURL("/assets/img/icons/binarycode/icon256.png")
}
const ICON_WEB_KEY_PATHS = {
"16": chrome.runtime.getURL("/assets/img/icons/web-security/icon16.png"),
"32": chrome.runtime.getURL("/assets/img/icons/web-security/icon32.png"),
"48": chrome.runtime.getURL("/assets/img/icons/web-security/icon48.png"),
"64": chrome.runtime.getURL("/assets/img/icons/web-security/icon64.png"),
"96": chrome.runtime.getURL("/assets/img/icons/web-security/icon96.png"),
"128": chrome.runtime.getURL("/assets/img/icons/web-security/icon128.png"),
"256": chrome.runtime.getURL("/assets/img/icons/web-security/icon256.png")
}
const ICON_HACKER_PATHS = {
"16": chrome.runtime.getURL("/assets/img/icons/hacker/icon16.png"),
"32": chrome.runtime.getURL("/assets/img/icons/hacker/icon32.png"),
"48": chrome.runtime.getURL("/assets/img/icons/hacker/icon48.png"),
"64": chrome.runtime.getURL("/assets/img/icons/hacker/icon64.png"),
"96": chrome.runtime.getURL("/assets/img/icons/hacker/icon96.png"),
"128": chrome.runtime.getURL("/assets/img/icons/hacker/icon128.png"),
"256": chrome.runtime.getURL("/assets/img/icons/hacker/icon256.png")
}
const ICON_SMART_KEY_PATHS = {
"16": chrome.runtime.getURL("/assets/img/icons/smart-key/icon16.png"),
"32": chrome.runtime.getURL("/assets/img/icons/smart-key/icon32.png"),
"48": chrome.runtime.getURL("/assets/img/icons/smart-key/icon48.png"),
"64": chrome.runtime.getURL("/assets/img/icons/smart-key/icon64.png"),
"96": chrome.runtime.getURL("/assets/img/icons/smart-key/icon96.png"),
"128": chrome.runtime.getURL("/assets/img/icons/smart-key/icon128.png"),
"256": chrome.runtime.getURL("/assets/img/icons/smart-key/icon256.png")
}
chrome.runtime.onInstalled.addListener(function () {
// Initialisiere den Speicher
chrome.storage.sync.set({
links: [],
iconPath: ICON_DEFAULT_PATHS
},
function () {
console.log('Linklist initialized!');
});
});
chrome.runtime.onStartup.addListener(function () {
loadAddonIcon();
});
function loadAddonIcon() {
chrome.storage.sync.get("iconPath", function (data) {
const iconPath = data.iconPath;
if (iconPath) {
changeAddonIcon(iconPath);
savedIconPath = iconPath;
console.log("Addon icon loaded:", iconPath);
}
});
}
function addLink(link) {
chrome.storage.sync.get('links', function (data) {
var links = data.links;
links.push(link);
chrome.storage.sync.set({
links: links
}, function () {
console.log('Link added:', link);
});
});
}
function saveAddonIcon(iconPath) {
chrome.storage.sync.set({
iconPath: iconPath
}, function () {
savedIconPath = iconPath;
console.log('Addon icon saved:', iconPath);
});
}
// remove hyperlink or notice from browser storage
function removeLink(link) {
chrome.storage.sync.get('links', function (data) {
var links = data.links;
var index = links.indexOf(link);
if (index > -1) {
links.splice(index, 1);
chrome.storage.sync.set({
links: links
}, function () {
console.log('Link removed:', link);
});
}
});
}
function openOptionsPage() {
chrome.tabs.create({
url: 'src/options.html'
});
}
function openSocialLink(type) {
const validTypes = ["github", "codepen", "youtube", "thm"];
if (validTypes.includes(type)) {
let url;
if (type === "github") {
url = 'https://www.github.com/sera619';
} else if (type === "codepen") {
url = 'https://www.codepen.io/sera619';
} else if (type === "youtube") {
url = 'https://www.youtube.com/@S3R43o3';
} else if (type === "thm") {
url = 'https://www.tryhackme.com/p/S3R43o3';
}
chrome.tabs.create({
url
});
} else {
console.error("Invalid social link type:", type);
}
}
function openNotification(message) {
// validate and clear message for injections
const validMessage = sanitizeMessage(message);
chrome.notifications.create({
type: 'basic',
iconUrl: chrome.runtime.getURL('/assets/img/icons/hacker/icon64.png'),
title: 'Benachrichtigung',
message: validMessage
});
}
function sanitizeMessage(message) {
const injectionRegex = /<script\b[^>]*>([\s\S]*?)<\/script>/gi;
const sanitizedMessage = message.replace(injectionRegex, '');
return sanitizedMessage;
}
function changeAddonIcon(iconPath) {
var final_path = null;
switch (iconPath) {
case "normal":
final_path = ICON_DEFAULT_PATHS;
break;
case "binary":
final_path = ICON_BINARY_PATHS;
break;
case "web-key":
final_path = ICON_WEB_KEY_PATHS;
break;
case "smart-key":
final_path = ICON_SMART_KEY_PATHS;
break;
case "hacker":
final_path = ICON_HACKER_PATHS;
break;
default:
break;
}
if (final_path === null) {
//console.log("icon not updated");
return;
} else {
//console.log("Icon updated");
chrome.action.setIcon({
path: final_path
});
}
}
// listen on events from popup window
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === 'addLink') {
addLink(request.link);
} else if (request.action === 'removeLink') {
removeLink(request.link);
} else if (request.action === 'openOption') {
openOptionsPage();
} else if (request.action === 'openSocial') {
openSocialLink(request.link);
} else if (request.action === 'notify') {
openNotification(request.link);
} else if (request.action === 'changeIcon') {
changeAddonIcon(request.link);
saveAddonIcon(request.link);
} else if (request.action === 'loadIcon') {
loadAddonIcon();
}
});