-
Notifications
You must be signed in to change notification settings - Fork 10
/
eventPage.js
229 lines (211 loc) · 6.06 KB
/
eventPage.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
let previous_events;
let current_events;
let button1Text;
let button2Text;
let button1Link;
let button2Link;
let blogId = 0;
let previous_blogs;
let current_blogs;
// current year , URL , headers for fetching the data
let year = new Date().getFullYear();
let yearLink = `https://usict.acm.org/eventYear.php?year=${year}`;
let blogLiveLink = `https://usict.acm.org/singleBlog.php?Id=${blogId}`;
const url = new URL(
`http://localhost/acm-new/admin/blogadmin/api.php/?q=readAllEvent&year=${year}`
);
const blogAPIURL = new URL(
`https://usict.acm.org/admin/blogAdmin/api.php/?q=readAll`
);
let headers = {
"Content-Type": "application/json",
Accept: "application/json",
};
// if Extension is Installed or Updated then redirecting it to some page
let installURL = "";
let updateURL = "";
let uninstallURL = "";
chrome.runtime.setUninstallURL(uninstallURL, () => {
// chrome.notifications.create({
// title: "ACM Alerts",
// message: `Extension is Removed Successfully`,
// iconUrl: "./assets/logo.png",
// type: "basic",
// priority: 2,
// });
});
let installReason = (details) => {
// console.log(details.reason);
if (details.reason === "install") {
fetch(url, {
method: "GET",
headers: headers,
})
.then((response) => response.json())
.then((data) => {
// use of local storage
chrome.storage.local.set({ numberOfEvents: data[0].length });
});
// fetch blog data
fetch(blogAPIURL, {
method: "GET",
headers: headers,
})
.then((response) => response.json())
.then((data) => {
// use of local storage
chrome.storage.local.set({ numberOfBlogs: data[0].length });
});
chrome.notifications.create({
title: "ACM Alerts",
message: `Extension is Added Successfully`,
iconUrl: "./assets/logo.png",
type: "basic",
priority: 2,
});
} else if (details.reason === "update") {
// chrome.notifications.onClicked.addListener(onClickNotification);
// notification();
}
};
function onClickNotification() {
chrome.tabs.create({
url: updateURL,
});
}
chrome.runtime.onInstalled.addListener((details) => {
installReason(details);
});
// function to show the update extension notification
function notification(eventName) {
let tapTo;
if (button2Text !== "" && button1Text !== "") {
tapTo = "Register";
} else if (button1Text !== "" && button2Text === "") {
tapTo = button1Text;
} else if (button2Text !== "" && button1Text === "") {
tapTo = button2Text;
} else {
tapTo = "View Event";
}
// console.log("dfghjj " + eventName);
chrome.notifications.create({
title: "ACM Alerts",
message: `Event : ${eventName} \nTap to ${tapTo}`,
iconUrl: "./assets/logo.png",
type: "basic",
priority: 2,
});
}
function notificationBlogs(blogTitle, blogAuthor, Sno) {
chrome.notifications.create({
title: "ACM Alerts",
message: `Blog : ${blogTitle} \nBy - ${blogAuthor}\n\nTap to view`,
iconUrl: "./assets/logo.png",
type: "basic",
priority: 2,
});
}
chrome.notifications.onClicked.addListener(function (notifId) {
// console.log(yearLink);
console.log(blogId);
// window.open(link, "_blank");
// if (blogId) {
// chrome.tabs.create({
// url: blogLiveLink,
// });
// }
console.log(button1Text);
console.log(button2Text);
if (
button2Text !== "" &&
button1Text !== "" &&
button2Text !== undefined &&
button1Text !== undefined
) {
if (button1Text.includes("Register")) {
chrome.tabs.create({
url: button1Link,
});
} else {
chrome.tabs.create({
url: button2Link,
});
}
} else if (button1Text !== "" && button2Text === "") {
chrome.tabs.create({
url: button1Link,
});
} else if (button2Text !== "" && button1Text === "") {
chrome.tabs.create({
url: button2Link,
});
} else if (button2Text == "" && button1Text == "") {
chrome.tabs.create({
url: yearLink,
});
} else {
// console.log(12);
if (blogId) {
// console.log(21);
// console.log(blogId);
console.log(blogLiveLink);
chrome.tabs.create({
url: `https://usict.acm.org/singleBlog.php?Id=${blogId}`,
});
}
}
});
// fetching the data
function countNumberOfEvents() {
// fetch number of events before fetching again
chrome.storage.local.get(["numberOfEvents"], (numberOfEvents) => {
previous_events = numberOfEvents.numberOfEvents;
});
fetch(url, {
method: "GET",
headers: headers,
})
.then((response) => response.json())
.then((data) => {
// use of local storage
chrome.storage.local.set({ numberOfEvents: data[0].length });
current_events = data[0].length;
// console.log(previous_events + " " + current_events);
let eventName;
if (current_events > previous_events) {
eventName = data[0][data[0].length - 1].name;
button1Text = data[0][data[0].length - 1].button1Text;
button2Text = data[0][data[0].length - 1].button2Text;
button1Link = data[0][data[0].length - 1].button1Link;
button2Link = data[0][data[0].length - 1].button2Link;
notification(eventName);
}
});
// fethcing blogs
chrome.storage.local.get(["numberOfBlogs"], (numberOfBlogs) => {
previous_blogs = numberOfBlogs.numberOfBlogs;
});
fetch(blogAPIURL, {
method: "GET",
headers: headers,
})
.then((response) => response.json())
.then((data) => {
// use of local storage
chrome.storage.local.set({ numberOfBlogs: data[0].length });
current_blogs = data[0].length;
// console.log(previous_events + " " + current_events);
if (current_blogs > previous_blogs - 1) {
// console.log(data[0][data[0].length - 1]);
blogTitle = data[0][data[0].length - 1].Title;
blogAuthor = data[0][data[0].length - 1].Author;
blogId = data[0][data[0].length - 1].Sno;
// console.log(blogTitle);
notificationBlogs(blogTitle, blogAuthor, blogId);
}
});
}
setInterval(function () {
countNumberOfEvents();
}, 5000);