-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
424 lines (358 loc) · 12.3 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
var alerts = 0;
var alerts_old = 0;
var messages = 0;
var messages_old = 0;
var my_notids_alerts = [];
var my_notids_messages = [];
var my_notids_messages_urls = [];
var updated_notid = -1;
var fools = false;
var background = true;
var customNewTheme = false;
var customNewThemeData = "";
var iconPath = "img/Korra/";
var loaded = false;
var theurl = 'https://projectkorra.com/forum';
var tempdata;
var debugEnabled = false;
var secretOptions = [];
var themedata = [];
var updatedThemes = true;
function checkEverything() {
if (!background) {
chrome.tabs.getAllInWindow(function(tabs) {
for (var i in tabs) {
tab = tabs[i];
debug("URL: " + tab.url + " | " + tab.url.startsWith(theurl))
if (tab.url.startsWith(theurl)) {
actuallyCheckStuff();
break;
}
}
});
} else {
actuallyCheckStuff();
}
}
function actuallyCheckStuff() {
$.ajax({
url: theurl,
success: function(data) {
data = data.replace(/\"\/\//g, "\"http://");
checkNotifications(data);
}
});
}
function makeNotification(title, message, minimessage) {
if (typeof minimessage == "undefined") {
minimessage = "";
}
chrome.notifications.create({
type: "basic",
iconUrl: iconPath + "icon128.png",
title: title,
message: message,
contextMessage: (title.startsWith("New M") ? "" : minimessage)
}, function(notificationid) {
if (title.startsWith("New A")) {
my_notids_alerts.push(notificationid);
} else if (title.startsWith("New M")) {
my_notids_messages.push(notificationid);
my_notids_messages_urls[notificationid] = minimessage;
} else if (title.startsWith("Extension")) {
updated_notid = notificationid;
}
});
new Audio("notification.mp3").play();
}
chrome.notifications.onClosed.addListener(function(notificationid, byuser) {
if (my_notids_alerts.indexOf(notificationid) > -1) {
my_notids_alerts.pop(notificationid);
} else if (my_notids_messages.indexOf(notificationid) > -1) {
my_notids_messages.pop(notificationid);
} else if (typeof my_notids_messages_urls[notificationid] === 'undefined') {
my_notids_messages_urls[notificationid] = 'undefined';
}
});
chrome.notifications.onClicked.addListener(function(notificationid) {
if (my_notids_alerts.indexOf(notificationid) > -1) {
my_notids_alerts.pop(notificationid);
chrome.notifications.clear(notificationid);
window.open(theurl + "/account/alerts");
} else if (my_notids_messages.indexOf(notificationid) > -1) {
my_notids_messages.pop(notificationid);
chrome.notifications.clear(notificationid);
window.open(my_notids_messages_urls[notificationid]);
my_notids_messages_urls[notificationid] = 'undefined';
} else if (updated_notid == notificationid) {
if (!fools) {window.open("changelog.html");}
else {window.open("https://www.youtube.com/watch?v=JSz8nK9yaeY");}
}
});
chrome.runtime.onInstalled.addListener(function(a) {
if (new Date().getDate() == 1 && new Date().getMonth() == 3) {
fools = true;
}
if (secretOptions.length == 0) {
addSecretKey("noadsplease");
}
if (a.reason === "update" || fools) {
var v = chrome.runtime.getManifest().version;
/*if (fools) {
var s = "TkHe ekhtenion is bRoken! PleaSe re-dovNload it aT onxe to sTuop pRoblens from ocsturrIng!";
makeNotification("Extension Broken", s, "Click for details!");
}*/
//else {
makeNotification("Extension Updated", "ProjectKorra Notifier has been updated to version " + v + "!", "Click for details!");
//}
if (v == "1.4.5") {
makeNotification("Thank you!", "Thanks for continuing to use PK Notification! As thanks, everyone now gets an ad free experience on the forums!");
}
}
});
function checkNotifications(data) {
alerts_old = alerts;
messages_old = messages;
alerts = parseInt($(data).find("a.badgeContainer[href='/forum/account/alerts']").attr("data-badge"));
messages = parseInt($(data).find("a.badgeContainer[href='/forum/conversations/']").attr("data-badge"));
chrome.storage.local.set({
'alerts': alerts
});
chrome.storage.local.set({
'messages': messages
});
var total = alerts + messages;
chrome.browserAction.setBadgeText({
text: (total == 0) ? "" : total.toString()
});
if (total > 0) {
if (alerts > alerts_old) {
alerts_old = alerts;
makeNotification("New Alert(s)", "You've got " + alerts + " new alert" + (alerts == 1 ? "" : "s") + "!");
}
if (messages > messages_old) {
messages_old = messages;
//Bye bye old message system!
//makeNotification("New Message(s)", "You've got " + messages + " unread message" + (messages == 1 ? "" : "s") + "!");
$.ajax({
url: theurl + "/conversations/",
success: function(data) {
checkMessages($.parseHTML(data));
}
});
}
}
}
function checkMessages(data) {
if (data !== NaN) {
//map = new Map(); //User, number
people = [];
msgs = 0;
for (var i = 0; i < messages_old; i++) {
tempdata = data;
//console.log(data.length);
//debug("i = " + $(data).find("li.unread").get(i).getAttribute("data-author"))
str2 = $(tempdata).find("div.is-unread").children("div.structItem-cell--latest").children("div.structItem-minor").text();
//debug("Object: " + String(str2));
//debug("Name of messagew: " + String(stringg));
if (people.indexOf(str2) == -1 && str2 != '') {
people.push(str2);
}
msgs += 1;
break; //This is temp. Currently it only gets the latest message from the latest member so until that is fixed, we break
}
//debug("Map: " + String(map.keys().toString()));
if (people.length == 1) {
url = $(tempdata).find("div.is-unread").children("div.structItem-cell--main").children("a")[0].getAttribute("href");
//debug("Count: " + msgs + " | " + people.toString() + " | " + (theurl + "/" + url));
makeNotification("New Message", "You've got " + msgs + " new message" + (msgs == 1 ? "" : "s") + " from " + people[0] + "!", "https://projectkorra.com" + url);
}
else if (people.length == 2) {
makeNotification("New Messages", "You've got " + msgs + " new messages from " + people[0] + " and " + people[1] + "!", theurl + "/conversations/");
}
else if (people.length >= 3) {
makeNotification("New Messages", "You've got " + msgs + " new messages from " + people[0] + " and " + (people.length - 1) + " other people!", theurl + "/conversations/");
}
} else {
debug("Failed!");
}
}
function debug(string) {
if (debugEnabled) makeNotification("DEBUG", string, theurl);
}
function generateHash(string) {
var hash = 0;
if (string.length == 0) return hash;
for (i = 0; i < string.length; i++) {
char = string.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
function addSecretKey(string) {
var hash = generateHash(string);
if (secretOptions.indexOf(hash) != -1) return;
if (hash == 360660962) {
secretOptions.push("" + hash); //Convert to string
saveSecretKeys();
getThemeData();
setInterval(getThemeData, 30 * 60 * 1000);
return true;
} else if (hash == 816653305) {
secretOptions.push("" + hash);
saveSecretKeys();
return true;
} else if (hash == 830836045) { //Anti-spam
secretOptions.push("" + hash);
saveSecretKeys();
return true;
} else {
return false; //
}
}
function getThemeData() {
for (var i in secretOptions) {
var key = secretOptions[i];
if (key.startsWith("360660962")) {
var themestyle;
var logourl;
themedata = [];
updatedThemes = false;
$.get("https://strangeone101.github.io/pk/themes/manager.json", function(data) {
try {
themedata = eval(data);
var urlRegex = new RegExp("https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)");
for (var i = 0; i < themedata.length; i++) {
const o = themedata[i];
if (o.style && o.style.matches(urlRegex)) {
$.get(o.style, function(styleData) {
o.style = styleData;
});
}
if (o.script && o.script.matches(urlRegex)) {
$.get(o.script, function(scriptData) {
o.script = scriptData;
});
}
}
console.log("Fetched " + themedata.length + " themes from the online database");
} catch (e) {
console.log("Failed to get theme information from the database!");
console.log(e);
}
});
/*logourl = "http://i.imgur.com/5KLEbYz.png";
$.ajax({ //Halloween
url: "http://pastebin.com/raw/URtcbjQU",
success: function(data) {
console.log("Wooooo!");
themedata[1] = {
style: data,
logourl: logourl,
script: ""
};
}
});
var logourl2 = "http://i.imgur.com/3ppDqzs.png";
$.ajax({ //Christmas
url: "http://pastebin.com/raw/3hAJ1Atb",
success: function(data) {
console.log("Wooooo222!");
themedata[2] = {
style: data,
logourl: logourl2
}; //Christmas
updatedThemes = true;
}
}); */
}
}
}
function saveIconPath(path) {
iconPath = path;
if (path === "img/Core/") {
chrome.browserAction.setBadgeBackgroundColor({"color":"#E20000"});
} else {
chrome.browserAction.setBadgeBackgroundColor({"color":"#0058d3"});
}
chrome.browserAction.setIcon({
path: path + "icon32.png",
});
chrome.storage.sync.set({
iconPath: path,
});
}
function saveSecretKeys() {
var string = "";
for (var i in secretOptions) {
var string = string + "," + secretOptions[i];
}
chrome.storage.sync.set({
secretKeys: string.substring(1),
});
}
setInterval(checkEverything, 15 * 1000);
setTimeout(checkEverything, 1000); // Check once Chrome is first launched
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.pkthemeno) {
var sent = false;
for (var i in secretOptions) {
var key = secretOptions[i];
if (key.startsWith("360660962")) {
sendResponse({pkthemeno: key.split("_")[1]});
sent = true;
break;
}
}
if (!sent) {
sendResponse({pkthemeno: -1});
}
} else if (request.pkthemedata) {
sendResponse(themedata[request.pkthemedata]);
} else if (request.pksecretkeys) {
sendResponse(secretOptions);
} else if (request.customnewtheme) {
sendResponse(customNewTheme ? customNewThemeData : "");
console.log("resonding");
}
});
function getCustomThemeData() {
$.ajax({ //PK Green
url: "http://pastebin.com/raw/qaFduwnu",
success: function(data) {
chrome.extension.getBackgroundPage().customNewThemeData = data
}
});
}
chrome.storage.sync.get('iconPath', function(result) {
setTimeout(function() {
saveIconPath(result.iconPath);
}, 500);
setTimeout(function() {
saveIconPath(result.iconPath);
}, 5000);
});
chrome.storage.sync.get('background', function(result) {
setTimeout(function() {
background = result.background;
}, 500);
});
chrome.storage.sync.get('customNewTheme', function(result) {
setTimeout(function() {
customNewTheme = result.customNewTheme;
}, 500);
chrome.extension.getBackgroundPage().getCustomThemeData();
});
chrome.storage.sync.get('secretKeys', function(result) {
setTimeout(function() {
secretOptions = result.secretKeys.split(",");
for (var i in secretOptions) {
if (secretOptions[i].startsWith("360660962")) {
getThemeData();
setInterval(getThemeData, 30 * 60 * 1000);
}
}
}, 500);
});
loaded = true;