This repository was archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
62 lines (59 loc) · 2.25 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
const checkIfSignedIn = async function () {
const response = await fetch("https://www.postlit.dev/me/");
const data = await response.json();
return !data.error;
};
const setMessageCount = async function () {
const isSignedIn = await checkIfSignedIn();
if (isSignedIn) {
const response = await fetch("https://www.postlit.dev/messages/count/");
const data = await response.json();
chrome.action.setBadgeText({ text: data.count.toString() });
chrome.action.setBadgeBackgroundColor({ color: "#9688F1" });
const lastMessageCount = (await chrome.storage.sync.get("message-count"))[
"message-count"
];
await chrome.storage.sync.set({ "message-count": data.count });
if (lastMessageCount < data.count) {
const messages = await fetch("https://www.postlit.dev/my/messages/");
const messageData = await messages.json();
messageData.forEach(function (message, i) {
const messageTypes = {
"repost": `@${message.creator} just reposted your post!`,
"mention": `@${message.creator} just mentioned you in their post!`,
"comment-mention": `@${message.creator} just mentioned you in their comment!`,
"comment-reply": `@${message.creator} just replied to your comment!`,
"comment": `@${message.creator} just commented on your post!`,
"follow": `@${message.creator} just followed you!`,
}
if (i + 1 > lastMessageCount) {
chrome.notifications.create(message._id, {
title: "@" + message.creator + " on postLit",
message: messageTypes[message.type],
iconUrl: "/icons/icon128.png",
type: "basic",
});
}
});
}
} else {
chrome.action.setBadgeText({ text: "?" });
chrome.action.setBadgeBackgroundColor({ color: "#f18888" });
}
};
chrome.runtime.onInstalled.addListener(async function (object) {
chrome.alarms.clearAll();
chrome.alarms.create("displayMessageCount", {
delayInMinutes: 0.1,
periodInMinutes: 0.1,
});
setMessageCount();
});
chrome.alarms.onAlarm.addListener(async function () {
chrome.alarms.clearAll();
chrome.alarms.create("displayMessageCount", {
delayInMinutes: 0.1,
periodInMinutes: 0.1,
});
setMessageCount();
});